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

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

Introduction

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

Prototype

public ITypeBinding resolveBinding() 

Source Link

Document

Resolves and returns the binding for the anonymous class declared in this declaration.

Usage

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

License:Open Source License

@Override
public boolean visit(final AnonymousClassDeclaration pNode) {
    final ITypeBinding lBinding = pNode.resolveBinding();

    if (ASTCrawler.checkForNull(lBinding))
        return false;
    if (ASTCrawler.checkForNull(this.aCurrType))
        return false;

    final IElement lAnonymousClass = ASTCrawler.convertBinding(lBinding);
    this.aCurrTypeReminder.push(this.aCurrType);
    this.aCurrType = (ClassElement) lAnonymousClass;
    this.aDB.addElement(this.aCurrType, pNode.resolveBinding().getModifiers());
    this.aDB.addRelation(this.aCurrMethod, Relation.DECLARES_TYPE, this.aCurrType);

    final ITypeBinding lSuperBinding = lBinding.getSuperclass();
    if (lSuperBinding != null) {
        final IElement lSuperClass = ASTCrawler.convertBinding(lSuperBinding);
        this.aDB.addElement(lSuperClass, lSuperBinding.getModifiers());
        this.aDB.addRelationAndTranspose(this.aCurrType, Relation.EXTENDS_CLASS, lSuperClass);
    }/* w w  w. j  a v a2 s. com*/

    final ITypeBinding lInterfaceBindings[] = lBinding.getInterfaces();
    for (final ITypeBinding element : lInterfaceBindings) {
        final IElement lInterface = ASTCrawler.convertBinding(element);
        this.aDB.addElement(lInterface, element.getModifiers() | ASTCrawler.ABSTRACT_FLAG);
        this.aDB.addRelationAndTranspose(this.aCurrType, Relation.IMPLEMENTS_INTERFACE, lInterface);
    }
    return true;
}

From source file:changetypes.ASTVisitorAtomicChange.java

License:Open Source License

public boolean visit(AnonymousClassDeclaration node) {
    ITypeBinding itb = node.resolveBinding();
    this.itbStack.push(itb);
    try {/*w  w w . j av a2s.c  om*/
        this.facts.add(Fact.makeTypeFact(getQualifiedName(itb), getSimpleName(itb), itb.getPackage().getName(),
                itb.isInterface() ? "interface" : "class"));
    } catch (Exception localException1) {
        System.err.println("Cannot resolve bindings for anonymous class " + itb.getName());
    }
    Object localObject;
    int j;
    int i;
    try {
        try {
            this.facts.add(Fact.makeSubtypeFact(getQualifiedName(itb.getSuperclass()), getQualifiedName(itb)));
            this.facts.add(Fact.makeExtendsFact(getQualifiedName(itb.getSuperclass()), getQualifiedName(itb)));
        } catch (NullPointerException localNullPointerException1) {
            return false;
        }
        j = (localObject = itb.getInterfaces()).length;
        for (i = 0; i < j; i++) {
            ITypeBinding i2 = localObject[i];
            try {
                this.facts.add(Fact.makeSubtypeFact(getQualifiedName(i2), getQualifiedName(itb)));
                this.facts.add(Fact.makeImplementsFact(getQualifiedName(i2), getQualifiedName(itb)));
            } catch (NullPointerException localNullPointerException2) {
                return false;
            }
        }
    } catch (Exception localException2) {
        System.err.println("Cannot resolve super class bindings for anonymous class " + itb.getName());
    }
    try {
        j = (localObject = itb.getDeclaredFields()).length;
        for (i = 0; i < j; i++) {
            IVariableBinding ivb = localObject[i];
            String visibility = getModifier(ivb);
            this.facts.add(Fact.makeFieldFact(getQualifiedName(ivb), ivb.getName(), getQualifiedName(itb),
                    visibility));
            this.facts.add(Fact.makeFieldTypeFact(getQualifiedName(ivb), getQualifiedName(ivb.getType())));
        }
    } catch (Exception localException3) {
        System.err.println("Cannot resolve field bindings for anonymous class " + itb.getName());
    }
    try {
        if (itb.isNested()) {
            this.facts.add(
                    Fact.makeTypeInTypeFact(getQualifiedName(itb), getQualifiedName(itb.getDeclaringClass())));
        }
    } catch (Exception localException4) {
        System.err.println("Cannot resolve inner type for anonymous class " + itb.getName());
    }
    return true;
}

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

