Search in sources :

Example 16 with ASTNode

use of org.eclipse.jdt.internal.compiler.ast.ASTNode in project lombok by rzwitserloot.

the class PatchExtensionMethodCompletionProposal method getFirstParameterType.

static TypeBinding getFirstParameterType(TypeDeclaration decl, CompletionProposalCollector completionProposalCollector) {
    TypeBinding firstParameterType = null;
    ASTNode node = getAssistNode(completionProposalCollector);
    if (node == null)
        return null;
    if (!(node instanceof CompletionOnQualifiedNameReference) && !(node instanceof CompletionOnSingleNameReference) && !(node instanceof CompletionOnMemberAccess))
        return null;
    // Never offer on 'super.<autocomplete>'.
    if (node instanceof FieldReference && ((FieldReference) node).receiver instanceof SuperReference)
        return null;
    if (node instanceof NameReference) {
        Binding binding = ((NameReference) node).binding;
        /*			if ((node instanceof SingleNameReference) && (((SingleNameReference) node).token.length == 0)) {
				firstParameterType = decl.binding;
			} else */
        if (binding instanceof VariableBinding) {
            firstParameterType = ((VariableBinding) binding).type;
        }
    } else if (node instanceof FieldReference) {
        firstParameterType = ((FieldReference) node).actualReceiverType;
    }
    return firstParameterType;
}
Also used : Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) CompletionOnQualifiedNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnQualifiedNameReference) NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) CompletionOnSingleNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnSingleNameReference) CompletionOnMemberAccess(org.eclipse.jdt.internal.codeassist.complete.CompletionOnMemberAccess) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) CompletionOnSingleNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnSingleNameReference) CompletionOnQualifiedNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnQualifiedNameReference) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) SuperReference(org.eclipse.jdt.internal.compiler.ast.SuperReference)

Example 17 with ASTNode

use of org.eclipse.jdt.internal.compiler.ast.ASTNode in project lombok by rzwitserloot.

the class HandleConstructor method createConstructor.

@SuppressWarnings("deprecation")
public static ConstructorDeclaration createConstructor(AccessLevel level, EclipseNode type, Collection<EclipseNode> fields, boolean allToDefault, EclipseNode sourceNode, List<Annotation> onConstructor) {
    ASTNode source = sourceNode.get();
    TypeDeclaration typeDeclaration = ((TypeDeclaration) type.get());
    long p = (long) source.sourceStart << 32 | source.sourceEnd;
    boolean isEnum = (((TypeDeclaration) type.get()).modifiers & ClassFileConstants.AccEnum) != 0;
    if (isEnum)
        level = AccessLevel.PRIVATE;
    boolean addConstructorProperties;
    if (fields.isEmpty()) {
        addConstructorProperties = false;
    } else {
        Boolean v = type.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_ADD_CONSTRUCTOR_PROPERTIES);
        addConstructorProperties = v != null ? v.booleanValue() : Boolean.FALSE.equals(type.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES));
    }
    ConstructorDeclaration constructor = new ConstructorDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
    constructor.modifiers = toEclipseModifier(level);
    constructor.selector = typeDeclaration.name;
    constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
    constructor.constructorCall.sourceStart = source.sourceStart;
    constructor.constructorCall.sourceEnd = source.sourceEnd;
    constructor.thrownExceptions = null;
    constructor.typeParameters = null;
    constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
    constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
    constructor.arguments = null;
    List<Argument> params = new ArrayList<Argument>();
    List<Statement> assigns = new ArrayList<Statement>();
    List<Statement> nullChecks = new ArrayList<Statement>();
    for (EclipseNode fieldNode : fields) {
        FieldDeclaration field = (FieldDeclaration) fieldNode.get();
        char[] rawName = field.name;
        char[] fieldName = removePrefixFromField(fieldNode);
        FieldReference thisX = new FieldReference(rawName, p);
        int s = (int) (p >> 32);
        int e = (int) p;
        thisX.receiver = new ThisReference(s, e);
        Expression assignmentExpr = allToDefault ? getDefaultExpr(field.type, s, e) : new SingleNameReference(fieldName, p);
        Assignment assignment = new Assignment(thisX, assignmentExpr, (int) p);
        assignment.sourceStart = (int) (p >> 32);
        assignment.sourceEnd = assignment.statementEnd = (int) (p >> 32);
        assigns.add(assignment);
        if (!allToDefault) {
            long fieldPos = (((long) field.sourceStart) << 32) | field.sourceEnd;
            Argument parameter = new Argument(fieldName, fieldPos, copyType(field.type, source), Modifier.FINAL);
            Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
            Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
            if (nonNulls.length != 0) {
                Statement nullCheck = generateNullCheck(parameter, sourceNode);
                if (nullCheck != null)
                    nullChecks.add(nullCheck);
            }
            parameter.annotations = copyAnnotations(source, nonNulls, nullables);
            params.add(parameter);
        }
    }
    nullChecks.addAll(assigns);
    constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]);
    constructor.arguments = params.isEmpty() ? null : params.toArray(new Argument[params.size()]);
    /* Generate annotations that must  be put on the generated method, and attach them. */
    {
        Annotation[] constructorProperties = null;
        if (!allToDefault && addConstructorProperties && !isLocalType(type)) {
            constructorProperties = createConstructorProperties(source, fields);
        }
        constructor.annotations = copyAnnotations(source, onConstructor.toArray(new Annotation[0]), constructorProperties);
    }
    constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope);
    return constructor;
}
Also used : FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) ExplicitConstructorCall(org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 18 with ASTNode

use of org.eclipse.jdt.internal.compiler.ast.ASTNode in project lombok by rzwitserloot.

the class HandleVal method visitLocal.

@Override
public void visitLocal(EclipseNode localNode, LocalDeclaration local) {
    TypeReference type = local.type;
    boolean isVal = typeMatches(val.class, localNode, type);
    boolean isVar = typeMatches(var.class, localNode, type);
    if (!(isVal || isVar))
        return;
    if (isVal)
        handleFlagUsage(localNode, ConfigurationKeys.VAL_FLAG_USAGE, "val");
    if (isVar)
        handleFlagUsage(localNode, ConfigurationKeys.VAR_FLAG_USAGE, "var");
    boolean variableOfForEach = false;
    if (localNode.directUp().get() instanceof ForeachStatement) {
        ForeachStatement fs = (ForeachStatement) localNode.directUp().get();
        variableOfForEach = fs.elementVariable == local;
    }
    String annotation = isVal ? "val" : "var";
    if (local.initialization == null && !variableOfForEach) {
        localNode.addError("'" + annotation + "' on a local variable requires an initializer expression");
        return;
    }
    if (local.initialization instanceof ArrayInitializer) {
        localNode.addError("'" + annotation + "' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })");
        return;
    }
    ASTNode parentRaw = localNode.directUp().get();
    if (isVal && parentRaw instanceof ForStatement) {
        localNode.addError("'val' is not allowed in old-style for loops");
        return;
    }
    if (parentRaw instanceof ForStatement && ((ForStatement) parentRaw).initializations != null && ((ForStatement) parentRaw).initializations.length > 1) {
        localNode.addError("'var' is not allowed in old-style for loops if there is more than 1 initializer");
        return;
    }
    if (local.initialization != null && local.initialization.getClass().getName().equals("org.eclipse.jdt.internal.compiler.ast.LambdaExpression")) {
        localNode.addError("'" + annotation + "' is not allowed with lambda expressions.");
        return;
    }
    if (isVar && local.initialization instanceof NullLiteral) {
        localNode.addError("variable initializer is 'null'");
        return;
    }
}
Also used : ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ForStatement(org.eclipse.jdt.internal.compiler.ast.ForStatement) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) ForeachStatement(org.eclipse.jdt.internal.compiler.ast.ForeachStatement) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 19 with ASTNode

