Search in sources :

Example 16 with Expression

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

the class EclipseHandlerUtil method addAnnotation.

private static Annotation[] addAnnotation(ASTNode source, Annotation[] originalAnnotationArray, char[][] annotationTypeFqn, ASTNode arg) {
    char[] simpleName = annotationTypeFqn[annotationTypeFqn.length - 1];
    if (originalAnnotationArray != null)
        for (Annotation ann : originalAnnotationArray) {
            if (ann.type instanceof QualifiedTypeReference) {
                char[][] t = ((QualifiedTypeReference) ann.type).tokens;
                if (Arrays.deepEquals(t, annotationTypeFqn))
                    return originalAnnotationArray;
            }
            if (ann.type instanceof SingleTypeReference) {
                char[] lastToken = ((SingleTypeReference) ann.type).token;
                if (Arrays.equals(lastToken, simpleName))
                    return originalAnnotationArray;
            }
        }
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    long[] poss = new long[annotationTypeFqn.length];
    Arrays.fill(poss, p);
    QualifiedTypeReference qualifiedType = new QualifiedTypeReference(annotationTypeFqn, poss);
    setGeneratedBy(qualifiedType, source);
    Annotation ann;
    if (arg instanceof Expression) {
        SingleMemberAnnotation sma = new SingleMemberAnnotation(qualifiedType, pS);
        sma.declarationSourceEnd = pE;
        arg.sourceStart = pS;
        arg.sourceEnd = pE;
        sma.memberValue = (Expression) arg;
        setGeneratedBy(sma.memberValue, source);
        ann = sma;
    } else if (arg instanceof MemberValuePair) {
        NormalAnnotation na = new NormalAnnotation(qualifiedType, pS);
        na.declarationSourceEnd = pE;
        arg.sourceStart = pS;
        arg.sourceEnd = pE;
        na.memberValuePairs = new MemberValuePair[] { (MemberValuePair) arg };
        setGeneratedBy(na.memberValuePairs[0], source);
        setGeneratedBy(na.memberValuePairs[0].value, source);
        na.memberValuePairs[0].value.sourceStart = pS;
        na.memberValuePairs[0].value.sourceEnd = pE;
        ann = na;
    } else {
        MarkerAnnotation ma = new MarkerAnnotation(qualifiedType, pS);
        ma.declarationSourceEnd = pE;
        ann = ma;
    }
    setGeneratedBy(ann, source);
    if (originalAnnotationArray == null)
        return new Annotation[] { ann };
    Annotation[] newAnnotationArray = new Annotation[originalAnnotationArray.length + 1];
    System.arraycopy(originalAnnotationArray, 0, newAnnotationArray, 0, originalAnnotationArray.length);
    newAnnotationArray[originalAnnotationArray.length] = ann;
    return newAnnotationArray;
}
Also used : SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) NormalAnnotation(org.eclipse.jdt.internal.compiler.ast.NormalAnnotation) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) MemberValuePair(org.eclipse.jdt.internal.compiler.ast.MemberValuePair) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) NormalAnnotation(org.eclipse.jdt.internal.compiler.ast.NormalAnnotation)

Example 17 with Expression

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

the class EclipseHandlerUtil method createFieldAccessor.

static Expression createFieldAccessor(EclipseNode field, FieldAccess fieldAccess, ASTNode source) {
    int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd;
    long p = (long) pS << 32 | pE;
    boolean lookForGetter = lookForGetter(field, fieldAccess);
    GetterMethod getter = lookForGetter ? findGetter(field) : null;
    if (getter == null) {
        FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
        FieldReference ref = new FieldReference(fieldDecl.name, p);
        if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) {
            EclipseNode containerNode = field.up();
            if (containerNode != null && containerNode.get() instanceof TypeDeclaration) {
                ref.receiver = new SingleNameReference(((TypeDeclaration) containerNode.get()).name, p);
            } else {
                Expression smallRef = new FieldReference(field.getName().toCharArray(), p);
                if (source != null)
                    setGeneratedBy(smallRef, source);
                return smallRef;
            }
        } else {
            ref.receiver = new ThisReference(pS, pE);
        }
        if (source != null) {
            setGeneratedBy(ref, source);
            setGeneratedBy(ref.receiver, source);
        }
        return ref;
    }
    MessageSend call = new MessageSend();
    setGeneratedBy(call, source);
    call.sourceStart = pS;
    call.statementEnd = call.sourceEnd = pE;
    call.receiver = new ThisReference(pS, pE);
    setGeneratedBy(call.receiver, source);
    call.selector = getter.name;
    return call;
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) EclipseNode(lombok.eclipse.EclipseNode) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 18 with Expression

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

