Search in sources :

Example 1 with ArrayInitializer

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

the class SourceTypeConverter method convert.

/*
	 * Convert a field source element into a parsed field declaration
	 */
private FieldDeclaration convert(SourceField fieldHandle, TypeDeclaration type, CompilationResult compilationResult) throws JavaModelException {
    SourceFieldElementInfo fieldInfo = (SourceFieldElementInfo) fieldHandle.getElementInfo();
    FieldDeclaration field = new FieldDeclaration();
    int start = fieldInfo.getNameSourceStart();
    int end = fieldInfo.getNameSourceEnd();
    field.name = fieldHandle.getElementName().toCharArray();
    field.sourceStart = start;
    field.sourceEnd = end;
    field.declarationSourceStart = fieldInfo.getDeclarationSourceStart();
    field.declarationSourceEnd = fieldInfo.getDeclarationSourceEnd();
    int modifiers = fieldInfo.getModifiers();
    boolean isEnumConstant = (modifiers & ClassFileConstants.AccEnum) != 0;
    if (isEnumConstant) {
        // clear AccEnum bit onto AST (binding will add it)
        field.modifiers = modifiers & ~ClassFileConstants.AccEnum;
    } else {
        field.modifiers = modifiers;
        field.type = createTypeReference(fieldInfo.getTypeName(), start, end);
    }
    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        field.annotations = convertAnnotations(fieldHandle);
    }
    /* conversion of field constant */
    if ((this.flags & FIELD_INITIALIZATION) != 0) {
        char[] initializationSource = fieldInfo.getInitializationSource();
        if (initializationSource != null) {
            if (this.parser == null) {
                this.parser = new Parser(this.problemReporter, true);
            }
            this.parser.parse(field, type, this.unit, initializationSource);
        }
    }
    /* conversion of local and anonymous types */
    if ((this.flags & LOCAL_TYPE) != 0) {
        IJavaElement[] children = fieldInfo.getChildren();
        int childrenLength = children.length;
        if (childrenLength == 1) {
            field.initialization = convert(children[0], isEnumConstant ? field : null, compilationResult);
        } else if (childrenLength > 1) {
            ArrayInitializer initializer = new ArrayInitializer();
            field.initialization = initializer;
            Expression[] expressions = new Expression[childrenLength];
            initializer.expressions = expressions;
            for (int i = 0; i < childrenLength; i++) {
                expressions[i] = convert(children[i], isEnumConstant ? field : null, compilationResult);
            }
        }
    }
    return field;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SourceFieldElementInfo(org.eclipse.jdt.internal.core.SourceFieldElementInfo) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) Parser(org.eclipse.jdt.internal.compiler.parser.Parser) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 2 with ArrayInitializer

use of org.eclipse.jdt.internal.compiler.ast.ArrayInitializer 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 3 with ArrayInitializer

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

the class HandleConstructor method createConstructorProperties.

