Example usage for org.eclipse.jdt.core.dom AbstractTypeDeclaration resolveBinding

List of usage examples for org.eclipse.jdt.core.dom AbstractTypeDeclaration resolveBinding

Introduction

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

Prototype

public final ITypeBinding resolveBinding() 

Source Link

Document

Resolves and returns the binding for the type declared in this type declaration.

Usage

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

License:Open Source License

@Override
public boolean visit(final PackageDeclaration pNode) {
    final IPackageBinding binding = pNode.resolveBinding();
    if (ASTCrawler.checkForNull(binding))
        return false;
    final IElement packageElem = this.convertBinding(binding);

    this.aDB.addElement(packageElem, binding.getModifiers());

    final CompilationUnit parent = (CompilationUnit) pNode.getParent();
    @SuppressWarnings("rawtypes")
    final List containedTypes = parent.types();
    for (@SuppressWarnings("rawtypes")
    final Iterator it = containedTypes.iterator(); it.hasNext();) {
        final AbstractTypeDeclaration type = (AbstractTypeDeclaration) it.next();
        final ITypeBinding typeBinding = type.resolveBinding();
        final IElement typeElem = ASTCrawler.convertBinding(typeBinding);
        this.aDB.addElement(typeElem, typeBinding.getModifiers());
        this.aDB.addRelation(packageElem, Relation.CONTAINS, typeElem);
    }//w  w w.j  a  v a  2  s. c  o m

    return true;
}

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

License:Open Source License

private CAstEntity visitTypeDecl(AbstractTypeDeclaration n, WalkContext context) {
    return createClassDeclaration(n, n.bodyDeclarations(), null, n.resolveBinding(),
            n.getName().getIdentifier(), n.getModifiers(), isInterface(n),
            n instanceof AnnotationTypeDeclaration, context);
}

From source file:de.jevopi.j2og.simpleParser.Visitor.java

License:Open Source License

protected boolean startType(AbstractTypeDeclaration i_node) {
    ITypeBinding binding = i_node.resolveBinding();
    if (binding != null) {

        Type type = getType(binding, false);
        classifierStack.push(type);//from  w  w w  .  ja  v  a  2  s.  com

        if (log.isLoggable(Level.INFO)) {
            log.info("push " + type); //$NON-NLS-1$
        }

        for (ITypeBinding _interface : binding.getInterfaces()) {
            type.addInterface((Interface) getType(_interface.getErasure(), true));
        }

        if (!binding.isInterface()) {
            ITypeBinding _superBinding = binding.getSuperclass();
            if (_superBinding != null) {
                ((Class) type).setSuper((Class) getType(_superBinding.getErasure(), true));
            }
        }

        return true;
    } else {
        return false;
    }
}

From source file:de.jevopi.j2og.simpleParser.Visitor.java

License:Open Source License

protected void endType(AbstractTypeDeclaration i_node) {
    ITypeBinding binding = i_node.resolveBinding();
    if (binding != null) {
        Type t = classifierStack.pop();

        if (log.isLoggable(Level.INFO)) {
            log.info("pop " + t); //$NON-NLS-1$
        }/*from  w  w w .j  a v  a2  s. co m*/
    }
}

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);
    }//from   w  w w .  j  ava2s .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.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

private AbstractTypeDeclaration findTypeDecl(String cls, List<?> typs) {
    AbstractTypeDeclaration atd = null;/* w  w w  .java 2  s.  com*/
    for (int i = 0; atd == null && i < typs.size(); ++i) {
        if (!(typs.get(i) instanceof AbstractTypeDeclaration))
            continue;
        AbstractTypeDeclaration d = (AbstractTypeDeclaration) typs.get(i);
        if (cls != null) {
            ITypeBinding tb = d.resolveBinding();
            if (tb != null && !cls.equals(tb.getQualifiedName())) {
                if (cls.startsWith(tb.getQualifiedName() + ".")) {
                    atd = findTypeDecl(cls, d.bodyDeclarations());
                }
                continue;
            }
        }
        atd = d;
    }

    return atd;
}

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

License:Open Source License

public void resolveBindings(AbstractTypeDeclaration node) {
    importBinding(node.resolveBinding());
}

From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java

License:Open Source License

public boolean visitWith(FieldDeclaration node, boolean ignoreInitializer) {
    if ((node.getModifiers() & Modifier.STATIC) != 0) {
        return false;
    }/*  w  w w .j av a 2  s .c  om*/
    ASTNode xparent = node.getParent();
    while (xparent != null && !(xparent instanceof AbstractTypeDeclaration)
            && !(xparent instanceof AnonymousClassDeclaration)) {
        xparent = xparent.getParent();
    }
    ITypeBinding typeBinding = null;
    //ITypeBinding anonBinding = null;
    if (xparent != null) {
        if (xparent instanceof AbstractTypeDeclaration) {
            AbstractTypeDeclaration type = (AbstractTypeDeclaration) xparent;
            typeBinding = type.resolveBinding();
        } else if (xparent instanceof AnonymousClassDeclaration) {
            AnonymousClassDeclaration type = (AnonymousClassDeclaration) xparent;
            typeBinding = type.resolveBinding();//.getSuperclass();
        }
    }

    List fragments = node.fragments();
    for (Iterator iter = fragments.iterator(); iter.hasNext();) {
        VariableDeclarationFragment element = (VariableDeclarationFragment) iter.next();
        String fieldName = getJ2SName(element.getName());
        //         String fieldName = element.getName().getIdentifier();
        String ext = "";
        if (checkKeyworkViolation(fieldName)) {
            ext += "$";
        }
        if (typeBinding != null && checkSameName(typeBinding, fieldName)) {
            ext += "$";
        }
        //fieldName = ext + fieldName;
        //buffer.append(fieldName);
        buffer.append("this.");
        if (isInheritedFieldName(typeBinding, fieldName)) {
            fieldName = getFieldName(typeBinding, fieldName);
            buffer.append(ext + fieldName);
        } else {
            buffer.append(ext + fieldName);
        }
        //buffer.append(element.getName());
        buffer.append(" = ");
        if (!ignoreInitializer && element.getInitializer() != null) {
            element.getInitializer().accept(this);
        } else {
            boolean isArray = false;
            List frags = node.fragments();
            if (frags.size() > 0) {
                VariableDeclarationFragment varFrag = (VariableDeclarationFragment) frags.get(0);
                IVariableBinding resolveBinding = varFrag.resolveBinding();
                if (resolveBinding != null) {
                    isArray = resolveBinding.getType().isArray();
                    if (isArray) {
                        buffer.append("null");
                    }
                }
            }
            if (!isArray) {
                if (node.getType().isPrimitiveType()) {
                    PrimitiveType pType = (PrimitiveType) node.getType();
                    if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) {
                        buffer.append("false");
                    } else if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) {
                        buffer.append("'\\0'");
                    } else {
                        buffer.append("0");
                    }
                } else {
                    buffer.append("null");
                }
            }
        }
        buffer.append(";\r\n");
    }
    return false;
}