License:Open Source License

public boolean visit(AnonymousClassDeclaration anonymousClassDeclaration) {
    typeBinding = anonymousClassDeclaration.resolveBinding();
    return false;
}

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

License:Open Source License

public boolean visit(AnonymousClassDeclaration node) {
    ASTNode name;//  www .  j ava  2s  .  com
    ASTNode parent = node.getParent();
    switch (parent.getNodeType()) {
    case ASTNode.CLASS_INSTANCE_CREATION:
        name = ((ClassInstanceCreation) parent).getType();
        if (name.getNodeType() == ASTNode.PARAMETERIZED_TYPE) {
            name = ((ParameterizedType) name).getType();
        }
        break;
    case ASTNode.ENUM_CONSTANT_DECLARATION:
        name = ((EnumConstantDeclaration) parent).getName();
        break;
    default:
        return true;
    }
    if (found(node, name) && this.resolveBinding)
        this.foundBinding = node.resolveBinding();
    return true;
}

From source file:com.google.dart.java2dart.SyntaxTranslator.java

License:Open Source License

@Override
public boolean visit(org.eclipse.jdt.core.dom.ClassInstanceCreation node) {
    IMethodBinding binding = node.resolveConstructorBinding();
    String signature = JavaUtils.getJdtSignature(binding);
    TypeName typeNameNode = (TypeName) translate(node.getType());
    final List<Expression> arguments = translateArguments(binding, node.arguments());
    final ClassDeclaration innerClass;
    {/*w ww.  ja va2  s .  com*/
        AnonymousClassDeclaration anoDeclaration = node.getAnonymousClassDeclaration();
        if (anoDeclaration != null) {
            ITypeBinding superclass = anoDeclaration.resolveBinding().getSuperclass();
            signature = superclass.getKey() + StringUtils.substringAfter(signature, ";");
            String name = typeNameNode.getName().getName().replace('.', '_');
            name = name + "_" + context.generateTechnicalAnonymousClassIndex();
            innerClass = declareInnerClass(binding, anoDeclaration, name, ArrayUtils.EMPTY_STRING_ARRAY);
            typeNameNode = typeName(name);
            // prepare enclosing type
            final ITypeBinding enclosingTypeBinding = getEnclosingTypeBinding(node);
            final SimpleIdentifier enclosingTypeRef;
            final AtomicBoolean addEnclosingTypeRef = new AtomicBoolean();
            {
                if (enclosingTypeBinding != null) {
                    enclosingTypeRef = identifier(enclosingTypeBinding.getName() + "_this");
                    // add enclosing class references
                    innerClass.accept(new RecursiveASTVisitor<Void>() {
                        @Override
                        public Void visitMethodInvocation(MethodInvocation node) {
                            if (node.getTarget() == null) {
                                IMethodBinding methodBinding = (IMethodBinding) context.getNodeBinding(node);
                                if (methodBinding != null
                                        && methodBinding.getDeclaringClass() == enclosingTypeBinding) {
                                    addEnclosingTypeRef.set(true);
                                    node.setTarget(enclosingTypeRef);
                                }
                            }
                            return super.visitMethodInvocation(node);
                        }

                        @Override
                        public Void visitSimpleIdentifier(SimpleIdentifier node) {
                            if (!(node.getParent() instanceof PropertyAccess)
                                    && !(node.getParent() instanceof PrefixedIdentifier)) {
                                Object binding = context.getNodeBinding(node);
                                if (binding instanceof IVariableBinding) {
                                    IVariableBinding variableBinding = (IVariableBinding) binding;
                                    if (variableBinding.isField()
                                            && variableBinding.getDeclaringClass() == enclosingTypeBinding) {
                                        addEnclosingTypeRef.set(true);
                                        replaceNode(node.getParent(), node,
                                                propertyAccess(enclosingTypeRef, node));
                                    }
                                }
                            }
                            return super.visitSimpleIdentifier(node);
                        }
                    });
                } else {
                    enclosingTypeRef = null;
                }
            }
            // declare referenced final variables XXX
            final String finalName = name;
            anoDeclaration.accept(new ASTVisitor() {
                final Set<org.eclipse.jdt.core.dom.IVariableBinding> addedParameters = Sets.newHashSet();
                final List<FormalParameter> constructorParameters = Lists.newArrayList();
                int index;

                @Override
                public void endVisit(AnonymousClassDeclaration node) {
                    if (!constructorParameters.isEmpty()) {
                        // add parameters to the existing "inner" constructor XXX
                        for (ClassMember classMember : innerClass.getMembers()) {
                            if (classMember instanceof ConstructorDeclaration) {
                                ConstructorDeclaration innerConstructor = (ConstructorDeclaration) classMember;
                                innerConstructor.getParameters().getParameters().addAll(constructorParameters);
                                return;
                            }
                        }
                        // create new "inner" constructor
                        innerClass.getMembers().add(index, constructorDeclaration(identifier(finalName), null,
                                formalParameterList(constructorParameters), null));
                    }
                    super.endVisit(node);
                }

                @Override
                public void endVisit(SimpleName node) {
                    IBinding nameBinding = node.resolveBinding();
                    if (nameBinding instanceof org.eclipse.jdt.core.dom.IVariableBinding) {
                        org.eclipse.jdt.core.dom.IVariableBinding variableBinding = (org.eclipse.jdt.core.dom.IVariableBinding) nameBinding;
                        org.eclipse.jdt.core.dom.MethodDeclaration enclosingMethod = getEnclosingMethod(node);
                        if (!variableBinding.isField() && enclosingMethod != null
                                && variableBinding.getDeclaringMethod() != enclosingMethod.resolveBinding()
                                && addedParameters.add(variableBinding)) {
                            TypeName parameterTypeName = translateTypeName(variableBinding.getType());
                            String parameterName = variableBinding.getName();
                            SimpleIdentifier parameterNameNode = identifier(parameterName);
                            innerClass.getMembers().add(index++, fieldDeclaration(parameterTypeName,
                                    variableDeclaration(parameterNameNode)));
                            constructorParameters.add(fieldFormalParameter(null, null, parameterNameNode));
                            arguments.add(parameterNameNode);
                            context.putReference(parameterNameNode, variableBinding, null);
                        }
                    }
                    super.endVisit(node);
                }

                @Override
                public boolean visit(AnonymousClassDeclaration node) {
                    if (addEnclosingTypeRef.get()) {
                        TypeName parameterTypeName = translateTypeName(enclosingTypeBinding);
                        innerClass.getMembers().add(index++, fieldDeclaration(false, Keyword.FINAL,
                                parameterTypeName, variableDeclaration(enclosingTypeRef)));
                        constructorParameters.add(fieldFormalParameter(null, null, enclosingTypeRef));
                        arguments.add(thisExpression());
                    }
                    return super.visit(node);
                }
            });
        } else {
            innerClass = null;
        }
    }
    InstanceCreationExpression creation = instanceCreationExpression(Keyword.NEW, typeNameNode, null,
            arguments);
    context.putNodeBinding(creation, binding);
    context.putAnonymousDeclaration(creation, innerClass);
    context.getConstructorDescription(binding).instanceCreations.add(creation);
    return done(creation);
}

