Java Code Examples for org.eclipse.jdt.core.dom.AST#newTypeDeclaration()

The following examples show how to use org.eclipse.jdt.core.dom.AST#newTypeDeclaration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmptyBuilderClassGeneratorFragment.java    From SparkBuilderGenerator with MIT License 6 votes vote down vote up
public TypeDeclaration createBuilderClass(AST ast, TypeDeclaration originalType) {
    TypeDeclaration builderType = ast.newTypeDeclaration();
    builderType.setName(ast.newSimpleName(getBuilderName(originalType)));

    if (preferencesManager.getPreferenceValue(ADD_GENERATED_ANNOTATION)) {
        generatedAnnotationPopulator.addGeneratedAnnotation(ast, builderType);
    }
    builderType.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    builderType.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
    builderType.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));

    if (preferencesManager.getPreferenceValue(ADD_JACKSON_DESERIALIZE_ANNOTATION)) {
        jsonPOJOBuilderAdderFragment.addJsonPOJOBuilder(ast, builderType);
    }

    if (preferencesManager.getPreferenceValue(GENERATE_JAVADOC_ON_BUILDER_CLASS)) {
        Javadoc javadoc = javadocGenerator.generateJavadoc(ast, String.format(Locale.ENGLISH, "Builder to build {@link %s}.", originalType.getName().toString()),
                Collections.emptyMap());
        builderType.setJavadoc(javadoc);
    }

    return builderType;
}
 
Example 2
Source File: StagedBuilderInterfaceTypeDefinitionCreatorFragment.java    From SparkBuilderGenerator with MIT License 5 votes vote down vote up
public TypeDeclaration createStageBuilderInterface(AST ast, String interfaceName) {
    TypeDeclaration addedInterface = ast.newTypeDeclaration();
    addedInterface.setInterface(true);
    addedInterface.setName(ast.newSimpleName(interfaceName));
    addedInterface.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    javadocAdder.addJavadocForStagedInterface(ast, interfaceName, addedInterface);
    return addedInterface;
}
 
Example 3
Source File: NewCUProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private TextEdit constructEnclosingTypeEdit(ICompilationUnit icu) throws CoreException {
	ASTParser astParser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
	astParser.setSource(icu);
	CompilationUnit cu = (CompilationUnit) astParser.createAST(null);
	TypeDeclaration enclosingDecl = findEnclosingTypeDeclaration(cu, fTypeContainer.getElementName());
	AST ast = cu.getAST();
	ASTRewrite rewrite = ASTRewrite.create(ast);
	final AbstractTypeDeclaration newDeclaration;
	switch (fTypeKind) {
		case K_CLASS:
			newDeclaration = ast.newTypeDeclaration();
			((TypeDeclaration) newDeclaration).setInterface(false);
			break;
		case K_INTERFACE:
			newDeclaration = ast.newTypeDeclaration();
			((TypeDeclaration) newDeclaration).setInterface(true);
			break;
		case K_ENUM:
			newDeclaration = ast.newEnumDeclaration();
			break;
		case K_ANNOTATION:
			newDeclaration = ast.newAnnotationTypeDeclaration();
			break;
		default:
			return null;
	}
	newDeclaration.setJavadoc(null);
	newDeclaration.setName(ast.newSimpleName(ASTNodes.getSimpleNameIdentifier(fNode)));
	newDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
	if (isParameterizedType(fTypeKind, fNode)) {
		addTypeParameters((TypeDeclaration) newDeclaration);
	}

	ListRewrite lrw = rewrite.getListRewrite(enclosingDecl, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
	lrw.insertLast(newDeclaration, null);
	return rewrite.rewriteAST();
}
 
Example 4
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private AbstractTypeDeclaration createNewNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
	final AST ast= fAnonymousInnerClassNode.getAST();

	final TypeDeclaration newDeclaration= ast.newTypeDeclaration();
	newDeclaration.setInterface(false);
	newDeclaration.setJavadoc(null);
	newDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiersForNestedClass()));
	newDeclaration.setName(ast.newSimpleName(fClassName));

	TypeParameter parameter= null;
	for (int index= 0; index < typeParameters.length; index++) {
		parameter= ast.newTypeParameter();
		parameter.setName(ast.newSimpleName(typeParameters[index].getName()));
		newDeclaration.typeParameters().add(parameter);
	}
	setSuperType(newDeclaration);

	IJavaProject project= fCu.getJavaProject();

	IVariableBinding[] bindings= getUsedLocalVariables();
	ArrayList<String> fieldNames= new ArrayList<String>();
	for (int i= 0; i < bindings.length; i++) {
		String name= StubUtility.getBaseName(bindings[i], project);
		String[] fieldNameProposals= StubUtility.getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, name, 0, fieldNames, true);
		fieldNames.add(fieldNameProposals[0]);


		if (fLinkedProposalModel != null) {
			LinkedProposalPositionGroup positionGroup= fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true);
			for (int k= 0; k < fieldNameProposals.length; k++) {
				positionGroup.addProposal(fieldNameProposals[k], null, fieldNameProposals.length - k);
			}
		}
	}
	String[] allFieldNames= fieldNames.toArray(new String[fieldNames.size()]);

	List<BodyDeclaration> newBodyDeclarations= newDeclaration.bodyDeclarations();

	createFieldsForAccessedLocals(rewrite, bindings, allFieldNames, newBodyDeclarations);

	MethodDeclaration newConstructorDecl= createNewConstructor(rewrite, bindings, allFieldNames);
	if (newConstructorDecl != null) {
		newBodyDeclarations.add(newConstructorDecl);
	}

	updateAndMoveBodyDeclarations(rewrite, bindings, allFieldNames, newBodyDeclarations, newConstructorDecl);

	if (doAddComments()) {
		String[] parameterNames= new String[typeParameters.length];
		for (int index= 0; index < parameterNames.length; index++) {
			parameterNames[index]= typeParameters[index].getName();
		}
		String string= CodeGeneration.getTypeComment(rewrite.getCu(), fClassName, parameterNames, StubUtility.getLineDelimiterUsed(fCu));
		if (string != null) {
			Javadoc javadoc= (Javadoc) rewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC);
			newDeclaration.setJavadoc(javadoc);
		}
	}
	if (fLinkedProposalModel != null) {
		addLinkedPosition(KEY_TYPE_NAME, newDeclaration.getName(), rewrite.getASTRewrite(), false);
		ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite.getASTRewrite(), newDeclaration.modifiers(), false);
	}

	return newDeclaration;
}