From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java

License:Open Source License

private void appendFieldName(ASTNode parent, ITypeBinding declaringClass) {
    String name = declaringClass.getQualifiedName();
    boolean isThis = false;
    int superLevel = 0;
    while (parent != null) {
        if (parent instanceof AbstractTypeDeclaration) {
            AbstractTypeDeclaration type = (AbstractTypeDeclaration) parent;
            ITypeBinding typeBinding = type.resolveBinding();
            superLevel++;/*from www.  j  a va2  s  . c  o  m*/
            if (Bindings.isSuperType(declaringClass, typeBinding)) {
                if (superLevel == 1) {
                    buffer.append("this.");
                    isThis = true;
                } else {
                    name = typeBinding.getQualifiedName();
                }
                break;
            }
        } else if (parent instanceof AnonymousClassDeclaration) {
            AnonymousClassDeclaration type = (AnonymousClassDeclaration) parent;
            ITypeBinding typeBinding = type.resolveBinding();
            superLevel++;
            if (Bindings.isSuperType(declaringClass, typeBinding)) {
                if (superLevel == 1) {
                    buffer.append("this.");
                    isThis = true;
                } else {
                    name = typeBinding.getQualifiedName();
                    if ((name == null || name.length() == 0) && typeBinding.isLocal()) {
                        name = typeBinding.getBinaryName();
                        int idx0 = name.lastIndexOf(".");
                        if (idx0 == -1) {
                            idx0 = 0;
                        }
                        int idx1 = name.indexOf('$', idx0);
                        if (idx1 != -1) {
                            int idx2 = name.indexOf('$', idx1 + 1);
                            String parentAnon = "";
                            if (idx2 == -1) { // maybe the name is already "$1$2..." for Java5.0+ in Eclipse 3.2+
                                parent = parent.getParent();
                                while (parent != null) {
                                    if (parent instanceof AbstractTypeDeclaration) {
                                        break;
                                    } else if (parent instanceof AnonymousClassDeclaration) {
                                        AnonymousClassDeclaration atype = (AnonymousClassDeclaration) parent;
                                        ITypeBinding aTypeBinding = atype.resolveBinding();
                                        String aName = aTypeBinding.getBinaryName();
                                        parentAnon = aName.substring(aName.indexOf('$')) + parentAnon;
                                    }
                                    parent = parent.getParent();
                                }
                                name = name.substring(0, idx1) + parentAnon + name.substring(idx1);
                            }
                        }
                    }
                }
                break;
            }
        }
        parent = parent.getParent();
    }
    if (!isThis) {
        buffer.append("this.callbacks[\"");
        buffer.append(shortenQualifiedName(name));
        buffer.append("\"].");
    }
}

From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java

License:Open Source License

public boolean visit(SuperFieldAccess node) {
    ASTNode xparent = node.getParent();/*from  ww w.  ja va  2  s.  co m*/
    while (xparent != null && !(xparent instanceof AbstractTypeDeclaration)
            && !(xparent instanceof AnonymousClassDeclaration)) {
        xparent = xparent.getParent();
    }
    ITypeBinding typeBinding = null;
    if (xparent != null) {
        if (xparent instanceof AbstractTypeDeclaration) {
            AbstractTypeDeclaration type = (AbstractTypeDeclaration) xparent;
            typeBinding = type.resolveBinding();
        } else if (xparent instanceof AnonymousClassDeclaration) {
            AnonymousClassDeclaration type = (AnonymousClassDeclaration) xparent;
            typeBinding = type.resolveBinding().getSuperclass();
        }
    }
    String fieldName = getJ2SName(node.getName());
    if (isInheritedFieldName(typeBinding, fieldName)) {
        if (typeBinding != null) {
            IVariableBinding[] declaredFields = typeBinding.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                String superFieldName = getJ2SName(declaredFields[i]);
                if (fieldName.equals(superFieldName)) {
                    buffer.append("this.");
                    if (checkKeyworkViolation(fieldName)) {
                        buffer.append('$');
                    }
                    fieldName = getFieldName(typeBinding.getSuperclass(), fieldName);
                    buffer.append(fieldName);
                    return false;
                }
            }
        }
    }
    buffer.append("this.");
    if (checkKeyworkViolation(fieldName)) {
        buffer.append('$');
    }
    buffer.append(fieldName);

    return false;
}