Search in sources :

Example 16 with FieldAccess

use of org.eclipse.jdt.core.dom.FieldAccess in project processing by processing.

the class CompletionGenerator method findDeclaration2.

/**
   * A variation of findDeclaration() but accepts an alternate parent ASTNode
   * @param findMe
   * @param alternateParent
   * @return
   */
protected static ASTNode findDeclaration2(Name findMe, ASTNode alternateParent) {
    ASTNode declaringClass;
    ASTNode parent = findMe.getParent();
    ASTNode ret;
    ArrayList<Integer> constrains = new ArrayList<>();
    if (parent.getNodeType() == ASTNode.METHOD_INVOCATION) {
        Expression exp = (Expression) parent.getStructuralProperty(MethodInvocation.EXPRESSION_PROPERTY);
        // Possibly a bug here. Investigate later.
        if (((MethodInvocation) parent).getName().toString().equals(findMe.toString())) {
            constrains.add(ASTNode.METHOD_DECLARATION);
            if (exp != null) {
                constrains.add(ASTNode.TYPE_DECLARATION);
                //              + exp.getClass().getName() + " parent: " + exp.getParent());
                if (exp instanceof MethodInvocation) {
                    SimpleType stp = extracTypeInfo(findDeclaration2(((MethodInvocation) exp).getName(), alternateParent));
                    if (stp == null)
                        return null;
                    declaringClass = findDeclaration2(stp.getName(), alternateParent);
                    return definedIn(declaringClass, ((MethodInvocation) parent).getName().toString(), constrains);
                } else if (exp instanceof FieldAccess) {
                    SimpleType stp = extracTypeInfo(findDeclaration2(((FieldAccess) exp).getName(), alternateParent));
                    if (stp == null)
                        return null;
                    declaringClass = findDeclaration2((stp.getName()), alternateParent);
                    return definedIn(declaringClass, ((MethodInvocation) parent).getName().toString(), constrains);
                }
                if (exp instanceof SimpleName) {
                    SimpleType stp = extracTypeInfo(findDeclaration2(((SimpleName) exp), alternateParent));
                    if (stp == null)
                        return null;
                    declaringClass = findDeclaration2(stp.getName(), alternateParent);
                    //            log("MI.SN " + getNodeAsString(declaringClass));
                    constrains.add(ASTNode.METHOD_DECLARATION);
                    return definedIn(declaringClass, ((MethodInvocation) parent).getName().toString(), constrains);
                }
            }
        } else {
            // Move one up the ast. V V IMP!!
            parent = parent.getParent();
            alternateParent = alternateParent.getParent();
        }
    } else if (parent.getNodeType() == ASTNode.FIELD_ACCESS) {
        FieldAccess fa = (FieldAccess) parent;
        Expression exp = fa.getExpression();
        if (fa.getName().toString().equals(findMe.toString())) {
            constrains.add(ASTNode.FIELD_DECLARATION);
            if (exp != null) {
                constrains.add(ASTNode.TYPE_DECLARATION);
                //              + exp.getClass().getName() + " parent: " + exp.getParent());
                if (exp instanceof MethodInvocation) {
                    SimpleType stp = extracTypeInfo(findDeclaration2(((MethodInvocation) exp).getName(), alternateParent));
                    if (stp == null)
                        return null;
                    declaringClass = findDeclaration2(stp.getName(), alternateParent);
                    return definedIn(declaringClass, fa.getName().toString(), constrains);
                } else if (exp instanceof FieldAccess) {
                    SimpleType stp = extracTypeInfo(findDeclaration2(((FieldAccess) exp).getName(), alternateParent));
                    if (stp == null)
                        return null;
                    declaringClass = findDeclaration2((stp.getName()), alternateParent);
                    constrains.add(ASTNode.TYPE_DECLARATION);
                    return definedIn(declaringClass, fa.getName().toString(), constrains);
                }
                if (exp instanceof SimpleName) {
                    SimpleType stp = extracTypeInfo(findDeclaration2(((SimpleName) exp), alternateParent));
                    if (stp == null)
                        return null;
                    declaringClass = findDeclaration2(stp.getName(), alternateParent);
                    //            log("FA.SN " + getNodeAsString(declaringClass));
                    constrains.add(ASTNode.METHOD_DECLARATION);
                    return definedIn(declaringClass, fa.getName().toString(), constrains);
                }
            }
        } else {
            // Move one up the ast. V V IMP!!
            parent = parent.getParent();
            alternateParent = alternateParent.getParent();
        }
    } else if (parent.getNodeType() == ASTNode.QUALIFIED_NAME) {
        QualifiedName qn = (QualifiedName) parent;
        if (!findMe.toString().equals(qn.getQualifier().toString())) {
            SimpleType stp = extracTypeInfo(findDeclaration2((qn.getQualifier()), alternateParent));
            if (stp == null)
                return null;
            declaringClass = findDeclaration2(stp.getName(), alternateParent);
            //        log(qn.getQualifier() + "->" + qn.getName());
            //        log("QN decl class: " + getNodeAsString(declaringClass));
            constrains.clear();
            constrains.add(ASTNode.TYPE_DECLARATION);
            constrains.add(ASTNode.FIELD_DECLARATION);
            return definedIn(declaringClass, qn.getName().toString(), constrains);
        } else {
            if (findMe instanceof QualifiedName) {
                QualifiedName qnn = (QualifiedName) findMe;
                //          log("findMe is a QN, "
                //              + (qnn.getQualifier().toString() + " other " + qnn.getName()
                //                  .toString()));
                SimpleType stp = extracTypeInfo(findDeclaration2((qnn.getQualifier()), alternateParent));
                if (stp == null) {
                    return null;
                }
                //          log(qnn.getQualifier() + "->" + qnn.getName());
                declaringClass = findDeclaration2(stp.getName(), alternateParent);
                //          log("QN decl class: "
                //              + getNodeAsString(declaringClass));
                constrains.clear();
                constrains.add(ASTNode.TYPE_DECLARATION);
                constrains.add(ASTNode.FIELD_DECLARATION);
                return definedIn(declaringClass, qnn.getName().toString(), constrains);
            }
        }
    } else if (parent.getNodeType() == ASTNode.SIMPLE_TYPE) {
        constrains.add(ASTNode.TYPE_DECLARATION);
        if (parent.getParent().getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
            constrains.add(ASTNode.CLASS_INSTANCE_CREATION);
    } else if (parent instanceof Expression) {
    //      constrains.add(ASTNode.TYPE_DECLARATION);
    //      constrains.add(ASTNode.METHOD_DECLARATION);
    //      constrains.add(ASTNode.FIELD_DECLARATION);
    }
    //    log("Alternate parent: " + getNodeAsString(alternateParent));
    while (alternateParent != null) {
        //          + getNodeAsString(alternateParent));
        for (Object oprop : alternateParent.structuralPropertiesForType()) {
            StructuralPropertyDescriptor prop = (StructuralPropertyDescriptor) oprop;
            if (prop.isChildProperty() || prop.isSimpleProperty()) {
                if (alternateParent.getStructuralProperty(prop) instanceof ASTNode) {
                    //            log(prop + " C/S Prop of -> "
                    //                + getNodeAsString(alternateParent));
                    ret = definedIn((ASTNode) alternateParent.getStructuralProperty(prop), findMe.toString(), constrains);
                    if (ret != null)
                        return ret;
                }
            } else if (prop.isChildListProperty()) {
                //          log((prop) + " ChildList props of "
                //              + getNodeAsString(alternateParent));
                List<ASTNode> nodelist = (List<ASTNode>) alternateParent.getStructuralProperty(prop);
                for (ASTNode retNode : nodelist) {
                    ret = definedIn(retNode, findMe.toString(), constrains);
                    if (ret != null)
                        return ret;
                }
            }
        }
        alternateParent = alternateParent.getParent();
    }
    return null;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 17 with FieldAccess

use of org.eclipse.jdt.core.dom.FieldAccess in project AutoRefactor by JnRouvignac.

the class BooleanConstantRatherThanValueOfRefactoring method toFieldAccess.

private FieldAccess toFieldAccess(final MethodInvocation node, final boolean booleanLiteral) {
    final ASTBuilder b = ctx.getASTBuilder();
    final FieldAccess fa = b.getAST().newFieldAccess();
    if (node.getExpression() instanceof Name) {
        fa.setExpression(b.copy(node.getExpression()));
    }
    fa.setName(b.simpleName(booleanLiteral ? "TRUE" : "FALSE"));
    return fa;
}
Also used : FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) Name(org.eclipse.jdt.core.dom.Name)

Example 18 with FieldAccess

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

the class DelegateFieldCreator method createDelegateFieldInitializer.

// ******************* INTERNAL HELPERS ***************************
private Expression createDelegateFieldInitializer(final FieldDeclaration declaration) throws JavaModelException {
    Assert.isNotNull(declaration);
    Expression qualification = getAccess();
    if (qualification != null) {
        FieldAccess access = getAst().newFieldAccess();
        access.setExpression(qualification);
        access.setName(getAst().newSimpleName(getNewElementName()));
        return access;
    } else {
        SimpleName access = getAst().newSimpleName(getNewElementName());
        return access;
    }
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 19 with FieldAccess

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

the class StubUtility2 method createDelegationStub.

public static MethodDeclaration createDelegationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding delegate, IVariableBinding delegatingField, CodeGenerationSettings settings) throws CoreException {
    Assert.isNotNull(delegate);
    Assert.isNotNull(delegatingField);
    Assert.isNotNull(settings);
    AST ast = rewrite.getAST();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, delegate.getModifiers() & ~Modifier.SYNCHRONIZED & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
    decl.setName(ast.newSimpleName(delegate.getName()));
    decl.setConstructor(false);
    createTypeParameters(imports, context, ast, delegate, decl);
    decl.setReturnType2(imports.addImport(delegate.getReturnType(), ast, context));
    List<SingleVariableDeclaration> params = createParameters(unit.getJavaProject(), imports, context, ast, delegate, null, decl);
    createThrownExceptions(decl, delegate, imports, context, ast);
    Block body = ast.newBlock();
    decl.setBody(body);
    String delimiter = StubUtility.getLineDelimiterUsed(unit);
    Statement statement = null;
    MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setName(ast.newSimpleName(delegate.getName()));
    List<Expression> arguments = invocation.arguments();
    for (int i = 0; i < params.size(); i++) arguments.add(ast.newSimpleName(params.get(i).getName().getIdentifier()));
    if (settings.useKeywordThis) {
        FieldAccess access = ast.newFieldAccess();
        access.setExpression(ast.newThisExpression());
        access.setName(ast.newSimpleName(delegatingField.getName()));
        invocation.setExpression(access);
    } else
        invocation.setExpression(ast.newSimpleName(delegatingField.getName()));
    if (delegate.getReturnType().isPrimitive() && delegate.getReturnType().getName().equals("void")) {
        //$NON-NLS-1$
        statement = ast.newExpressionStatement(invocation);
    } else {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(invocation);
        statement = returnStatement;
    }
    body.statements().add(statement);
    ITypeBinding declaringType = delegatingField.getDeclaringClass();
    if (declaringType == null) {
        // can be null for
        return decl;
    }
    String qualifiedName = declaringType.getQualifiedName();
    IPackageBinding packageBinding = declaringType.getPackage();
    if (packageBinding != null) {
        if (packageBinding.getName().length() > 0 && qualifiedName.startsWith(packageBinding.getName()))
            qualifiedName = qualifiedName.substring(packageBinding.getName().length());
    }
    if (settings.createComments) {
        /*
			 * TODO: have API for delegate method comments This is an inlined
			 * version of
			 * {@link CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, IMethodBinding, String)}
			 */
        delegate = delegate.getMethodDeclaration();
        String declaringClassQualifiedName = delegate.getDeclaringClass().getQualifiedName();
        String linkToMethodName = delegate.getName();
        String[] parameterTypesQualifiedNames = StubUtility.getParameterTypeNamesForSeeTag(delegate);
        String string = StubUtility.getMethodComment(unit, qualifiedName, decl, delegate.isDeprecated(), linkToMethodName, declaringClassQualifiedName, parameterTypesQualifiedNames, true, delimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Javadoc(org.eclipse.jdt.core.dom.Javadoc) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 20 with FieldAccess

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

the class StubUtility method getBaseNameFromExpression.

private static String getBaseNameFromExpression(IJavaProject project, Expression assignedExpression, int variableKind) {
    String name = null;
    if (assignedExpression instanceof CastExpression) {
        assignedExpression = ((CastExpression) assignedExpression).getExpression();
    }
    if (assignedExpression instanceof Name) {
        Name simpleNode = (Name) assignedExpression;
        IBinding binding = simpleNode.resolveBinding();
        if (binding instanceof IVariableBinding)
            return getBaseName((IVariableBinding) binding, project);
        return ASTNodes.getSimpleNameIdentifier(simpleNode);
    } else if (assignedExpression instanceof MethodInvocation) {
        name = ((MethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof SuperMethodInvocation) {
        name = ((SuperMethodInvocation) assignedExpression).getName().getIdentifier();
    } else if (assignedExpression instanceof FieldAccess) {
        return ((FieldAccess) assignedExpression).getName().getIdentifier();
    } else if (variableKind == NamingConventions.VK_STATIC_FINAL_FIELD && (assignedExpression instanceof StringLiteral || assignedExpression instanceof NumberLiteral)) {
        String string = assignedExpression instanceof StringLiteral ? ((StringLiteral) assignedExpression).getLiteralValue() : ((NumberLiteral) assignedExpression).getToken();
        StringBuffer res = new StringBuffer();
        boolean needsUnderscore = false;
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (Character.isJavaIdentifierPart(ch)) {
                if (res.length() == 0 && !Character.isJavaIdentifierStart(ch) || needsUnderscore) {
                    res.append('_');
                }
                res.append(ch);
                needsUnderscore = false;
            } else {
                needsUnderscore = res.length() > 0;
            }
        }
        if (res.length() > 0) {
            return res.toString();
        }
    }
    if (name != null) {
        for (int i = 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) {
            String curr = KNOWN_METHOD_NAME_PREFIXES[i];
            if (name.startsWith(curr)) {
                if (name.equals(curr)) {
                    // don't suggest 'get' as variable name
                    return null;
                } else if (Character.isUpperCase(name.charAt(curr.length()))) {
                    return name.substring(curr.length());
                }
            }
        }
    }
    return name;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) CastExpression(org.eclipse.jdt.core.dom.CastExpression) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Aggregations

FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)30 SimpleName (org.eclipse.jdt.core.dom.SimpleName)20 Expression (org.eclipse.jdt.core.dom.Expression)18 ASTNode (org.eclipse.jdt.core.dom.ASTNode)17 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 IBinding (org.eclipse.jdt.core.dom.IBinding)11 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 Name (org.eclipse.jdt.core.dom.Name)10 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)8 AST (org.eclipse.jdt.core.dom.AST)7 Assignment (org.eclipse.jdt.core.dom.Assignment)7 Block (org.eclipse.jdt.core.dom.Block)7 SuperFieldAccess (org.eclipse.jdt.core.dom.SuperFieldAccess)7 CastExpression (org.eclipse.jdt.core.dom.CastExpression)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 ArrayList (java.util.ArrayList)5