Search in sources :

Example 21 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class ConvertAnonymousToNestedRefactoring method accessesAnonymousFields.

private boolean accessesAnonymousFields() {
    List<IVariableBinding> anonymousInnerFieldTypes = getAllEnclosingAnonymousTypesField();
    List<IBinding> accessedField = getAllAccessedFields();
    final Iterator<IVariableBinding> it = anonymousInnerFieldTypes.iterator();
    while (it.hasNext()) {
        final IVariableBinding variableBinding = it.next();
        final Iterator<IBinding> it2 = accessedField.iterator();
        while (it2.hasNext()) {
            IVariableBinding variableBinding2 = (IVariableBinding) it2.next();
            if (Bindings.equals(variableBinding, variableBinding2)) {
                return true;
            }
        }
    }
    return false;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 22 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class ConvertAnonymousToNestedRefactoring method createNewNestedClass.

private AbstractTypeDeclaration createNewNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
    final AST ast = fAnonymousInnerClassNode.getAST();
    final TypeDeclaration newDeclaration = ast.newTypeDeclaration();
    newDeclaration.setInterface(false);
    newDeclaration.setJavadoc(null);
    newDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiersForNestedClass()));
    newDeclaration.setName(ast.newSimpleName(fClassName));
    TypeParameter parameter = null;
    for (int index = 0; index < typeParameters.length; index++) {
        parameter = ast.newTypeParameter();
        parameter.setName(ast.newSimpleName(typeParameters[index].getName()));
        newDeclaration.typeParameters().add(parameter);
    }
    setSuperType(newDeclaration);
    IJavaProject project = fCu.getJavaProject();
    IVariableBinding[] bindings = getUsedLocalVariables();
    ArrayList<String> fieldNames = new ArrayList<String>();
    for (int i = 0; i < bindings.length; i++) {
        String name = StubUtility.getBaseName(bindings[i], project);
        String[] fieldNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, name, 0, fieldNames, true);
        fieldNames.add(fieldNameProposals[0]);
        if (fLinkedProposalModel != null) {
            LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true);
            for (int k = 0; k < fieldNameProposals.length; k++) {
                positionGroup.addProposal(fieldNameProposals[k], null, fieldNameProposals.length - k);
            }
        }
    }
    String[] allFieldNames = fieldNames.toArray(new String[fieldNames.size()]);
    List<BodyDeclaration> newBodyDeclarations = newDeclaration.bodyDeclarations();
    createFieldsForAccessedLocals(rewrite, bindings, allFieldNames, newBodyDeclarations);
    MethodDeclaration newConstructorDecl = createNewConstructor(rewrite, bindings, allFieldNames);
    if (newConstructorDecl != null) {
        newBodyDeclarations.add(newConstructorDecl);
    }
    updateAndMoveBodyDeclarations(rewrite, bindings, allFieldNames, newBodyDeclarations, newConstructorDecl);
    if (doAddComments()) {
        String[] parameterNames = new String[typeParameters.length];
        for (int index = 0; index < parameterNames.length; index++) {
            parameterNames[index] = typeParameters[index].getName();
        }
        String string = CodeGeneration.getTypeComment(rewrite.getCu(), fClassName, parameterNames, StubUtility.getLineDelimiterUsed(fCu));
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC);
            newDeclaration.setJavadoc(javadoc);
        }
    }
    if (fLinkedProposalModel != null) {
        addLinkedPosition(KEY_TYPE_NAME, newDeclaration.getName(), rewrite.getASTRewrite(), false);
        ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite.getASTRewrite(), newDeclaration.modifiers(), false);
    }
    return newDeclaration;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) IJavaProject(org.eclipse.jdt.core.IJavaProject) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 23 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding 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 24 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class ConvertAnonymousToNestedRefactoring method getAllAccessedFields.

private List<IBinding> getAllAccessedFields() {
    final List<IBinding> accessedFields = new ArrayList<IBinding>();
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(FieldAccess node) {
            final IVariableBinding binding = node.resolveFieldBinding();
            if (binding != null && !binding.isEnumConstant())
                accessedFields.add(binding);
            return super.visit(node);
        }

        @Override
        public boolean visit(QualifiedName node) {
            final IBinding binding = node.resolveBinding();
            if (binding != null && binding instanceof IVariableBinding) {
                IVariableBinding variable = (IVariableBinding) binding;
                if (!variable.isEnumConstant() && variable.isField())
                    accessedFields.add(binding);
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(SimpleName node) {
            final IBinding binding = node.resolveBinding();
            if (binding != null && binding instanceof IVariableBinding) {
                IVariableBinding variable = (IVariableBinding) binding;
                if (!variable.isEnumConstant() && variable.isField())
                    accessedFields.add(binding);
            }
            return super.visit(node);
        }

        @Override
        public boolean visit(SuperFieldAccess node) {
            final IVariableBinding binding = node.resolveFieldBinding();
            if (binding != null && !binding.isEnumConstant())
                accessedFields.add(binding);
            return super.visit(node);
        }
    };
    fAnonymousInnerClassNode.accept(visitor);
    return accessedFields;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 25 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project che by eclipse.

the class ConvertAnonymousToNestedRefactoring method areLocalsUsedIn.

private boolean areLocalsUsedIn(Expression fieldInitializer, List<VariableDeclarationFragment> fieldsToInitialize) {
    Set<IBinding> localsUsed = new HashSet<IBinding>(0);
    collectRefrencedVariables(fieldInitializer, localsUsed);
    ITypeBinding anonType = fAnonymousInnerClassNode.resolveBinding();
    for (Iterator<IBinding> iterator = localsUsed.iterator(); iterator.hasNext(); ) {
        IVariableBinding curr = (IVariableBinding) iterator.next();
        if (isBindingToTemp(curr)) {
            // reference a local from outside
            return true;
        } else if (curr.isField() && (curr.getDeclaringClass() == anonType) && fieldsToInitialize.contains(fCompilationUnitNode.findDeclaringNode(curr))) {
            // references a field that references a local from outside
            return true;
        }
    }
    return false;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) HashSet(java.util.HashSet)

Aggregations

IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)101 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)48 IBinding (org.eclipse.jdt.core.dom.IBinding)45 SimpleName (org.eclipse.jdt.core.dom.SimpleName)41 ASTNode (org.eclipse.jdt.core.dom.ASTNode)30 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)28 ArrayList (java.util.ArrayList)22 Expression (org.eclipse.jdt.core.dom.Expression)22 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 Name (org.eclipse.jdt.core.dom.Name)13 CastExpression (org.eclipse.jdt.core.dom.CastExpression)12 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)12 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)12 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)11 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)11 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)10 AST (org.eclipse.jdt.core.dom.AST)9 Assignment (org.eclipse.jdt.core.dom.Assignment)9