Search in sources :

Example 81 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project whole by wholeplatform.

the class EntityDescriptorEnumBuilder method addSimpleEntity.

// public void addSupertypes(String eType, String name, Set<String> supertypes) {
// if (supertypes.size()>1) {//!supertypes.isEmpty()) {
// ExpressionStatement expStm = etypeExpressionMap.get(eType);
// Expression edExp = expStm.getExpression();
// 
// MethodInvocation nfd = newMethodInvocation("withSupertypes");
// expStm.setExpression(nfd);
// nfd.setExpression(edExp); // after unparenting edExp
// 
// for (String sType : supertypes)
// if (!sType.equals(name))
// nfd.arguments().add(ast.newSimpleName(sType+"_ord"));
// }
// }
@SuppressWarnings("unchecked")
public void addSimpleEntity(String eName, String name, String eType, Set<String> modifiers, Set<String> allSubtypes) {
    String eName_Ord = eName + "_ord";
    String oldType = entities.put(eName, eType);
    if (oldType == null) {
        // public static final int [eName_ord] = [nextOrdinal];
        FieldDeclaration fieldDecl = newFieldDeclaration("int", eName_Ord, newLiteral(nextOrdinal));
        // assume ModifierKeyword.PRIVATE_KEYWORD
        fieldDecl.modifiers().remove(0);
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
        addBodyDeclaration(nextOrdinal++, fieldDecl);
        // public static final EntityDescriptor<eName> [eName] = (EntityDescriptor<eName>) instance.valueOf([eName_ord]);
        fieldDecl = newFieldDeclaration(newParameterizedType(EntityDescriptor.class.getName(), eName), newVariableDeclarationFragment(eName, newCastExpression(newParameterizedType(EntityDescriptor.class.getName(), eName), newMethodInvocation("instance", "valueOf", ast.newSimpleName(eName_Ord)))));
        // assume ModifierKeyword.PRIVATE_KEYWORD
        fieldDecl.modifiers().remove(0);
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
        addBodyDeclaration(nextOrdinal * 2, fieldDecl);
    } else if (oldType.equals(eType))
        return;
    // putSimpleEntity([eName_ord], "[eName]", [eType].class);
    MethodInvocation callExp = newMethodInvocation("putSimpleEntity");
    callExp.arguments().add(ast.newSimpleName(eName_Ord));
    callExp.arguments().add(newLiteral(name));
    if (!eName.equals(name))
        callExp.arguments().add(newLiteral(eName));
    callExp.arguments().add(newTypeLiteral(eType));
    callExp.arguments().add(newLiteral(modifiers != null && modifiers.contains("ABSTRACT")));
    if (!allSubtypes.isEmpty())
        for (String type : allSubtypes) if (!type.equals(name))
            callExp.arguments().add(ast.newSimpleName(type + "_ord"));
    ExpressionStatement expStm = newExpressionStatement(callExp);
    initEntityDescriptors.getBody().statements().add(expStm);
    etypeExpressionMap.put(eType, expStm);
}
Also used : EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 82 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project whole by wholeplatform.

the class EnumTypeBuilder method addValue.

public void addValue(String valueName, String valueImplName) {
    String valueName_ord = valueImplName + "_ord";
    String oldType = (String) values.put(valueImplName, valueType);
    if (oldType == null) {
        // public static final int [valueName_ord] = [nextOrdinal];
        FieldDeclaration fieldDecl = newFieldDeclaration("int", valueName_ord, newLiteral(nextOrdinal));
        // assume ModifierKeyword.PRIVATE_KEYWORD
        fieldDecl.modifiers().remove(0);
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
        addBodyDeclaration(nextOrdinal++, fieldDecl);
        // public static final Value [valueName] = instance.valueOf([valueName_ord]);
        MethodInvocation callExp = newMethodInvocation("instance", "valueOf");
        callExp.arguments().add(ast.newSimpleName(valueName_ord));
        fieldDecl = newFieldDeclaration("Value", valueImplName, callExp);
        // assume ModifierKeyword.PRIVATE_KEYWORD
        fieldDecl.modifiers().remove(0);
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
        fieldDecl.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
        addBodyDeclaration(nextOrdinal * 2, fieldDecl);
        // enumValue([valueName_ord], "[valueName]");
        callExp = newMethodInvocation("enumValue");
        callExp.arguments().add(ast.newSimpleName(valueName_ord));
        callExp.arguments().add(newLiteral(valueName));
        if (!valueName.equals(valueImplName))
            callExp.arguments().add(newLiteral(valueImplName));
        constructor.getBody().statements().add(ast.newExpressionStatement(callExp));
    }
}
Also used : MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 83 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project whole by wholeplatform.

the class CompilationUnitBuilder method addSingletonField.