use of org.eclipse.jdt.internal.compiler.ast.ASTNode in project eclipse.jdt.ls by eclipse.

the class SnippetCompletionProposal method accept.

private static boolean accept(ICompilationUnit cu, CompletionContext completionContext, boolean acceptClass) {
    if (completionContext != null && completionContext.isExtended()) {
        if (completionContext.isInJavadoc()) {
            return false;
        }
        if (completionContext instanceof InternalCompletionContext) {
            InternalCompletionContext internalCompletionContext = (InternalCompletionContext) completionContext;
            ASTNode node = internalCompletionContext.getCompletionNode();
            if (node instanceof CompletionOnKeyword2) {
                return true;
            }
            if (node instanceof CompletionOnFieldType) {
                return true;
            }
            if (acceptClass && node instanceof CompletionOnSingleNameReference) {
                if (completionContext.getEnclosingElement() instanceof IMethod) {
                    CompilationUnit ast = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
                    org.eclipse.jdt.core.dom.ASTNode astNode = ASTNodeSearchUtil.getAstNode(ast, completionContext.getTokenStart(), completionContext.getTokenEnd() - completionContext.getTokenStart() + 1);
                    return (astNode == null || (astNode.getParent() instanceof ExpressionStatement));
                }
                return true;
            }
        }
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CompletionOnKeyword2(org.eclipse.jdt.internal.codeassist.complete.CompletionOnKeyword2) InternalCompletionContext(org.eclipse.jdt.internal.codeassist.InternalCompletionContext) CompletionOnFieldType(org.eclipse.jdt.internal.codeassist.complete.CompletionOnFieldType) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) CompletionOnSingleNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnSingleNameReference) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)19 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)7 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)7 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)6 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)6 ArrayList (java.util.ArrayList)5 EclipseNode (lombok.eclipse.EclipseNode)5 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)5 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)5 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)4 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)4 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)4 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)3 ConstructorDeclaration (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration)3 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)3 NameReference (org.eclipse.jdt.internal.compiler.ast.NameReference)3 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)3 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 InternalCompletionContext (org.eclipse.jdt.internal.codeassist.InternalCompletionContext)2