From source file:com.google.dart.java2dart.SyntaxTranslator.java

License:Open Source License

private ClassDeclaration declareInnerClass(IMethodBinding constructorBinding,
        AnonymousClassDeclaration anoClassDeclaration, String innerClassName, String[] additionalParameters) {
    ITypeBinding superTypeBinding = anoClassDeclaration.resolveBinding().getSuperclass();
    ExtendsClause extendsClause = null;/*w  ww. ja  v  a 2  s.co m*/
    ImplementsClause implementsClause = null;
    {
        ITypeBinding[] superInterfaces = anoClassDeclaration.resolveBinding().getInterfaces();
        if (superInterfaces.length != 0) {
            superTypeBinding = superInterfaces[0];
            TypeName superType = typeName(superInterfaces[0].getName());
            putReference(superTypeBinding, (SimpleIdentifier) superType.getName());
            implementsClause = implementsClause(superType);
        } else {
            TypeName superType = translateTypeName(superTypeBinding);
            putReference(superTypeBinding, (SimpleIdentifier) superType.getName());
            extendsClause = extendsClause(superType);
        }
    }
    ClassDeclaration innerClass = classDeclaration(null, identifier(innerClassName), extendsClause, null,
            implementsClause, null);
    artificialUnitDeclarations.add(innerClass);
    if (extendsClause != null) {
        List<FormalParameter> parameters = Lists.newArrayList();
        List<Expression> arguments = Lists.newArrayList();
        // find "super" constructor
        IMethodBinding superConstructor = null;
        for (IMethodBinding superMethod : superTypeBinding.getDeclaredMethods()) {
            if (superMethod.isConstructor()) {
                if (isSuperConstructor(superMethod, constructorBinding)) {
                    superConstructor = superMethod;
                    // additional parameters
                    for (int i = 0; i < additionalParameters.length / 2; i++) {
                        parameters.add(simpleFormalParameter(null, typeName(additionalParameters[2 * i + 0]),
                                additionalParameters[2 * i + 1]));
                        arguments.add(identifier(additionalParameters[2 * i + 1]));
                    }
                    // "declared" parameters
                    ITypeBinding[] parameterTypes = superMethod.getParameterTypes();
                    for (int i = 0; i < parameterTypes.length; i++) {
                        TypeName dartParameterType = typeName(parameterTypes[i].getName());
                        String parameterName = "arg" + i;
                        parameters.add(simpleFormalParameter(dartParameterType, parameterName));
                        arguments.add(identifier(parameterName));
                    }
                    // done, we found and processed "super" constructor
                    break;
                }
            }
        }
        // declare "inner" constructor
        FormalParameterList parameterList = formalParameterList(parameters);
        ArgumentList argList = new ArgumentList(null, arguments, null);
        SuperConstructorInvocation superCI = new SuperConstructorInvocation(null, null, null, argList);
        context.getConstructorDescription(superConstructor).superInvocations.add(superCI);
        ConstructorDeclaration innerConstructor = constructorDeclaration(null, null, identifier(innerClassName),
                null, parameterList, ImmutableList.<ConstructorInitializer>of(superCI), emptyFunctionBody());
        innerClass.getMembers().add(innerConstructor);
    }
    for (Object javaBodyDeclaration : anoClassDeclaration.bodyDeclarations()) {
        ClassMember classMember = translate((org.eclipse.jdt.core.dom.ASTNode) javaBodyDeclaration);
        innerClass.getMembers().add(classMember);
    }
    return innerClass;
}

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

