Example usage for org.eclipse.jdt.core.dom FieldDeclaration getModifiers

List of usage examples for org.eclipse.jdt.core.dom FieldDeclaration getModifiers

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom FieldDeclaration getModifiers.

Prototype

public int getModifiers() 

Source Link

Document

Returns the modifiers explicitly specified on this declaration.

Usage

From source file:ca.mcgill.cs.swevo.jayfx.ASTCrawler.java

License:Open Source License

@Override
public boolean visit(final FieldDeclaration pNode) {
    @SuppressWarnings("rawtypes")
    final List fragments = pNode.fragments();
    IElement lField;/*ww  w  .ja  v  a2  s .  c  o m*/

    this.aCurrMethodReminder.push(this.aCurrMethod);

    if (Modifier.isStatic(pNode.getModifiers())) {
        this.aCurrMethod = (MethodElement) FlyweightElementFactory.getElement(Category.METHOD,
                this.aCurrType.getId() + "." + ASTCrawler.aCLINIT_METHOD_NAME);
        if (!this.aDB.contains(this.aCurrMethod))
            // This <clinit>() method will be in any class that has a static
            // field with initialization
            // But the DECLARES relation will be linked only if this method
            // has at least one sub relations
            // The linkage is done at the end of traversing each
            // compilations unit while end visiting type Declaration
            this.aDB.addElement(this.aCurrMethod, pNode.getModifiers());
    } else
        this.aCurrMethod = this.aTempMethod;

    // Consider multiple declaration in one statment
    for (@SuppressWarnings("rawtypes")
    final Iterator itr = fragments.iterator(); itr.hasNext();) {
        final VariableDeclarationFragment fragment = (VariableDeclarationFragment) itr.next();
        final String lSimpleName = fragment.getName().getIdentifier();
        final Expression lInit = fragment.getInitializer();

        if (lSimpleName != null) {
            lField = FlyweightElementFactory.getElement(Category.FIELD,
                    this.aCurrType.getId() + "." + lSimpleName);
            this.aDB.addElement(lField, pNode.getModifiers());

            this.aDB.addRelation(this.aCurrType, Relation.DECLARES_FIELD, lField);

            // If there is any initialization to this field then we write
            // them as an access by <init> or <clinit>
            if (lInit != null) {
                this.aDB.addRelationAndTranspose(this.aCurrMethod, Relation.ACCESSES, lField);
                this.aDB.addRelationAndTranspose(this.aCurrMethod, Relation.SETS, lField);

                // Want to go into the right side of assignment operator
                lInit.accept(this);
            }
        }
    }

    // Because we have covered everything we need about field declaration,
    // we dont' have to go deeper into this node.
    // return false;
    return true;
}

From source file:com.architexa.diagrams.jdt.extractors.TypeMembersModifierExtractor.java

License:Open Source License

@Override
public boolean visit(VariableDeclarationFragment variableDecl) {
    FieldDeclaration fieldDecl = (FieldDeclaration) getParent(FieldDeclaration.class);
    if (fieldDecl == null)
        return true; // var. decl. in statements

    Resource variableDeclRes = bindingToResource(variableDecl.resolveBinding());
    addModifiers(variableDeclRes, fieldDecl.getModifiers());
    return true;/*w  w  w  .j av  a  2  s .c  o  m*/
}

From source file:com.google.devtools.j2cpp.gen.CppImplementationGenerator.java

License:Open Source License

