Example usage for org.eclipse.jdt.core.dom EnumConstantDeclaration resolveVariable

List of usage examples for org.eclipse.jdt.core.dom EnumConstantDeclaration resolveVariable

Introduction

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

Prototype

public IVariableBinding resolveVariable() 

Source Link

Document

Resolves and returns the field binding for this enum constant.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.util.DOMFinder.java

License:Open Source License

public boolean visit(EnumConstantDeclaration node) {
    if (found(node, node.getName()) && this.resolveBinding)
        this.foundBinding = node.resolveVariable();
    return true;/*from   w  w  w .j av  a 2 s .c om*/
}

From source file:com.google.devtools.j2cpp.sym.SymbolTableBuilder.java

License:Open Source License

@Override
public boolean visit(EnumConstantDeclaration node) {
    addVariable(node, node.resolveVariable());
    return true;
}

From source file:com.ibm.wala.cast.java.translator.jdt.JDTJava2CAstTranslator.java

License:Open Source License

/**
 * Only called from createClassDeclaration.
 * //from   w  w w  . j av  a2s  .  c  om
 * @param decl
 * @param context
 * @return
 */
private CAstEntity visit(EnumConstantDeclaration decl, WalkContext context) {
    return new FieldEntity(decl.getName().getIdentifier(), decl.resolveVariable().getType(), enumQuals,
            makePosition(decl.getStartPosition(), decl.getStartPosition() + decl.getLength()), null);
}

From source file:com.ibm.wala.cast.java.translator.jdt.JDTJava2CAstTranslator.java

License:Open Source License

/**
 * Called only from visitFieldInitNode(node,context)
 *//*  w  ww  .  j a v a2 s .c  o  m*/