public MethodDeclaration addSingletonField(String typeName, String implName) {
    FieldDeclaration instanceField = newFieldDeclaration(typeName, "instance", newClassInstanceCreation(implName));
    // assume ModifierKeyword.PRIVATE_KEYWORD
    instanceField.modifiers().remove(0);
    instanceField.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
    instanceField.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
    instanceField.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
    addBodyDeclaration(0, instanceField);
    MethodDeclaration constructor = null;
    if (!isInterface) {
        constructor = newConstructorDeclaration(typeDec);
        // assume ModifierKeyword.PUBLIC_KEYWORD
        constructor.modifiers().remove(0);
        constructor.modifiers().add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
        addBodyDeclaration(1, constructor);
    }
    return constructor;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 84 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project generator by mybatis.

the class JavaFileMerger method getMergedSource.

@SuppressWarnings({ "unchecked", "rawtypes" })
public String getMergedSource() throws ShellException, InvalidExistingFileException {
    NewJavaFileVisitor newJavaFileVisitor = visitNewJavaFile();
    IDocument document = new Document(existingJavaSource);
    // delete generated stuff, and collect imports
    ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(javaDocTags);
    CompilationUnit cu = getCompilationUnitFromSource(existingJavaSource);
    AST ast = cu.getAST();
    cu.recordModifications();
    cu.accept(visitor);
    TypeDeclaration typeDeclaration = visitor.getTypeDeclaration();
    if (typeDeclaration == null) {
        throw new InvalidExistingFileException(ErrorCode.NO_TYPES_DEFINED_IN_FILE);
    }
    // reconcile the superinterfaces
    List<Type> newSuperInterfaces = getNewSuperInterfaces(typeDeclaration.superInterfaceTypes(), newJavaFileVisitor);
    for (Type newSuperInterface : newSuperInterfaces) {
        typeDeclaration.superInterfaceTypes().add(ASTNode.copySubtree(ast, newSuperInterface));
    }
    // set the superclass
    if (newJavaFileVisitor.getSuperclass() != null) {
        typeDeclaration.setSuperclassType((Type) ASTNode.copySubtree(ast, newJavaFileVisitor.getSuperclass()));
    } else {
        typeDeclaration.setSuperclassType(null);
    }
    // interface or class?
    if (newJavaFileVisitor.isInterface()) {
        typeDeclaration.setInterface(true);
    } else {
        typeDeclaration.setInterface(false);
    }
    // reconcile the imports
    List<ImportDeclaration> newImports = getNewImports(cu.imports(), newJavaFileVisitor);
    for (ImportDeclaration newImport : newImports) {
        Name name = ast.newName(newImport.getName().getFullyQualifiedName());
        ImportDeclaration newId = ast.newImportDeclaration();
        newId.setName(name);
        cu.imports().add(newId);
    }
    TextEdit textEdit = cu.rewrite(document, null);
    try {
        textEdit.apply(document);
    } catch (BadLocationException e) {
        throw new ShellException("BadLocationException removing prior fields and methods");
    }
    // regenerate the CompilationUnit to reflect all the deletes and changes
    CompilationUnit strippedCu = getCompilationUnitFromSource(document.get());
    // find the top level public type declaration
    TypeDeclaration topLevelType = null;
    Iterator iter = strippedCu.types().iterator();
    while (iter.hasNext()) {
        TypeDeclaration td = (TypeDeclaration) iter.next();
        if (td.getParent().equals(strippedCu) && (td.getModifiers() & Modifier.PUBLIC) > 0) {
            topLevelType = td;
            break;
        }
    }
    // now add all the new methods and fields to the existing
    // CompilationUnit with a ListRewrite
    ASTRewrite rewrite = ASTRewrite.create(topLevelType.getRoot().getAST());
    ListRewrite listRewrite = rewrite.getListRewrite(topLevelType, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
    Iterator<ASTNode> astIter = newJavaFileVisitor.getNewNodes().iterator();
    int i = 0;
    while (astIter.hasNext()) {
        ASTNode node = astIter.next();
        if (node.getNodeType() == ASTNode.TYPE_DECLARATION) {
            String name = ((TypeDeclaration) node).getName().getFullyQualifiedName();
            if (visitor.containsInnerClass(name)) {
                continue;
            }
        } else if (node instanceof FieldDeclaration) {
            addExistsAnnotations((BodyDeclaration) node, visitor.getFieldAnnotations((FieldDeclaration) node));
        } else if (node instanceof MethodDeclaration) {
            addExistsAnnotations((BodyDeclaration) node, visitor.getMethodAnnotations((MethodDeclaration) node));
        }
        listRewrite.insertAt(node, i++, null);
    }
    textEdit = rewrite.rewriteAST(document, JavaCore.getOptions());
    try {
        textEdit.apply(document);
    } catch (BadLocationException e) {
        throw new ShellException("BadLocationException adding new fields and methods");
    }
    String newSource = document.get();
    return newSource;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Name(org.eclipse.jdt.core.dom.Name) Iterator(java.util.Iterator) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ShellException(org.mybatis.generator.exception.ShellException) Type(org.eclipse.jdt.core.dom.Type) TextEdit(org.eclipse.text.edits.TextEdit) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 85 with FieldDeclaration

use of org.eclipse.jdt.core.dom.FieldDeclaration in project flux by eclipse.

the class ScopeAnalyzer method addLocalDeclarations.

private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
    if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
        if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
            ScopeAnalyzerVisitor visitor = new ScopeAnalyzerVisitor(offset, flags, requestor);
            declaration.accept(visitor);
            return visitor.fBreak;
        }
    }
    return false;
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)103 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)53 ASTNode (org.eclipse.jdt.core.dom.ASTNode)51 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)46 AST (org.eclipse.jdt.core.dom.AST)26 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)26 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)26 Type (org.eclipse.jdt.core.dom.Type)24 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)22 Block (org.eclipse.jdt.core.dom.Block)17 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)16 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)16 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)16 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)16 ArrayList (java.util.ArrayList)15 SimpleName (org.eclipse.jdt.core.dom.SimpleName)15 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)14 Expression (org.eclipse.jdt.core.dom.Expression)14 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)14 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)13