Example usage for org.eclipse.jdt.core.dom ClassInstanceCreation getAnonymousClassDeclaration

List of usage examples for org.eclipse.jdt.core.dom ClassInstanceCreation getAnonymousClassDeclaration

Introduction

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

Prototype

public AnonymousClassDeclaration getAnonymousClassDeclaration() 

Source Link

Document

Returns the anonymous class declaration introduced by this class instance creation expression, if it has one.

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(ClassInstanceCreation node) {
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
        this.fBuffer.append(".");//$NON-NLS-1$
    }/*from   w ww  .  ja  v  a 2 s . c  o  m*/
    this.fBuffer.append("new ");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= JLS3) {
        if (!node.typeArguments().isEmpty()) {
            this.fBuffer.append("<");//$NON-NLS-1$
            for (Iterator<Type> it = node.typeArguments().iterator(); it.hasNext();) {
                Type t = it.next();
                t.accept(this);
                if (it.hasNext()) {
                    this.fBuffer.append(",");//$NON-NLS-1$
                }
            }
            this.fBuffer.append(">");//$NON-NLS-1$
        }
        node.getType().accept(this);
    }
    this.fBuffer.append("(");//$NON-NLS-1$
    for (Iterator<Expression> it = node.arguments().iterator(); it.hasNext();) {
        Expression e = it.next();
        e.accept(this);
        if (it.hasNext()) {
            this.fBuffer.append(",");//$NON-NLS-1$
        }
    }
    this.fBuffer.append(")");//$NON-NLS-1$
    if (node.getAnonymousClassDeclaration() != null) {
        node.getAnonymousClassDeclaration().accept(this);
    }
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(ClassInstanceCreation node) {
    boa.types.Ast.Expression.Builder b = boa.types.Ast.Expression.newBuilder();
    //      b.setPosition(pos.build());
    b.setKind(boa.types.Ast.Expression.ExpressionKind.NEW);
    boa.types.Ast.Type.Builder tb = boa.types.Ast.Type.newBuilder();
    tb.setName(getIndex(typeName(node.getType())));
    tb.setKind(boa.types.Ast.TypeKind.CLASS);
    b.setNewType(tb.build());//from   w w  w  .j av  a2s .  c o m
    for (Object t : node.typeArguments()) {
        boa.types.Ast.Type.Builder gtb = boa.types.Ast.Type.newBuilder();
        gtb.setName(getIndex(typeName((org.eclipse.jdt.core.dom.Type) t)));
        gtb.setKind(boa.types.Ast.TypeKind.GENERIC);
        b.addGenericParameters(gtb.build());
    }
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
        b.addExpressions(expressions.pop());
    }
    for (Object a : node.arguments()) {
        ((org.eclipse.jdt.core.dom.Expression) a).accept(this);
        b.addExpressions(expressions.pop());
    }
    if (node.getAnonymousClassDeclaration() != null) {
        declarations.push(new ArrayList<boa.types.Ast.Declaration>());
        node.getAnonymousClassDeclaration().accept(this);
        for (boa.types.Ast.Declaration d : declarations.pop())
            b.setAnonDeclaration(d);
    }
    expressions.push(b.build());
    return false;
}

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(ClassInstanceCreation node) {
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
        this.buffer.append(".");//$NON-NLS-1$
    }//from   w  ww .j a  v  a2  s .c  om
    this.buffer.append("new ");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= AST.JLS3) {
        if (!node.typeArguments().isEmpty()) {
            this.buffer.append("<");//$NON-NLS-1$
            for (Iterator it = node.typeArguments().iterator(); it.hasNext();) {
                Type t = (Type) it.next();
                t.accept(this);
                if (it.hasNext()) {
                    this.buffer.append(",");//$NON-NLS-1$
                }
            }
            this.buffer.append(">");//$NON-NLS-1$
        }
        node.getType().accept(this);
    }
    this.buffer.append("(");//$NON-NLS-1$
    for (Iterator it = node.arguments().iterator(); it.hasNext();) {
        Expression e = (Expression) it.next();
        e.accept(this);
        if (it.hasNext()) {
            this.buffer.append(",");//$NON-NLS-1$
        }
    }
    this.buffer.append(")");//$NON-NLS-1$
    if (node.getAnonymousClassDeclaration() != null) {
        node.getAnonymousClassDeclaration().accept(this);
    }
    return false;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.WrapPreparator.java

License:Open Source License