private CAstNode createEnumConstantDeclarationInit(EnumConstantDeclaration node, WalkContext context) {
    String hiddenVariableName = (String) node
            .getProperty("com.ibm.wala.cast.java.translator.jdt.fakeValuesDeclName");
    if (hiddenVariableName == null) {
        FieldReference fieldRef = fIdentityMapper.getFieldRef(node.resolveVariable());
        // We use null to indicate an OBJECT_REF to a static field
        CAstNode lhsNode = makeNode(context, fFactory, node, CAstNode.OBJECT_REF,
                makeNode(context, fFactory, null, CAstNode.VOID), fFactory.makeConstant(fieldRef));

        // CONSTRUCT ARGUMENTS & "new MyEnum(...)" statement
        ArrayList<Object> arguments = new ArrayList<Object>();
        arguments.add(fFactory.makeConstant(node.getName().getIdentifier())); // name of constant
        arguments.add(fFactory.makeConstant(node.resolveVariable().getVariableId())); // id
        arguments.addAll(node.arguments());
        CAstNode rhsNode = createClassInstanceCreation(node, arguments, node.resolveConstructorBinding(), null,
                node.getAnonymousClassDeclaration(), context);

        CAstNode assNode = makeNode(context, fFactory, node, CAstNode.ASSIGN, lhsNode, rhsNode);

        return assNode; // their naming, not mine
    } else {

        // String[] x = (new Direction[] {
        // NORTH, EAST, SOUTH, WEST, $VALUES, $VALUES$
        // });

        return null;
    }
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

/********************************************************************************/

void getTextRegions(String proj, String bid, String file, String cls, boolean pfx, boolean statics,
        boolean compunit, boolean imports, boolean pkgfg, boolean topdecls, boolean fields, boolean all,
        IvyXmlWriter xw) throws BedrockException {
    if (file == null) {
        file = getFileFromClass(proj, cls);
    }/*  www.  java 2 s  . com*/

    FileData fd = findFile(proj, file, bid, null);
    if (fd == null)
        throw new BedrockException("Can't find file " + file + " in " + proj);

    CompilationUnit cu = fd.getDefaultRoot(bid);
    if (cu == null)
        throw new BedrockException("Can't get compilation unit for " + file);

    List<?> typs = cu.types();
    AbstractTypeDeclaration atd = findTypeDecl(cls, typs);
    int start = 0;
    if (atd != null && atd != typs.get(0))
        start = cu.getExtendedStartPosition(atd);

    if (compunit) {
        xw.begin("RANGE");
        xw.field("PATH", file);
        xw.field("START", 0);
        int ln = fd.getLength();
        if (ln < 0) {
            File f = new File(file);
            ln = (int) f.length();
        }
        xw.field("END", ln);
        xw.end("RANGE");
    }

    if (pfx && atd != null) {
        int xpos = cu.getExtendedStartPosition(atd);
        int xlen = cu.getExtendedLength(atd);
        int spos = atd.getStartPosition();
        int len = atd.getLength();
        int epos = -1;
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            int apos = cu.getExtendedStartPosition(an);
            if (epos < 0 || epos >= apos)
                epos = apos - 1;
        }
        if (epos < 0) { // no body declarations
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", start);
            xw.field("END", xpos + xlen);
            xw.end("RANGE");
        } else {
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", start);
            xw.field("END", epos);
            xw.end("RANGE");
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", spos + len - 1);
            xw.field("END", xpos + xlen);
            xw.end("RANGE");
        }
    }

    if (pkgfg) {
        PackageDeclaration pkg = cu.getPackage();
        if (pkg != null) {
            outputRange(cu, pkg, file, xw);
        }
    }

    if (imports) {
        for (Iterator<?> it = cu.imports().iterator(); it.hasNext();) {
            ImportDeclaration id = (ImportDeclaration) it.next();
            outputRange(cu, id, file, xw);
        }
    }

    if (topdecls && atd != null) {
        int spos = atd.getStartPosition();
        int len = atd.getLength();
        int epos = -1;
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            int apos = cu.getExtendedStartPosition(an);
            if (epos < 0 || epos >= apos)
                epos = apos - 1;
        }
        if (epos < 0) { // no body declarations
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", spos);
            xw.field("END", spos + len);
            xw.end("RANGE");
        } else {
            xw.begin("RANGE");
            xw.field("PATH", file);
            xw.field("START", spos);
            xw.field("END", epos);
            xw.end("RANGE");
        }
    }

    if ((statics || all) && atd != null) {
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            if (an.getNodeType() == ASTNode.INITIALIZER) {
                outputRange(cu, an, file, xw);
            }
        }
    }

    if (fields && atd != null) {
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            switch (an.getNodeType()) {
            case ASTNode.FIELD_DECLARATION:
                outputRange(cu, an, file, xw);
                break;
            case ASTNode.ENUM_CONSTANT_DECLARATION:
                outputRange(cu, an, file, xw);
                break;
            }
        }
        if (atd instanceof EnumDeclaration) {
            for (Object o : ((EnumDeclaration) atd).enumConstants()) {
                ASTNode an = (ASTNode) o;
                switch (an.getNodeType()) {
                case ASTNode.FIELD_DECLARATION:
                    outputRange(cu, an, file, xw);
                    break;
                case ASTNode.ENUM_CONSTANT_DECLARATION:
                    outputRange(cu, an, file, xw);
                    break;
                }
            }
        }
    }

    if (all && atd != null) {
        for (Object o : atd.bodyDeclarations()) {
            ASTNode an = (ASTNode) o;
            IJavaElement elt = null;
            switch (an.getNodeType()) {
            case ASTNode.ANNOTATION_TYPE_DECLARATION:
            case ASTNode.ENUM_DECLARATION:
            case ASTNode.TYPE_DECLARATION:
                AbstractTypeDeclaration atdecl = (AbstractTypeDeclaration) an;
                ITypeBinding atbnd = atdecl.resolveBinding();
                if (atbnd != null)
                    elt = atbnd.getJavaElement();
                break;
            case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
                break;
            case ASTNode.ENUM_CONSTANT_DECLARATION:
                EnumConstantDeclaration ecdecl = (EnumConstantDeclaration) an;
                IVariableBinding ecbnd = ecdecl.resolveVariable();
                if (ecbnd != null)
                    elt = ecbnd.getJavaElement();
                break;
            case ASTNode.FIELD_DECLARATION:
                FieldDeclaration fdecl = (FieldDeclaration) an;
                for (Iterator<?> it = fdecl.fragments().iterator(); it.hasNext();) {
                    VariableDeclarationFragment vdf = (VariableDeclarationFragment) it.next();
                    IVariableBinding vbnd = vdf.resolveBinding();
                    if (vbnd != null) {
                        IJavaElement velt = vbnd.getJavaElement();
                        if (velt != null)
                            BedrockUtil.outputJavaElement(velt, xw);
                    }
                }
                break;
            case ASTNode.INITIALIZER:
                break;
            case ASTNode.METHOD_DECLARATION:
                MethodDeclaration mdecl = (MethodDeclaration) an;
                IMethodBinding mbnd = mdecl.resolveBinding();
                if (mbnd != null)
                    elt = mbnd.getJavaElement();
                break;
            default:
                break;
            }
            if (elt != null)
                BedrockUtil.outputJavaElement(elt, false, xw);
        }
    }
}

From source file:edu.cmu.cs.crystal.internal.WorkspaceUtilities.java

License:Open Source License

public boolean visit(EnumConstantDeclaration node) {
    addNewBinding(node.resolveVariable(), node);
    return true;
}

From source file:fr.labri.harmony.rta.junit.jdt.JDTVisitorRTA.java

License:Open Source License

public boolean visit(EnumConstantDeclaration fd) {
    if (fd.resolveVariable() != null) {
        if (fd.resolveVariable().getDeclaringClass() != null) {
            String field = fd.resolveVariable().getDeclaringClass().getQualifiedName() + "."
                    + fd.resolveVariable().getName();
            hashes.put(field, MD5Generator.md5(fd.toString()));
            //Put the field in the database
            currentJavaMethod = getJavaMethod(field);
            currentJavaMethod//from   w ww  . j  av a2s .  co  m
                    .setClassName(getJavaClass(fd.resolveVariable().getDeclaringClass().getQualifiedName()));
        }
    }
    return true;
}