public static Annotation[] createConstructorProperties(ASTNode source, Collection<EclipseNode> fields) {
    if (fields.isEmpty())
        return null;
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    long[] poss = new long[3];
    Arrays.fill(poss, p);
    QualifiedTypeReference constructorPropertiesType = new QualifiedTypeReference(JAVA_BEANS_CONSTRUCTORPROPERTIES, poss);
    setGeneratedBy(constructorPropertiesType, source);
    SingleMemberAnnotation ann = new SingleMemberAnnotation(constructorPropertiesType, pS);
    ann.declarationSourceEnd = pE;
    ArrayInitializer fieldNames = new ArrayInitializer();
    fieldNames.sourceStart = pS;
    fieldNames.sourceEnd = pE;
    fieldNames.expressions = new Expression[fields.size()];
    int ctr = 0;
    for (EclipseNode field : fields) {
        char[] fieldName = removePrefixFromField(field);
        fieldNames.expressions[ctr] = new StringLiteral(fieldName, pS, pE, 0);
        setGeneratedBy(fieldNames.expressions[ctr], source);
        ctr++;
    }
    ann.memberValue = fieldNames;
    setGeneratedBy(ann, source);
    setGeneratedBy(ann.memberValue, source);
    return new Annotation[] { ann };
}
Also used : StringLiteral(org.eclipse.jdt.internal.compiler.ast.StringLiteral) SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) EclipseNode(lombok.eclipse.EclipseNode) SingleMemberAnnotation(org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 4 with ArrayInitializer

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

the class EclipseHandlerUtil method createAnnotation.

/**
 * Provides AnnotationValues with the data it needs to do its thing.
 */
public static <A extends java.lang.annotation.Annotation> AnnotationValues<A> createAnnotation(Class<A> type, final EclipseNode annotationNode) {
    final Annotation annotation = (Annotation) annotationNode.get();
    Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
    MemberValuePair[] memberValuePairs = annotation.memberValuePairs();
    if (memberValuePairs != null)
        for (final MemberValuePair pair : memberValuePairs) {
            List<String> raws = new ArrayList<String>();
            List<Object> expressionValues = new ArrayList<Object>();
            List<Object> guesses = new ArrayList<Object>();
            Expression[] expressions = null;
            char[] n = pair.name;
            String mName = (n == null || n.length == 0) ? "value" : new String(pair.name);
            final Expression rhs = pair.value;
            if (rhs instanceof ArrayInitializer) {
                expressions = ((ArrayInitializer) rhs).expressions;
            } else if (rhs != null) {
                expressions = new Expression[] { rhs };
            }
            if (expressions != null)
                for (Expression ex : expressions) {
                    StringBuffer sb = new StringBuffer();
                    ex.print(0, sb);
                    raws.add(sb.toString());
                    expressionValues.add(ex);
                    guesses.add(calculateValue(ex));
                }
            final Expression[] exprs = expressions;
            values.put(mName, new AnnotationValue(annotationNode, raws, expressionValues, guesses, true) {

                @Override
                public void setError(String message, int valueIdx) {
                    Expression ex;
                    if (valueIdx == -1)
                        ex = rhs;
                    else
                        ex = exprs != null ? exprs[valueIdx] : null;
                    if (ex == null)
                        ex = annotation;
                    int sourceStart = ex.sourceStart;
                    int sourceEnd = ex.sourceEnd;
                    annotationNode.addError(message, sourceStart, sourceEnd);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    Expression ex;
                    if (valueIdx == -1)
                        ex = rhs;
                    else
                        ex = exprs != null ? exprs[valueIdx] : null;
                    if (ex == null)
                        ex = annotation;
                    int sourceStart = ex.sourceStart;
                    int sourceEnd = ex.sourceEnd;
                    annotationNode.addWarning(message, sourceStart, sourceEnd);
                }
            });
        }
    for (Method m : type.getDeclaredMethods()) {
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        String name = m.getName();
        if (!values.containsKey(name)) {
            values.put(name, new AnnotationValue(annotationNode, new ArrayList<String>(), new ArrayList<Object>(), new ArrayList<Object>(), false) {

                @Override
                public void setError(String message, int valueIdx) {
                    annotationNode.addError(message);
                }

                @Override
                public void setWarning(String message, int valueIdx) {
                    annotationNode.addWarning(message);
                }
            });
        }
    }
    return new AnnotationValues<A>(type, values, annotationNode);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) 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) 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) AnnotationValues(lombok.core.AnnotationValues) AnnotationValue(lombok.core.AnnotationValues.AnnotationValue) List(java.util.List) ArrayList(java.util.ArrayList) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 5 with ArrayInitializer

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

the class HandleSneakyThrows method handle.

@Override
public void handle(AnnotationValues<SneakyThrows> annotation, Annotation source, EclipseNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.SNEAKY_THROWS_FLAG_USAGE, "@SneakyThrows");
    List<String> exceptionNames = annotation.getRawExpressions("value");
    List<DeclaredException> exceptions = new ArrayList<DeclaredException>();
    MemberValuePair[] memberValuePairs = source.memberValuePairs();
    if (memberValuePairs == null || memberValuePairs.length == 0) {
        exceptions.add(new DeclaredException("java.lang.Throwable", source));
    } else {
        Expression arrayOrSingle = memberValuePairs[0].value;
        final Expression[] exceptionNameNodes;
        if (arrayOrSingle instanceof ArrayInitializer) {
            exceptionNameNodes = ((ArrayInitializer) arrayOrSingle).expressions;
        } else
            exceptionNameNodes = new Expression[] { arrayOrSingle };
        if (exceptionNames.size() != exceptionNameNodes.length) {
            annotationNode.addError("LOMBOK BUG: The number of exception classes in the annotation isn't the same pre- and post- guessing.");
        }
        int idx = 0;
        for (String exceptionName : exceptionNames) {
            if (exceptionName.endsWith(".class"))
                exceptionName = exceptionName.substring(0, exceptionName.length() - 6);
            exceptions.add(new DeclaredException(exceptionName, exceptionNameNodes[idx++]));
        }
    }
    EclipseNode owner = annotationNode.up();
    switch(owner.getKind()) {
        // return handleField(annotationNode, (FieldDeclaration)owner.get(), exceptions);
        case METHOD:
            handleMethod(annotationNode, (AbstractMethodDeclaration) owner.get(), exceptions);
            break;
        default:
            annotationNode.addError("@SneakyThrows is legal only on methods and constructors.");
    }
}
Also used : MemberValuePair(org.eclipse.jdt.internal.compiler.ast.MemberValuePair) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) ArrayList(java.util.ArrayList) EclipseNode(lombok.eclipse.EclipseNode) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Aggregations

ArrayInitializer (org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)7 ArrayList (java.util.ArrayList)3 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)3 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)3 MemberValuePair (org.eclipse.jdt.internal.compiler.ast.MemberValuePair)3 SingleMemberAnnotation (org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation)3 EclipseNode (lombok.eclipse.EclipseNode)2 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)2 CastExpression (org.eclipse.jdt.internal.compiler.ast.CastExpression)2 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)2 MarkerAnnotation (org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation)2 NormalAnnotation (org.eclipse.jdt.internal.compiler.ast.NormalAnnotation)2 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)2 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AnnotationValues (lombok.core.AnnotationValues)1 AnnotationValue (lombok.core.AnnotationValues.AnnotationValue)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)1