the class EclipseHandlerUtil method unboxAndRemoveAnnotationParameter.

public static List<Annotation> unboxAndRemoveAnnotationParameter(Annotation annotation, String annotationName, String errorName, EclipseNode errorNode) {
    if ("value".equals(annotationName)) {
        // and onConstructor. Let's exit early and very obviously:
        throw new UnsupportedOperationException("Lombok cannot unbox 'value' from SingleMemberAnnotation at this time.");
    }
    if (!NormalAnnotation.class.equals(annotation.getClass())) {
        // CompletionOnAnnotationMemberValuePair from triggering this handler.
        return Collections.emptyList();
    }
    NormalAnnotation normalAnnotation = (NormalAnnotation) annotation;
    MemberValuePair[] pairs = normalAnnotation.memberValuePairs;
    if (pairs == null)
        return Collections.emptyList();
    char[] nameAsCharArray = annotationName.toCharArray();
    top: for (int i = 0; i < pairs.length; i++) {
        boolean allowRaw;
        char[] name = pairs[i].name;
        if (name == null)
            continue;
        if (name.length < nameAsCharArray.length)
            continue;
        for (int j = 0; j < nameAsCharArray.length; j++) {
            if (name[j] != nameAsCharArray[j])
                continue top;
        }
        allowRaw = name.length > nameAsCharArray.length;
        for (int j = nameAsCharArray.length; j < name.length; j++) {
            if (name[j] != '_')
                continue top;
        }
        // If we're still here it's the targeted annotation param.
        Expression value = pairs[i].value;
        MemberValuePair[] newPairs = new MemberValuePair[pairs.length - 1];
        if (i > 0)
            System.arraycopy(pairs, 0, newPairs, 0, i);
        if (i < pairs.length - 1)
            System.arraycopy(pairs, i + 1, newPairs, i, pairs.length - i - 1);
        normalAnnotation.memberValuePairs = newPairs;
        // We have now removed the annotation parameter and stored the value,
        // which we must now unbox. It's either annotations, or @__(annotations).
        Expression content = null;
        if (value instanceof ArrayInitializer) {
            if (!allowRaw) {
                addError(errorName, errorNode);
                return Collections.emptyList();
            }
            content = value;
        } else if (!(value instanceof Annotation)) {
            addError(errorName, errorNode);
            return Collections.emptyList();
        } else {
            Annotation atDummyIdentifier = (Annotation) value;
            if (atDummyIdentifier.type instanceof SingleTypeReference && isAllValidOnXCharacters(((SingleTypeReference) atDummyIdentifier.type).token)) {
                if (atDummyIdentifier instanceof MarkerAnnotation) {
                    return Collections.emptyList();
                } else if (atDummyIdentifier instanceof NormalAnnotation) {
                    MemberValuePair[] mvps = ((NormalAnnotation) atDummyIdentifier).memberValuePairs;
                    if (mvps == null || mvps.length == 0) {
                        return Collections.emptyList();
                    }
                    if (mvps.length == 1 && Arrays.equals("value".toCharArray(), mvps[0].name)) {
                        content = mvps[0].value;
                    }
                } else if (atDummyIdentifier instanceof SingleMemberAnnotation) {
                    content = ((SingleMemberAnnotation) atDummyIdentifier).memberValue;
                } else {
                    addError(errorName, errorNode);
                    return Collections.emptyList();
                }
            } else {
                if (allowRaw) {
                    content = atDummyIdentifier;
                } else {
                    addError(errorName, errorNode);
                    return Collections.emptyList();
                }
            }
        }
        if (content == null) {
            addError(errorName, errorNode);
            return Collections.emptyList();
        }
        if (content instanceof Annotation) {
            return Collections.singletonList((Annotation) content);
        } else if (content instanceof ArrayInitializer) {
            Expression[] expressions = ((ArrayInitializer) content).expressions;
            List<Annotation> result = new ArrayList<Annotation>();
            if (expressions != null)
                for (Expression ex : expressions) {
                    if (ex instanceof Annotation)
                        result.add((Annotation) ex);
                    else {
                        addError(errorName, errorNode);
                        return Collections.emptyList();
                    }
                }
            return result;
        } else {
            addError(errorName, errorNode);
            return Collections.emptyList();
        }
    }
    return Collections.emptyList();
}
Also used : SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) ArrayList(java.util.ArrayList) MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) NormalAnnotation(org.eclipse.jdt.internal.compiler.ast.NormalAnnotation) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) MarkerAnnotation(org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation) MemberValuePair(org.eclipse.jdt.internal.compiler.ast.MemberValuePair) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) NormalAnnotation(org.eclipse.jdt.internal.compiler.ast.NormalAnnotation) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 19 with Expression

