Search in sources :

Example 1 with HierarchicalASTVisitor

use of org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor in project che by eclipse.

the class CallInliner method crossCheckArguments.

/**
     * Checks whether arguments are passed to the method which do some assignments
     * inside the expression. If so these arguments can't be inlined into the
     * calling method since the assignments might be reorder. An example is:
     * <code>
     *   add((field=args).length,field.hashCode());
     * </code>
     * Field might not be initialized when the arguments are reorder in the called
     * method.
     * @param arguments the arguments
     * @return all arguments that cannot be inlined
     */
private Set<Expression> crossCheckArguments(List<Expression> arguments) {
    final Set<IBinding> assigned = new HashSet<IBinding>();
    final Set<Expression> result = new HashSet<Expression>();
    for (Iterator<Expression> iter = arguments.iterator(); iter.hasNext(); ) {
        final Expression expression = iter.next();
        expression.accept(new ASTVisitor() {

            @Override
            public boolean visit(Assignment node) {
                Expression lhs = node.getLeftHandSide();
                if (lhs instanceof Name) {
                    IBinding binding = ((Name) lhs).resolveBinding();
                    if (binding instanceof IVariableBinding) {
                        assigned.add(binding);
                        result.add(expression);
                    }
                }
                return true;
            }
        });
    }
    for (Iterator<Expression> iter = arguments.iterator(); iter.hasNext(); ) {
        final Expression expression = iter.next();
        if (!result.contains(expression)) {
            expression.accept(new HierarchicalASTVisitor() {

                @Override
                public boolean visit(Name node) {
                    IBinding binding = node.resolveBinding();
                    if (binding != null && assigned.contains(binding))
                        result.add(expression);
                    return false;
                }
            });
        }
    }
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) HierarchicalASTVisitor(org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor) HashSet(java.util.HashSet) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) HierarchicalASTVisitor(org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 2 with HierarchicalASTVisitor

use of org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor in project che by eclipse.

the class TypeContextChecker method fillWithTypeStubs.

private static void fillWithTypeStubs(final StringBuffer bufBefore, final StringBuffer bufAfter, final int focalPosition, List<? extends BodyDeclaration> types) {
    StringBuffer buf;
    for (Iterator<? extends BodyDeclaration> iter = types.iterator(); iter.hasNext(); ) {
        BodyDeclaration bodyDeclaration = iter.next();
        if (!(bodyDeclaration instanceof AbstractTypeDeclaration)) {
            //account for local classes:
            if (!(bodyDeclaration instanceof MethodDeclaration))
                continue;
            int bodyStart = bodyDeclaration.getStartPosition();
            int bodyEnd = bodyDeclaration.getStartPosition() + bodyDeclaration.getLength();
            if (!(bodyStart < focalPosition && focalPosition < bodyEnd))
                continue;
            MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
            buf = bufBefore;
            appendModifiers(buf, methodDeclaration.modifiers());
            appendTypeParameters(buf, methodDeclaration.typeParameters());
            //$NON-NLS-1$
            buf.append(" void ");
            buf.append(methodDeclaration.getName().getIdentifier());
            //$NON-NLS-1$
            buf.append("(){\n");
            Block body = methodDeclaration.getBody();
            body.accept(new HierarchicalASTVisitor() {

                @Override
                public boolean visit(AbstractTypeDeclaration node) {
                    fillWithTypeStubs(bufBefore, bufAfter, focalPosition, Collections.singletonList(node));
                    return false;
                }

                @Override
                public boolean visit(ClassInstanceCreation node) {
                    AnonymousClassDeclaration anonDecl = node.getAnonymousClassDeclaration();
                    if (anonDecl == null)
                        // could be in CIC parameter list
                        return true;
                    int anonStart = anonDecl.getStartPosition();
                    int anonEnd = anonDecl.getStartPosition() + anonDecl.getLength();
                    if (!(anonStart < focalPosition && focalPosition < anonEnd))
                        return false;
                    //$NON-NLS-1$
                    bufBefore.append(" new ");
                    bufBefore.append(node.getType().toString());
                    //$NON-NLS-1$
                    bufBefore.append("(){\n");
                    fillWithTypeStubs(bufBefore, bufAfter, focalPosition, anonDecl.bodyDeclarations());
                    //$NON-NLS-1$
                    bufAfter.append("};\n");
                    return false;
                }
            });
            buf = bufAfter;
            //$NON-NLS-1$
            buf.append("}\n");
            continue;
        }
        AbstractTypeDeclaration decl = (AbstractTypeDeclaration) bodyDeclaration;
        buf = decl.getStartPosition() < focalPosition ? bufBefore : bufAfter;
        appendModifiers(buf, decl.modifiers());
        if (decl instanceof TypeDeclaration) {
            TypeDeclaration type = (TypeDeclaration) decl;
            //$NON-NLS-1$//$NON-NLS-2$
            buf.append(type.isInterface() ? "interface " : "class ");
            buf.append(type.getName().getIdentifier());
            appendTypeParameters(buf, type.typeParameters());
            if (type.getSuperclassType() != null) {
                //$NON-NLS-1$
                buf.append(" extends ");
                buf.append(ASTNodes.asString(type.getSuperclassType()));
            }
            List<Type> superInterfaces = type.superInterfaceTypes();
            appendSuperInterfaces(buf, superInterfaces);
        } else if (decl instanceof AnnotationTypeDeclaration) {
            AnnotationTypeDeclaration annotation = (AnnotationTypeDeclaration) decl;
            //$NON-NLS-1$
            buf.append("@interface ");
            buf.append(annotation.getName().getIdentifier());
        } else if (decl instanceof EnumDeclaration) {
            EnumDeclaration enumDecl = (EnumDeclaration) decl;
            //$NON-NLS-1$
            buf.append("enum ");
            buf.append(enumDecl.getName().getIdentifier());
            List<Type> superInterfaces = enumDecl.superInterfaceTypes();
            appendSuperInterfaces(buf, superInterfaces);
        }
        //$NON-NLS-1$
        buf.append("{\n");
        if (decl instanceof EnumDeclaration)
            //$NON-NLS-1$
            buf.append(";\n");
        fillWithTypeStubs(bufBefore, bufAfter, focalPosition, decl.bodyDeclarations());
        buf = decl.getStartPosition() + decl.getLength() < focalPosition ? bufBefore : bufAfter;
        //$NON-NLS-1$
        buf.append("}\n");
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) HierarchicalASTVisitor(org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor) EnumDeclaration(org.eclipse.jdt.core.dom.EnumDeclaration) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

HierarchicalASTVisitor (org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor)2 HashSet (java.util.HashSet)1 IType (org.eclipse.jdt.core.IType)1 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)1 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)1 AnnotationTypeDeclaration (org.eclipse.jdt.core.dom.AnnotationTypeDeclaration)1 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)1 ArrayType (org.eclipse.jdt.core.dom.ArrayType)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 Block (org.eclipse.jdt.core.dom.Block)1 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)1 EnumDeclaration (org.eclipse.jdt.core.dom.EnumDeclaration)1 Expression (org.eclipse.jdt.core.dom.Expression)1 IBinding (org.eclipse.jdt.core.dom.IBinding)1 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1 Name (org.eclipse.jdt.core.dom.Name)1 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)1