private void printStaticVars(List<FieldDeclaration> fields) {
    boolean hadStaticVar = false;
    for (FieldDeclaration f : fields) {
        if (Modifier.isStatic(f.getModifiers())) {
            @SuppressWarnings("unchecked")
            List<VariableDeclarationFragment> fragments = f.fragments(); // safe by specification
            for (VariableDeclarationFragment var : fragments) {
                IVariableBinding binding = Types.getVariableBinding(var);
                if (!Types.isPrimitiveConstant(binding)) {
                    String name = NameTable.getName(binding);
                    Expression initializer = var.getInitializer();
                    if (initializer != null) {
                        printConstant(name, initializer);
                    } else {
                        printf("static %s %s;\n", NameTable.javaRefToCpp(f.getType()), name);
                    }//from   w  w  w  .j  av  a 2 s.co m
                    hadStaticVar = true;
                }
            }
        }
    }
    if (hadStaticVar) {
        newline();
    }
}

From source file:com.google.devtools.j2cpp.gen.CppImplementationGenerator.java

License:Open Source License

private void printProperties(FieldDeclaration[] fields) {
    int nPrinted = 0;
    for (FieldDeclaration field : fields) {
        if ((field.getModifiers() & Modifier.STATIC) == 0) {
            @SuppressWarnings("unchecked")
            List<VariableDeclarationFragment> vars = field.fragments(); // safe by definition
            for (VariableDeclarationFragment var : vars) {
                if (var.getName().getIdentifier().startsWith("this$") && superDefinesVariable(var)) {
                    // Don't print, as it shadows an inner field in a super class.
                    continue;
                }//  w w  w  . jav a2 s  .co  m

                String name = NameTable.getName(var.getName());
                ITypeBinding type = Types.getTypeBinding(field.getType());
                String typeString = NameTable.javaRefToCpp(type);
                if (!typeString.endsWith("*")) {
                    typeString += " ";
                }

                // Don't emit the getter when there is already a method with the
                // same name.
                // TODO(user,user): Update when getters are merged with property
                // accessors (see issues).
                boolean noGetter = false;
                ITypeBinding declaringClass = Types.getTypeBinding(field.getParent());
                if (declaringClass != null) {
                    IMethodBinding[] methods = declaringClass.getDeclaredMethods();
                    for (IMethodBinding method : methods) {
                        if (method.getName().equals(name) && method.getParameterTypes().length == 0) {
                            noGetter = true;
                            break;
                        }
                    }
                }

                String objCFieldName = NameTable.javaFieldToCpp(name);

                // Getter
                if (!noGetter) {
                    printf(String.format("- (%s)%s {\n  return %s;\n}\n\n", typeString.trim(), name,
                            objCFieldName));
                }

                // Setter
                printf(String.format("- (void)set%s:(%s)new%s {\n", NameTable.capitalize(name),
                        typeString.trim(), NameTable.capitalize(name)));
                if (type.isPrimitive()) {
                    printf(String.format("  %s = new%s;\n}\n\n", objCFieldName, NameTable.capitalize(name)));
                } else if (Options.useReferenceCounting()
                        && !Types.isWeakReference(Types.getVariableBinding(var))) {
                    String retentionMethod = type.isEqualTo(Types.getNSString()) ? "copy" : "retain";
                    printf(String.format("  [%s autorelease];\n  %s = [new%s %s];\n}\n\n", objCFieldName,
                            objCFieldName, NameTable.capitalize(name), retentionMethod));
                } else {
                    printf(String.format("  %s = new%s;\n}\n\n", objCFieldName, NameTable.capitalize(name)));
                }
                nPrinted++;
            }
        }
    }
    if (nPrinted > 0) {
        newline();
    }
}

From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java

License:Open Source License

/**
 * Initializes final instance fields in classes whose constructors have all
 * been deleted.  This must take place after dead methods are removed.
 *///w w w  .  j  av  a 2 s .  c o m
