Search in sources :

Example 26 with Expression

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

the class EclipseJavaUtilMapSingularizer method generateSingularMethod.

private void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    List<Statement> statements = new ArrayList<Statement>();
    statements.add(createConstructBuilderVarIfNeeded(data, builderType, true));
    String sN = new String(data.getSingularName());
    String pN = new String(data.getPluralName());
    char[] keyParamName = (sN + "Key").toCharArray();
    char[] valueParamName = (sN + "Value").toCharArray();
    char[] keyFieldName = (pN + "$key").toCharArray();
    char[] valueFieldName = (pN + "$value").toCharArray();
    /* this.pluralname$key.add(singularnameKey); */
    {
        FieldReference thisDotKeyField = new FieldReference(keyFieldName, 0L);
        thisDotKeyField.receiver = new ThisReference(0, 0);
        MessageSend thisDotKeyFieldDotAdd = new MessageSend();
        thisDotKeyFieldDotAdd.arguments = new Expression[] { new SingleNameReference(keyParamName, 0L) };
        thisDotKeyFieldDotAdd.receiver = thisDotKeyField;
        thisDotKeyFieldDotAdd.selector = "add".toCharArray();
        statements.add(thisDotKeyFieldDotAdd);
    }
    /* this.pluralname$value.add(singularnameValue); */
    {
        FieldReference thisDotValueField = new FieldReference(valueFieldName, 0L);
        thisDotValueField.receiver = new ThisReference(0, 0);
        MessageSend thisDotValueFieldDotAdd = new MessageSend();
        thisDotValueFieldDotAdd.arguments = new Expression[] { new SingleNameReference(valueParamName, 0L) };
        thisDotValueFieldDotAdd.receiver = thisDotValueField;
        thisDotValueFieldDotAdd.selector = "add".toCharArray();
        statements.add(thisDotValueFieldDotAdd);
    }
    if (returnStatement != null)
        statements.add(returnStatement);
    md.statements = statements.toArray(new Statement[statements.size()]);
    TypeReference keyParamType = cloneParamType(0, data.getTypeArgs(), builderType);
    Argument keyParam = new Argument(keyParamName, 0, keyParamType, 0);
    TypeReference valueParamType = cloneParamType(1, data.getTypeArgs(), builderType);
    Argument valueParam = new Argument(valueParamName, 0, valueParamType, 0);
    md.arguments = new Argument[] { keyParam, valueParam };
    md.returnType = returnType;
    md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("put", new String(data.getSingularName())).toCharArray();
    data.setGeneratedByRecursive(md);
    injectMethod(builderType, md);
}
Also used : FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ForeachStatement(org.eclipse.jdt.internal.compiler.ast.ForeachStatement) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)

Example 27 with Expression

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

the class EclipseJavaUtilSingularizer method createConstructBuilderVarIfNeeded.

protected Statement createConstructBuilderVarIfNeeded(SingularData data, EclipseNode builderType, boolean mapMode) {
    char[] v1Name, v2Name;
    if (mapMode) {
        String n = new String(data.getPluralName());
        v1Name = (n + "$key").toCharArray();
        v2Name = (n + "$value").toCharArray();
    } else {
        v1Name = data.getPluralName();
        v2Name = null;
    }
    FieldReference thisDotField = new FieldReference(v1Name, 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    Expression cond = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL);
    thisDotField = new FieldReference(v1Name, 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    TypeReference v1Type = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS);
    v1Type = addTypeArgs(1, false, builderType, v1Type, data.getTypeArgs());
    AllocationExpression constructArrayList = new AllocationExpression();
    constructArrayList.type = v1Type;
    Assignment initV1 = new Assignment(thisDotField, constructArrayList, 0);
    Statement thenPart;
    if (mapMode) {
        thisDotField = new FieldReference(v2Name, 0L);
        thisDotField.receiver = new ThisReference(0, 0);
        TypeReference v2Type = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS);
        List<TypeReference> tArgs = data.getTypeArgs();
        if (tArgs != null && tArgs.size() > 1)
            tArgs = Collections.singletonList(tArgs.get(1));
        else
            tArgs = Collections.emptyList();
        v2Type = addTypeArgs(1, false, builderType, v2Type, tArgs);
        constructArrayList = new AllocationExpression();
        constructArrayList.type = v2Type;
        Assignment initV2 = new Assignment(thisDotField, constructArrayList, 0);
        Block b = new Block(0);
        b.statements = new Statement[] { initV1, initV2 };
        thenPart = b;
    } else {
        thenPart = initV1;
    }
    return new IfStatement(cond, thenPart, 0, 0);
}
Also used : FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) BreakStatement(org.eclipse.jdt.internal.compiler.ast.BreakStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) ForStatement(org.eclipse.jdt.internal.compiler.ast.ForStatement) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) PostfixExpression(org.eclipse.jdt.internal.compiler.ast.PostfixExpression) BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) AllocationExpression(org.eclipse.jdt.internal.compiler.ast.AllocationExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) Block(org.eclipse.jdt.internal.compiler.ast.Block) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Example 28 with Expression

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

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

the class EclipseHandlerUtil method makeCastExpression.

/**
	 * In eclipse 3.7+, the CastExpression constructor was changed from a really weird version to
	 * a less weird one. Unfortunately that means we need to use reflection as we want to be compatible
	 * with eclipse versions before 3.7 and 3.7+.
	 * 
	 * @param ref The {@code foo} in {@code (String)foo}.
	 * @param castTo The {@code String} in {@code (String)foo}.
	 */
public static CastExpression makeCastExpression(Expression ref, TypeReference castTo, ASTNode source) {
    CastExpression result;
    try {
        if (castExpressionConstructorIsTypeRefBased) {
            result = castExpressionConstructor.newInstance(ref, castTo);
        } else {
            Expression castToConverted = castTo;
            if (castTo.getClass() == SingleTypeReference.class && !isPrimitive(castTo)) {
                SingleTypeReference str = (SingleTypeReference) castTo;
                //Why a SingleNameReference instead of a SingleTypeReference you ask? I don't know. It seems dumb. Ask the ecj guys.
                castToConverted = new SingleNameReference(str.token, 0);
                castToConverted.bits = (castToConverted.bits & ~Binding.VARIABLE) | Binding.TYPE;
                castToConverted.sourceStart = str.sourceStart;
                castToConverted.sourceEnd = str.sourceEnd;
                setGeneratedBy(castToConverted, source);
            } else if (castTo.getClass() == QualifiedTypeReference.class) {
                QualifiedTypeReference qtr = (QualifiedTypeReference) castTo;
                //Same here, but for the more complex types, they stay types.
                castToConverted = new QualifiedNameReference(qtr.tokens, copy(qtr.sourcePositions), qtr.sourceStart, qtr.sourceEnd);
                castToConverted.bits = (castToConverted.bits & ~Binding.VARIABLE) | Binding.TYPE;
                setGeneratedBy(castToConverted, source);
            }
            result = castExpressionConstructor.newInstance(ref, castToConverted);
        }
    } catch (InvocationTargetException e) {
        throw Lombok.sneakyThrow(e.getCause());
    } catch (IllegalAccessException e) {
        throw Lombok.sneakyThrow(e);
    } catch (InstantiationException e) {
        throw Lombok.sneakyThrow(e);
    }
    result.sourceStart = source.sourceStart;
    result.sourceEnd = source.sourceEnd;
    result.statementEnd = source.sourceEnd;
    setGeneratedBy(result, source);
    return result;
}
Also used : 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) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 30 with Expression

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

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