Search in sources :

Example 1 with ForeachStatement

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

the class EclipseJavaUtilMapSingularizer method generatePluralMethod.

private void generatePluralMethod(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;
    String pN = new String(data.getPluralName());
    char[] keyFieldName = (pN + "$key").toCharArray();
    char[] valueFieldName = (pN + "$value").toCharArray();
    List<Statement> statements = new ArrayList<Statement>();
    statements.add(createConstructBuilderVarIfNeeded(data, builderType, true));
    char[] entryName = "$lombokEntry".toCharArray();
    TypeReference forEachType = new QualifiedTypeReference(JAVA_UTIL_MAP_ENTRY, NULL_POSS);
    forEachType = addTypeArgs(2, true, builderType, forEachType, data.getTypeArgs());
    MessageSend keyArg = new MessageSend();
    keyArg.receiver = new SingleNameReference(entryName, 0L);
    keyArg.selector = "getKey".toCharArray();
    MessageSend addKey = new MessageSend();
    FieldReference thisDotKeyField = new FieldReference(keyFieldName, 0L);
    thisDotKeyField.receiver = new ThisReference(0, 0);
    addKey.receiver = thisDotKeyField;
    addKey.selector = new char[] { 'a', 'd', 'd' };
    addKey.arguments = new Expression[] { keyArg };
    MessageSend valueArg = new MessageSend();
    valueArg.receiver = new SingleNameReference(entryName, 0L);
    valueArg.selector = "getValue".toCharArray();
    MessageSend addValue = new MessageSend();
    FieldReference thisDotValueField = new FieldReference(valueFieldName, 0L);
    thisDotValueField.receiver = new ThisReference(0, 0);
    addValue.receiver = thisDotValueField;
    addValue.selector = new char[] { 'a', 'd', 'd' };
    addValue.arguments = new Expression[] { valueArg };
    LocalDeclaration elementVariable = new LocalDeclaration(entryName, 0, 0);
    elementVariable.type = forEachType;
    ForeachStatement forEach = new ForeachStatement(elementVariable, 0);
    MessageSend invokeEntrySet = new MessageSend();
    invokeEntrySet.selector = new char[] { 'e', 'n', 't', 'r', 'y', 'S', 'e', 't' };
    invokeEntrySet.receiver = new SingleNameReference(data.getPluralName(), 0L);
    forEach.collection = invokeEntrySet;
    Block forEachContent = new Block(0);
    forEachContent.statements = new Statement[] { addKey, addValue };
    forEach.action = forEachContent;
    statements.add(forEach);
    if (returnStatement != null)
        statements.add(returnStatement);
    md.statements = statements.toArray(new Statement[statements.size()]);
    TypeReference paramType = new QualifiedTypeReference(JAVA_UTIL_MAP, NULL_POSS);
    paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs());
    Argument param = new Argument(data.getPluralName(), 0, paramType, 0);
    md.arguments = new Argument[] { param };
    md.returnType = returnType;
    md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName("putAll", new String(data.getPluralName())).toCharArray();
    data.setGeneratedByRecursive(md);
    injectMethod(builderType, md);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) 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) 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) ForeachStatement(org.eclipse.jdt.internal.compiler.ast.ForeachStatement)

Example 2 with ForeachStatement

use of org.eclipse.jdt.internal.compiler.ast.ForeachStatement 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;
    }
    if (isVal && localNode.directUp().get() instanceof ForStatement) {
        localNode.addError("'val' is not allowed in old-style for loops");
        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 : 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 3 with ForeachStatement

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

the class PatchValEclipse method copyInitializationOfForEachIterable.

public static void copyInitializationOfForEachIterable(Parser parser) {
    ASTNode[] astStack;
    int astPtr;
    try {
        astStack = (ASTNode[]) Reflection.astStackField.get(parser);
        astPtr = (Integer) Reflection.astPtrField.get(parser);
    } catch (Exception e) {
        // Most likely we're in ecj or some other plugin usage of the eclipse compiler. No need for this.
        return;
    }
    ForeachStatement foreachDecl = (ForeachStatement) astStack[astPtr];
    ASTNode init = foreachDecl.collection;
    if (init == null)
        return;
    boolean val = couldBeVal(foreachDecl.elementVariable.type);
    boolean var = couldBeVar(foreachDecl.elementVariable.type);
    if (foreachDecl.elementVariable == null || !(val || var))
        return;
    try {
        if (Reflection.iterableCopyField != null)
            Reflection.iterableCopyField.set(foreachDecl.elementVariable, init);
    } catch (Exception e) {
    // In ecj mode this field isn't there and we don't need the copy anyway, so, we ignore the exception.
    }
}
Also used : ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) InvocationTargetException(java.lang.reflect.InvocationTargetException) ForeachStatement(org.eclipse.jdt.internal.compiler.ast.ForeachStatement)

Aggregations

ForeachStatement (org.eclipse.jdt.internal.compiler.ast.ForeachStatement)3 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)1 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)1 ArrayInitializer (org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)1 Block (org.eclipse.jdt.internal.compiler.ast.Block)1 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)1 ForStatement (org.eclipse.jdt.internal.compiler.ast.ForStatement)1 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)1 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)1 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)1 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)1 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)1 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)1 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)1 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)1 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)1 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)1