private void initializeFinalFields(List<BodyDeclaration> body) {
    Set<VariableDeclarationFragment> finalVars = Sets.newHashSet();
    for (BodyDeclaration declaration : body) {
        // If there's any non-generated constructor remaining, it must initialize
        // all final fields.
        if (declaration instanceof MethodDeclaration) {
            MethodDeclaration method = (MethodDeclaration) declaration;
            if (!generatedMethods.contains(method) && method.isConstructor()) {
                return;
            }
        }
        // Collect final fields that lack initializers.
        if (declaration instanceof FieldDeclaration) {
            FieldDeclaration field = (FieldDeclaration) declaration;
            int modifiers = field.getModifiers();
            if (!Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
                @SuppressWarnings("unchecked")
                List<VariableDeclarationFragment> fragments = field.fragments();
                for (VariableDeclarationFragment fragment : fragments) {
                    if (fragment.getInitializer() == null) {
                        finalVars.add(fragment);
                    }
                }
            }
        }
    }
    // If we get this far, there are no explicit constructors left.
    for (VariableDeclarationFragment var : finalVars) {
        if (var.getInitializer() == null) {
            addFieldInitializer(var);
        }
    }
}

From source file:com.google.devtools.j2cpp.translate.DestructorGenerator.java

License:Open Source License

@Override
public boolean visit(TypeDeclaration node) {
    final List<IVariableBinding> releaseableFields = Lists.newArrayList();
    for (final FieldDeclaration field : node.getFields()) {
        if (!field.getType().isPrimitiveType() && !isStatic(field)) {
            ErrorReportingASTVisitor varFinder = new ErrorReportingASTVisitor() {
                @Override//from   w w  w.  j ava2s  . c  o  m
                public boolean visit(VariableDeclarationFragment node) {
                    IVariableBinding binding = Types.getVariableBinding(node);
                    if (!Modifier.isStatic(field.getModifiers()) && !Types.isConstantVariable(binding)) {
                        releaseableFields.add(binding);
                    }
                    return true;
                }
            };
            varFinder.run(field);
        }
    }
    if (!releaseableFields.isEmpty()) {
        Types.addReleaseableFields(releaseableFields);

        boolean foundDestructor = false;

        // If a destructor method already exists, append release statements.
        for (MethodDeclaration method : node.getMethods()) {
            if (FINALIZE_METHOD.equals(method.getName().getIdentifier())) {
                addReleaseStatements(method, releaseableFields);
                foundDestructor = true;
            }
        }

        // No destructor, so create a new one.
        if (!foundDestructor && !Options.useARC()) {
            MethodDeclaration finalizeMethod = buildFinalizeMethod(node.getAST(), Types.getTypeBinding(node),
                    releaseableFields);
            @SuppressWarnings("unchecked")
            List<BodyDeclaration> declarations = node.bodyDeclarations();
            declarations.add(finalizeMethod);
        }
    }

    // Rename method to correct destructor name.  This is down outside of
    // the loop above, because a class may have a finalize() method but no
    // releasable fields.
    for (MethodDeclaration method : node.getMethods()) {
        if (needsRenaming(method.getName())) {
            NameTable.rename(Types.getBinding(method), destructorName);
        }
    }
    return super.visit(node);
}

From source file:com.google.devtools.j2cpp.translate.DestructorGenerator.java

License:Open Source License

private boolean isStatic(FieldDeclaration f) {
    return (f.getModifiers() & Modifier.STATIC) != 0;
}

From source file:com.google.devtools.j2cpp.translate.InitializationNormalizer.java

License:Open Source License

/**
 * Strip field initializers, convert them to assignment statements, and
 * add them to the appropriate list of initialization statements.
 *//*from   w  ww.ja v a2s  .c om*/
private void addFieldInitializer(BodyDeclaration member, List<Statement> initStatements,
        List<Statement> classInitStatements) {
    FieldDeclaration field = (FieldDeclaration) member;
    @SuppressWarnings("unchecked")
    List<VariableDeclarationFragment> fragments = field.fragments(); // safe by specification
    for (VariableDeclarationFragment frag : fragments) {
        if (frag.getInitializer() != null) {
            Statement assignStmt = makeAssignmentStatement(frag);
            if (Modifier.isStatic(field.getModifiers())) {
                IVariableBinding binding = Types.getVariableBinding(frag);
                if (binding.getConstantValue() == null) { // constants don't need initialization
                    classInitStatements.add(assignStmt);
                    frag.setInitializer(null);
                }
            } else {
                // always initialize instance variables, since they can't be constants
                initStatements.add(assignStmt);
                frag.setInitializer(null);
            }
        }
    }
}