From source file:lang.java.jdt.internal.BindingsResolver.java

License:Open Source License

public void resolveBindings(EnumConstantDeclaration node) {
    importBinding(node.resolveConstructorBinding());
    importBinding(node.resolveVariable());
}

From source file:org.eclipse.babel.tapiji.tools.java.ui.refactoring.Cal10nEnumRefactoringVisitor.java

License:Open Source License

/**
 * Modifies the enum file. It replaces the old key with the new one.
 *//*from  w ww . ja v a 2 s.  c om*/
@Override
public boolean visit(EnumConstantDeclaration node) {

    if (node.resolveVariable().getName().equals(oldKey)) {

        // ASTRewrite
        AST ast = enumCu.getAST();
        ASTRewrite rewriter = ASTRewrite.create(ast);

        EnumConstantDeclaration newDeclaration = ast.newEnumConstantDeclaration();

        SimpleName newSimpleName = ast.newSimpleName(newKey);
        newDeclaration.setName(newSimpleName);

        rewriter.replace(node, newDeclaration, null);

        try {
            TextEdit textEdit = rewriter.rewriteAST();
            if (textEdit.hasChildren()) { // if the compilation unit has
                                          // been
                                          // changed
                ICompilationUnit icu = (ICompilationUnit) enumCu.getJavaElement();
                icu.applyTextEdit(textEdit, null);
                icu.getBuffer().save(null, true);

                // protocol
                int startPos = node.getStartPosition();
                changeSet.add(icu.getPath().toPortableString() + ": line " + enumCu.getLineNumber(startPos));
            }
        } catch (Exception e) {
            Logger.logError(e);
        }
    }

    return false;
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public EnumConstantDeclaration convert(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration enumConstant) {
    checkCanceled();/*from  w w  w  .jav  a  2s. c o m*/
    EnumConstantDeclaration enumConstantDeclaration = new EnumConstantDeclaration(this.ast);
    final SimpleName typeName = new SimpleName(this.ast);
    typeName.internalSetIdentifier(new String(enumConstant.name));
    typeName.setSourceRange(enumConstant.sourceStart, enumConstant.sourceEnd - enumConstant.sourceStart + 1);
    enumConstantDeclaration.setName(typeName);
    int declarationSourceStart = enumConstant.declarationSourceStart;
    int declarationSourceEnd = enumConstant.declarationSourceEnd;
    final org.eclipse.jdt.internal.compiler.ast.Expression initialization = enumConstant.initialization;
    if (initialization != null) {
        if (initialization instanceof QualifiedAllocationExpression) {
            org.eclipse.jdt.internal.compiler.ast.TypeDeclaration anonymousType = ((QualifiedAllocationExpression) initialization).anonymousType;
            if (anonymousType != null) {
                AnonymousClassDeclaration anonymousClassDeclaration = new AnonymousClassDeclaration(this.ast);
                int start = retrieveStartBlockPosition(anonymousType.sourceEnd, anonymousType.bodyEnd);
                int end = retrieveRightBrace(anonymousType.bodyEnd, declarationSourceEnd);
                if (end == -1)
                    end = anonymousType.bodyEnd;
                anonymousClassDeclaration.setSourceRange(start, end - start + 1);
                enumConstantDeclaration.setAnonymousClassDeclaration(anonymousClassDeclaration);
                buildBodyDeclarations(anonymousType, anonymousClassDeclaration);
                if (this.resolveBindings) {
                    recordNodes(anonymousClassDeclaration, anonymousType);
                    anonymousClassDeclaration.resolveBinding();
                }
                enumConstantDeclaration.setSourceRange(declarationSourceStart,
                        end - declarationSourceStart + 1);
            }
        } else {
            enumConstantDeclaration.setSourceRange(declarationSourceStart,
                    declarationSourceEnd - declarationSourceStart + 1);
        }
        final org.eclipse.jdt.internal.compiler.ast.Expression[] arguments = ((org.eclipse.jdt.internal.compiler.ast.AllocationExpression) initialization).arguments;
        if (arguments != null) {
            for (int i = 0, max = arguments.length; i < max; i++) {
                enumConstantDeclaration.arguments().add(convert(arguments[i]));
            }
        }
    } else {
        enumConstantDeclaration.setSourceRange(declarationSourceStart,
                declarationSourceEnd - declarationSourceStart + 1);
    }
    setModifiers(enumConstantDeclaration, enumConstant);
    if (this.resolveBindings) {
        recordNodes(enumConstantDeclaration, enumConstant);
        recordNodes(typeName, enumConstant);
        enumConstantDeclaration.resolveVariable();
    }
    convert(enumConstant.javadoc, enumConstantDeclaration);
    return enumConstantDeclaration;
}