@Override
public boolean visit(ClassInstanceCreation node) {
    AnonymousClassDeclaration anonymousClass = node.getAnonymousClassDeclaration();
    if (anonymousClass != null) {
        forceContinuousWrapping(anonymousClass, this.tm.firstIndexIn(node, TokenNamenew));
    }//w  w  w.  j av a  2s  .co m

    int wrappingOption = node.getExpression() != null
            ? this.options.alignment_for_arguments_in_qualified_allocation_expression
            : this.options.alignment_for_arguments_in_allocation_expression;
    handleArguments(node.arguments(), wrappingOption);
    return true;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.SpacePreparator.java

License:Open Source License

@Override
public boolean visit(ClassInstanceCreation node) {
    List<Type> typeArguments = node.typeArguments();
    handleTypeArguments(typeArguments);/*from  w  w w.j  a  va2s.co m*/
    handleInvocation(node, node.getType(), node.getAnonymousClassDeclaration());
    if (!typeArguments.isEmpty()) {
        handleTokenBefore(typeArguments.get(0), TokenNamenew, false, true); // fix for: new<Integer>A<String>()
    }
    handleCommas(node.arguments(), this.options.insert_space_before_comma_in_allocation_expression,
            this.options.insert_space_after_comma_in_allocation_expression);
    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;
    {/*from  w w  w . ja  v  a 2s .  co m*/
        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.devtools.j2cpp.translate.JavaToIOSMethodTranslator.java

License:Open Source License

@Override
public boolean visit(ClassInstanceCreation node) {
    // translate any embedded method invocations
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
    }/*  w  w  w.  ja va  2  s. com*/
    @SuppressWarnings("unchecked")
    List<Expression> args = node.arguments(); // safe by design
    for (Expression e : args) {
        e.accept(this);
    }
    if (node.getAnonymousClassDeclaration() != null) {
        node.getAnonymousClassDeclaration().accept(this);
    }

    IMethodBinding binding = Types.getMethodBinding(node);
    JavaMethod md = descriptions.get(binding);
    if (md != null) {
        String key = md.getKey();
        String value = methodMappings.get(key);
        if (value != null) {
            IOSMethod iosMethod = new IOSMethod(value, binding, binding.getDeclaringClass(), ast);
            IMethodBinding methodBinding = iosMethod.resolveBinding();
            MethodInvocation newInvocation = createMappedInvocation(iosMethod, binding, methodBinding);

            // Set parameters.
            @SuppressWarnings("unchecked")
            List<Expression> oldArgs = node.arguments(); // safe by definition
            @SuppressWarnings("unchecked")
            List<Expression> newArgs = newInvocation.arguments(); // safe by definition
            copyInvocationArguments(null, oldArgs, newArgs);

            Types.substitute(node, newInvocation);
            Types.addMappedIOSMethod(binding, iosMethod);
            Types.addMappedInvocation(node, iosMethod.resolveBinding());
        } else {
            J2ObjC.error(node, createMissingMethodMessage(binding));
        }
    }
    return false;
}

From source file:com.google.devtools.j2objc.ast.DebugASTPrinter.java

License:Apache License

@Override
public boolean visit(ClassInstanceCreation node) {
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
        sb.print('.');
    }//from w  w  w .  jav a2  s  . com
    sb.print("new ");
    printTypeParameters(node.getMethodBinding().getTypeParameters());
    node.getType().accept(this);
    sb.print("(");
    for (Iterator<Expression> it = node.getArguments().iterator(); it.hasNext();) {
        Expression e = it.next();
        e.accept(this);
        if (it.hasNext()) {
            sb.print(',');
        }
    }
    sb.print(')');
    if (node.getAnonymousClassDeclaration() != null) {
        node.getAnonymousClassDeclaration().accept(this);
    }
    return false;
}

From source file:com.google.devtools.j2objc.translate.AnonymousClassConverterTest.java

License:Open Source License

/**
 * Verify that an anonymous class is moved to the compilation unit's types list.
 *///from  ww  w .j av  a 2  s . c  o m
public void testAnonymousClassExtracted() {
    List<TypeDeclaration> types = translateClassBody(
            "Object test() { return new java.util.Enumeration<Object>() { "
                    + "public boolean hasMoreElements() { return false; } "
                    + "public Object nextElement() { return null; } }; }");
    assertEquals(2, types.size());

    TypeDeclaration type = types.get(1);
    assertEquals("$1", type.getName().getIdentifier());

    type = types.get(0);
    assertEquals("Test", type.getName().getIdentifier());
    MethodDeclaration testMethod = (MethodDeclaration) type.bodyDeclarations().get(0);
    ReturnStatement stmt = (ReturnStatement) testMethod.getBody().statements().get(0);
    ClassInstanceCreation expr = (ClassInstanceCreation) stmt.getExpression();
    assertNull(expr.getAnonymousClassDeclaration());
}

From source file:com.google.gdt.eclipse.designer.model.widgets.cell.ColumnInfo.java

License:Open Source License

@Override
protected void refresh_finish() throws Exception {
    super.refresh_finish();
    String cellTypeName = null;//w ww  .  j  a  v a2  s .  co  m
    // if anonymous, then it was mocked as TextColumn
    if (getCreationSupport().getNode() instanceof ClassInstanceCreation) {
        ClassInstanceCreation creation = (ClassInstanceCreation) getCreationSupport().getNode();
        if (creation.getAnonymousClassDeclaration() != null) {
            ITypeBinding anonymousBinding = AstNodeUtils.getTypeBinding(creation);
            ITypeBinding superBinding = anonymousBinding.getSuperclass();
            String superName = AstNodeUtils.getFullyQualifiedName(superBinding, false);
            if ("com.google.gwt.user.cellview.client.Column".equals(superName)) {
                Expression cellExpression = DomGenerics.arguments(creation).get(0);
                cellTypeName = AstNodeUtils.getFullyQualifiedName(cellExpression, false);
            }
        }
    }
    // get Cell from Column instance
    if (cellTypeName == null) {
        Object cell = ReflectionUtils.invokeMethod2(getObject(), "getCell");
        Class<?> cellClass = cell.getClass();
        cellTypeName = ReflectionUtils.getCanonicalName(cellClass);
    }
    // use icon which corresponds to the Cell type
    if (cellTypeName != null) {
        int index = ArrayUtils.indexOf(CELL_TYPES, cellTypeName);
        if (index != ArrayUtils.INDEX_NOT_FOUND) {
            m_specialIcon = getCreationIcon(CELL_IDS[index]);
        }
    }
}