Example usage for org.eclipse.jdt.core.dom IVariableBinding getJavaElement

List of usage examples for org.eclipse.jdt.core.dom IVariableBinding getJavaElement

Introduction

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

Prototype

public IJavaElement getJavaElement();

Source Link

Document

Returns the Java element that corresponds to this binding.

Usage

From source file:com.codenvy.ide.ext.java.server.JavadocFinder.java

License:Open Source License

private void addValue(StringBuffer buf, IJavaElement element, Object value) throws URISyntaxException {
    // Note: To be bug-compatible with Javadoc from Java 5/6/7, we currently don't escape HTML tags in String-valued annotations.
    if (value instanceof ITypeBinding) {
        ITypeBinding typeBinding = (ITypeBinding) value;
        IJavaElement type = typeBinding.getJavaElement();
        if (type == null) {
            buf.append(typeBinding.getName());
        } else {//from  ww  w . j a  v  a  2 s. c o m
            String uri = JavaElementLinks.createURI(baseHref, type);
            String name = type.getElementName();
            addLink(buf, uri, name);
        }
        buf.append(".class"); //$NON-NLS-1$

    } else if (value instanceof IVariableBinding) { // only enum constants
        IVariableBinding variableBinding = (IVariableBinding) value;
        IJavaElement variable = variableBinding.getJavaElement();
        String uri = JavaElementLinks.createURI(baseHref, variable);
        String name = variable.getElementName();
        addLink(buf, uri, name);

    } else if (value instanceof IAnnotationBinding) {
        IAnnotationBinding annotationBinding = (IAnnotationBinding) value;
        addAnnotation(buf, element, annotationBinding);

    } else if (value instanceof String) {
        buf.append(ASTNodes.getEscapedStringLiteral((String) value));

    } else if (value instanceof Character) {
        buf.append(ASTNodes.getEscapedCharacterLiteral((Character) value));

    } else if (value instanceof Object[]) {
        Object[] values = (Object[]) value;
        buf.append('{');
        for (int i = 0; i < values.length; i++) {
            if (i > 0) {
                buf.append(JavaElementLabels.COMMA_STRING);
            }
            addValue(buf, element, values[i]);
        }
        buf.append('}');

    } else { // primitive types (except char) or null
        buf.append(String.valueOf(value));
    }
}

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);
    }/*ww w .j a v a2  s.c  o  m*/

    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:org.eclipse.core.tools.search.FindUnusedMembers.java

License:Open Source License

public void doSearchType(ITypeBinding typeBinding, IProgressMonitor monitor) throws CoreException {
    IMethodBinding[] methods = typeBinding.getDeclaredMethods();
    IVariableBinding[] fields = typeBinding.getDeclaredFields();

    monitor.beginTask("Searching for references.", methods.length + fields.length); //$NON-NLS-1$

    try {/*w  w  w .  ja v a  2 s .  co m*/
        for (int i = 0; i < methods.length; i++) {
            if (monitor.isCanceled())
                throw new OperationCanceledException();

            IMethodBinding methodBinding = methods[i];
            if (methodOverrides(methodBinding))
                continue;

            IMethod method = (IMethod) methodBinding.getJavaElement();
            if (method == null)
                continue;

            if (hasReferences(method, new SubProgressMonitor(monitor, 1)))
                continue;
            result.unusedElementFound(method);
            unusedMemberCount++;
        }
        for (int i = 0; i < fields.length; i++) {
            if (monitor.isCanceled())
                throw new OperationCanceledException();

            IVariableBinding fieldBinding = fields[i];
            IField field = (IField) fieldBinding.getJavaElement();
            if (field == null)
                continue;
            if (hasReferences(field, new SubProgressMonitor(monitor, 1)))
                continue;
            result.unusedElementFound(field);
            unusedMemberCount++;
        }

        if (monitor.isCanceled())
            throw new OperationCanceledException();
    } finally {
        monitor.done();
    }
}

From source file:org.joe_e.eclipse.Taming.java

License:BSD License

boolean isAllowed(ITypeBinding classBinding, IVariableBinding fieldBinding) {
    if (Preferences.isTamingEnabled()) {
        Entry e = db.get((IType) classBinding.getJavaElement());
        IField field = (IField) fieldBinding.getJavaElement();
        return ((e != null) && e.allowedFields.containsKey(field));
    } else {/*from w ww.  j a v a 2 s . c  o  m*/
        return true;
    }
}

From source file:org.joe_e.eclipse.Taming.java

License:BSD License

/**
 * Get the taming comment (if any) for a disabled field
 * @param classBinding the class containing the disabled field
 * @param fieldBinding the disabled field
 * @return the taming comment for the disabled field, or <code>null</code>
 *         if none./*from   w w  w  .  ja v a  2  s.  co m*/
 */
String getTamingComment(ITypeBinding classBinding, IVariableBinding fieldBinding) {
    Entry e = db.get((IType) classBinding.getJavaElement());
    IField field = (IField) fieldBinding.getJavaElement();
    if (e == null || e.disabledFields == null) {
        return null;
    } else {
        return e.disabledFields.get(field);
    }
}