use of org.eclipse.jdt.internal.compiler.ast.Expression in project che by eclipse.

the class SourceTypeConverter method convert.

/*
	 * Convert a method source element into a parsed method/constructor declaration
	 */
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException {
    AbstractMethodDeclaration method;
    /* only source positions available */
    int start = methodInfo.getNameSourceStart();
    int end = methodInfo.getNameSourceEnd();
    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed
		   on behalf of a 1.4 project we must internalize type variables properly in order to be able to
		   recognize usages of them in the method signature, to apply substitutions and thus to be able to
		   detect overriding in the presence of generics. If we simply drop them, when the method signature
		   refers to the type parameter, we won't know it should be bound to the type parameter and perform
		   incorrect lookup and may mistakenly end up with missing types
		 */
    TypeParameter[] typeParams = null;
    char[][] typeParameterNames = methodInfo.getTypeParameterNames();
    if (typeParameterNames != null) {
        int parameterCount = typeParameterNames.length;
        if (parameterCount > 0) {
            // method's type parameters must be null if no type parameter
            char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds();
            typeParams = new TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
            }
        }
    }
    int modifiers = methodInfo.getModifiers();
    if (methodInfo.isConstructor()) {
        ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
        decl.bits &= ~ASTNode.IsDefaultConstructor;
        method = decl;
        decl.typeParameters = typeParams;
    } else {
        MethodDeclaration decl;
        if (methodInfo.isAnnotationMethod()) {
            AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration(compilationResult);
            /* conversion of default value */
            SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo;
            boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1;
            if ((this.flags & FIELD_INITIALIZATION) != 0) {
                if (hasDefaultValue) {
                    char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1);
                    if (defaultValueSource != null) {
                        Expression expression = parseMemberValue(defaultValueSource);
                        if (expression != null) {
                            annotationMethodDeclaration.defaultValue = expression;
                        }
                    } else {
                        // could not retrieve the default value
                        hasDefaultValue = false;
                    }
                }
            }
            if (hasDefaultValue)
                modifiers |= ClassFileConstants.AccAnnotationDefault;
            decl = annotationMethodDeclaration;
        } else {
            decl = new MethodDeclaration(compilationResult);
        }
        // convert return type
        decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end);
        // type parameters
        decl.typeParameters = typeParams;
        method = decl;
    }
    method.selector = methodHandle.getElementName().toCharArray();
    boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0;
    method.modifiers = modifiers & ~ClassFileConstants.AccVarargs;
    method.sourceStart = start;
    method.sourceEnd = end;
    method.declarationSourceStart = methodInfo.getDeclarationSourceStart();
    method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd();
    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        method.annotations = convertAnnotations(methodHandle);
    }
    /* convert arguments */
    String[] argumentTypeSignatures = methodHandle.getParameterTypes();
    char[][] argumentNames = methodInfo.getArgumentNames();
    int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length;
    if (argumentCount > 0) {
        ILocalVariable[] parameters = methodHandle.getParameters();
        long position = ((long) start << 32) + end;
        method.arguments = new Argument[argumentCount];
        for (int i = 0; i < argumentCount; i++) {
            TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end);
            if (isVarargs && i == argumentCount - 1) {
                typeReference.bits |= ASTNode.IsVarArgs;
            }
            method.arguments[i] = new Argument(argumentNames[i], position, typeReference, ClassFileConstants.AccDefault);
            // convert 1.5 specific constructs only if compliance is 1.5 or above
            if (this.has1_5Compliance) {
                /* convert annotations */
                method.arguments[i].annotations = convertAnnotations(parameters[i]);
            }
        }
    }
    /* convert thrown exceptions */
    char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames();
    int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
    if (exceptionCount > 0) {
        method.thrownExceptions = new TypeReference[exceptionCount];
        for (int i = 0; i < exceptionCount; i++) {
            method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end);
        }
    }
    /* convert local and anonymous types */
    if ((this.flags & LOCAL_TYPE) != 0) {
        IJavaElement[] children = methodInfo.getChildren();
        int typesLength = children.length;
        if (typesLength != 0) {
            Statement[] statements = new Statement[typesLength];
            for (int i = 0; i < typesLength; i++) {
                SourceType type = (SourceType) children[i];
                TypeDeclaration localType = convert(type, compilationResult);
                if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
                    QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
                    expression.type = localType.superclass;
                    localType.superclass = null;
                    localType.superInterfaces = null;
                    localType.allocation = expression;
                    statements[i] = expression;
                } else {
                    statements[i] = localType;
                }
            }
            method.statements = statements;
        }
    }
    return method;
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) IJavaElement(org.eclipse.jdt.core.IJavaElement) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) SourceAnnotationMethodInfo(org.eclipse.jdt.internal.core.SourceAnnotationMethodInfo) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 20 with Expression