From source file:com.google.devtools.j2cpp.translate.Rewriter.java

License:Open Source License

@Override
public boolean visit(FieldDeclaration node) {
    int mods = node.getModifiers();
    if (Modifier.isStatic(mods)) {
        ASTNode parent = node.getParent();
        @SuppressWarnings("unchecked")
        List<BodyDeclaration> classMembers = parent instanceof AbstractTypeDeclaration
                ? ((AbstractTypeDeclaration) parent).bodyDeclarations()
                : ((AnonymousClassDeclaration) parent).bodyDeclarations(); // safe by specification
        int indexOfNewMember = classMembers.indexOf(node) + 1;

        @SuppressWarnings("unchecked")
        List<VariableDeclarationFragment> fragments = node.fragments(); // safe by specification
        for (VariableDeclarationFragment var : fragments) {
            IVariableBinding binding = Types.getVariableBinding(var);
            if (Types.isPrimitiveConstant(binding) && Modifier.isPrivate(binding.getModifiers())) {
                // Don't define accessors for private constants, since they can be
                // directly referenced.
                continue;
            }/*w  w  w  . j a  va  2s  . c om*/

            // rename varName to varName_, per Obj-C style guide
            SimpleName oldName = var.getName();
            ITypeBinding type = ((AbstractTypeDeclaration) node.getParent()).resolveBinding();
            String varName = NameTable.getStaticVarQualifiedName(type, oldName.getIdentifier());
            NameTable.rename(binding, varName);
            ITypeBinding typeBinding = binding.getType();
            var.setExtraDimensions(0); // if array, type was corrected above

            // add accessor(s)
            if (needsReader(var, classMembers)) {
                classMembers.add(indexOfNewMember++, makeStaticReader(var, mods));
            }
            if (!Modifier.isFinal(node.getModifiers()) && needsWriter(var, classMembers)) {
                classMembers.add(indexOfNewMember++,
                        makeStaticWriter(var, oldName.getIdentifier(), node.getType(), mods));
            }

            // move non-constant initialization to init block
            Expression initializer = var.getInitializer();
            if (initializer != null && initializer.resolveConstantExpressionValue() == null) {
                var.setInitializer(null);

                AST ast = var.getAST();
                SimpleName newName = ast.newSimpleName(varName);
                Types.addBinding(newName, binding);
                Assignment assign = ast.newAssignment();
                assign.setLeftHandSide(newName);
                Expression newInit = NodeCopier.copySubtree(ast, initializer);
                assign.setRightHandSide(newInit);
                Types.addBinding(assign, typeBinding);

                Block initBlock = ast.newBlock();
                @SuppressWarnings("unchecked")
                List<Statement> stmts = initBlock.statements(); // safe by definition
                stmts.add(ast.newExpressionStatement(assign));
                Initializer staticInitializer = ast.newInitializer();
                staticInitializer.setBody(initBlock);
                @SuppressWarnings("unchecked")
                List<IExtendedModifier> initMods = staticInitializer.modifiers(); // safe by definition
                initMods.add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
                classMembers.add(indexOfNewMember++, staticInitializer);
            }
        }
    }
    return true;
}

From source file:com.google.devtools.j2cpp.types.ImplementationImportCollector.java

License:Open Source License

@Override
public boolean visit(FieldDeclaration node) {
    // non-private references are gathered by the header file collector
    if (Modifier.isPrivate(node.getModifiers())) {
        addReference(node.getType());//from w ww  .  j  a  v  a  2s.  c  om
    }
    return super.visit(node);
}