License:Open Source License

@Override
public void endVisit(AnonymousClassDeclaration node) {
    ITypeBinding binding = node.resolveBinding();
    @SuppressWarnings("unchecked")
    List<BodyDeclaration> body = node.bodyDeclarations();
    eliminateDeadCode(binding, body);/*from ww  w  .  j a va  2  s  .c  om*/
    generateMissingMethods(node.getAST(), binding, body);
    finishElimination();
}

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

License:Open Source License

@Override
public boolean visit(AnonymousClassDeclaration node) {
    put(node, node.resolveBinding());
    return true;
}

From source file:com.google.devtools.j2objc.jdt.TreeConverter.java

License:Apache License

private static TypeDeclaration convertAnonymousClassDeclaration(
        org.eclipse.jdt.core.dom.AnonymousClassDeclaration node, JdtExecutableElement constructorElem,
        IMethodBinding constructorBinding) {
    TypeDeclaration typeDecl = (TypeDeclaration) new TypeDeclaration(
            (TypeElement) BindingConverter.getElement(node.resolveBinding()))
                    .addBodyDeclaration(createAnonymousConstructor(constructorElem, constructorBinding))
                    .setPosition(getPosition(node));
    for (Object bodyDecl : node.bodyDeclarations()) {
        typeDecl.addBodyDeclaration((BodyDeclaration) convert(bodyDecl));
    }//from  w ww . ja  va 2 s . co  m
    return typeDecl;
}

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

License:Open Source License

private CAstEntity visit(AnonymousClassDeclaration n, WalkContext context) {
    return createClassDeclaration(n, n.bodyDeclarations(), null, n.resolveBinding(),
            JDT2CAstUtils.anonTypeName(n.resolveBinding()), 0 /* no modifiers */, false, false, context);
}