use of org.eclipse.jdt.internal.compiler.ast.Expression in project che by eclipse.

the class SourceTypeConverter method convertAnnotations.

private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException {
    IAnnotation[] annotations = element.getAnnotations();
    int length = annotations.length;
    Annotation[] astAnnotations = new Annotation[length];
    if (length > 0) {
        char[] cuSource = getSource();
        int recordedAnnotations = 0;
        for (int i = 0; i < length; i++) {
            ISourceRange positions = annotations[i].getSourceRange();
            int start = positions.getOffset();
            int end = start + positions.getLength();
            char[] annotationSource = CharOperation.subarray(cuSource, start, end);
            if (annotationSource != null) {
                Expression expression = parseMemberValue(annotationSource);
                /*
	    			 * expression can be null or not an annotation if the source has changed between
	    			 * the moment where the annotation source positions have been retrieved and the moment were
	    			 * this parsing occurred.
	    			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916
	    			 */
                if (expression instanceof Annotation) {
                    astAnnotations[recordedAnnotations++] = (Annotation) expression;
                }
            }
        }
        if (length != recordedAnnotations) {
            // resize to remove null annotations
            System.arraycopy(astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0, recordedAnnotations);
        }
    }
    return astAnnotations;
}
Also used : IAnnotation(org.eclipse.jdt.core.IAnnotation) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) IAnnotation(org.eclipse.jdt.core.IAnnotation) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

Expression (org.eclipse.jdt.internal.compiler.ast.Expression)30 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)16 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)16 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)16 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)16 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)15 ArrayList (java.util.ArrayList)14 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)14 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)14 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)13 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)12 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)12 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)11 EclipseNode (lombok.eclipse.EclipseNode)10 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)10 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)10 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)9 CastExpression (org.eclipse.jdt.internal.compiler.ast.CastExpression)9 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)9 QualifiedNameReference (org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)9