Search in sources :

Example 6 with QualifiedTypeReference

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

the class EclipseJavaUtilListSetSingularizer method generatePluralMethod.

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;
    List<Statement> statements = new ArrayList<Statement>();
    statements.add(createConstructBuilderVarIfNeeded(data, builderType, false));
    FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    MessageSend thisDotFieldDotAddAll = new MessageSend();
    thisDotFieldDotAddAll.arguments = new Expression[] { new SingleNameReference(data.getPluralName(), 0L) };
    thisDotFieldDotAddAll.receiver = thisDotField;
    thisDotFieldDotAddAll.selector = "addAll".toCharArray();
    statements.add(thisDotFieldDotAddAll);
    if (returnStatement != null)
        statements.add(returnStatement);
    md.statements = statements.toArray(new Statement[statements.size()]);
    TypeReference paramType = new QualifiedTypeReference(TypeConstants.JAVA_UTIL_COLLECTION, NULL_POSS);
    paramType = addTypeArgs(1, 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("addAll", new String(data.getPluralName())).toCharArray();
    data.setGeneratedByRecursive(md);
    injectMethod(builderType, md);
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) 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) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayList(java.util.ArrayList) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference)

Example 7 with QualifiedTypeReference

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

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

the class EclipseJavaUtilMapSingularizer method generateFields.

@Override
public List<EclipseNode> generateFields(SingularData data, EclipseNode builderType) {
    if (useGuavaInstead(builderType)) {
        return guavaMapSingularizer.generateFields(data, builderType);
    }
    char[] keyName = (new String(data.getPluralName()) + "$key").toCharArray();
    char[] valueName = (new String(data.getPluralName()) + "$value").toCharArray();
    FieldDeclaration buildKeyField;
    {
        TypeReference type = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS);
        type = addTypeArgs(1, false, builderType, type, data.getTypeArgs());
        buildKeyField = new FieldDeclaration(keyName, 0, -1);
        buildKeyField.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
        buildKeyField.modifiers = ClassFileConstants.AccPrivate;
        buildKeyField.declarationSourceEnd = -1;
        buildKeyField.type = type;
    }
    FieldDeclaration buildValueField;
    {
        TypeReference type = 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();
        type = addTypeArgs(1, false, builderType, type, tArgs);
        buildValueField = new FieldDeclaration(valueName, 0, -1);
        buildValueField.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
        buildValueField.modifiers = ClassFileConstants.AccPrivate;
        buildValueField.declarationSourceEnd = -1;
        buildValueField.type = type;
    }
    data.setGeneratedByRecursive(buildKeyField);
    data.setGeneratedByRecursive(buildValueField);
    EclipseNode keyFieldNode = injectFieldAndMarkGenerated(builderType, buildKeyField);
    EclipseNode valueFieldNode = injectFieldAndMarkGenerated(builderType, buildValueField);
    return Arrays.asList(keyFieldNode, valueFieldNode);
}
Also used : QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) EclipseNode(lombok.eclipse.EclipseNode) ArrayList(java.util.ArrayList) LombokImmutableList(lombok.core.LombokImmutableList) List(java.util.List) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 9 with QualifiedTypeReference

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

the class EclipseJavaUtilSingularizer method createJavaUtilSetMapInitialCapacitySwitchStatements.

protected List<Statement> createJavaUtilSetMapInitialCapacitySwitchStatements(SingularData data, EclipseNode builderType, boolean mapMode, String emptyCollectionMethod, String singletonCollectionMethod, String targetType) {
    List<Statement> switchContents = new ArrayList<Statement>();
    char[] keyName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName();
    if (emptyCollectionMethod != null) {
        // case 0: (empty); break;
        switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '0' }, null), 0, 0));
        /* pluralName = java.util.Collections.emptyCollectionMethod(); */
        {
            MessageSend invoke = new MessageSend();
            invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
            invoke.selector = emptyCollectionMethod.toCharArray();
            switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
        }
        switchContents.add(new BreakStatement(null, 0, 0));
    }
    if (singletonCollectionMethod != null) {
        // case 1: (singleton); break;
        switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '1' }, null), 0, 0));
        /* !mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName.get(0));
			   mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName$key.get(0), this.pluralName$value.get(0)); */
        {
            FieldReference thisDotKey = new FieldReference(keyName, 0L);
            thisDotKey.receiver = new ThisReference(0, 0);
            MessageSend thisDotKeyGet0 = new MessageSend();
            thisDotKeyGet0.receiver = thisDotKey;
            thisDotKeyGet0.selector = new char[] { 'g', 'e', 't' };
            thisDotKeyGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) };
            Expression[] args;
            if (mapMode) {
                char[] valueName = (new String(data.getPluralName()) + "$value").toCharArray();
                FieldReference thisDotValue = new FieldReference(valueName, 0L);
                thisDotValue.receiver = new ThisReference(0, 0);
                MessageSend thisDotValueGet0 = new MessageSend();
                thisDotValueGet0.receiver = thisDotValue;
                thisDotValueGet0.selector = new char[] { 'g', 'e', 't' };
                thisDotValueGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) };
                args = new Expression[] { thisDotKeyGet0, thisDotValueGet0 };
            } else {
                args = new Expression[] { thisDotKeyGet0 };
            }
            MessageSend invoke = new MessageSend();
            invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
            invoke.selector = singletonCollectionMethod.toCharArray();
            invoke.arguments = args;
            switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
        }
        switchContents.add(new BreakStatement(null, 0, 0));
    }
    {
        // default:
        switchContents.add(new CaseStatement(null, 0, 0));
        switchContents.addAll(createJavaUtilSimpleCreationAndFillStatements(data, builderType, mapMode, false, true, emptyCollectionMethod == null, targetType));
    }
    SwitchStatement switchStat = new SwitchStatement();
    switchStat.statements = switchContents.toArray(new Statement[switchContents.size()]);
    switchStat.expression = getSize(builderType, keyName, true);
    TypeReference localShadowerType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()), NULL_POSS);
    localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs());
    LocalDeclaration varDefStat = new LocalDeclaration(data.getPluralName(), 0, 0);
    varDefStat.type = localShadowerType;
    return Arrays.asList(varDefStat, switchStat);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) 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) CaseStatement(org.eclipse.jdt.internal.compiler.ast.CaseStatement) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) Assignment(org.eclipse.jdt.internal.compiler.ast.Assignment) BreakStatement(org.eclipse.jdt.internal.compiler.ast.BreakStatement) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) SwitchStatement(org.eclipse.jdt.internal.compiler.ast.SwitchStatement) 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) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Example 10 with QualifiedTypeReference

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

the class HandleFieldDefaults method visitType.

@Override
public void visitType(EclipseNode typeNode, TypeDeclaration type) {
    AnnotationValues<FieldDefaults> fieldDefaults = null;
    EclipseNode source = typeNode;
    boolean levelIsExplicit = false;
    boolean makeFinalIsExplicit = false;
    FieldDefaults fd = null;
    for (EclipseNode jn : typeNode.down()) {
        if (jn.getKind() != Kind.ANNOTATION)
            continue;
        Annotation ann = (Annotation) jn.get();
        TypeReference typeTree = ann.type;
        if (typeTree == null)
            continue;
        if (typeTree instanceof SingleTypeReference) {
            char[] t = ((SingleTypeReference) typeTree).token;
            if (!Arrays.equals(t, FIELD_DEFAULTS))
                continue;
        } else if (typeTree instanceof QualifiedTypeReference) {
            char[][] t = ((QualifiedTypeReference) typeTree).tokens;
            if (!Eclipse.nameEquals(t, "lombok.experimental.FieldDefaults"))
                continue;
        } else {
            continue;
        }
        if (!typeMatches(FieldDefaults.class, jn, typeTree))
            continue;
        source = jn;
        fieldDefaults = createAnnotation(FieldDefaults.class, jn);
        levelIsExplicit = fieldDefaults.isExplicit("level");
        makeFinalIsExplicit = fieldDefaults.isExplicit("makeFinal");
        handleExperimentalFlagUsage(jn, ConfigurationKeys.FIELD_DEFAULTS_FLAG_USAGE, "@FieldDefaults");
        fd = fieldDefaults.getInstance();
        if (!levelIsExplicit && !makeFinalIsExplicit) {
            jn.addError("This does nothing; provide either level or makeFinal or both.");
        }
        if (levelIsExplicit && fd.level() == AccessLevel.NONE) {
            jn.addError("AccessLevel.NONE doesn't mean anything here. Pick another value.");
            levelIsExplicit = false;
        }
        break;
    }
    if (fd == null && (type.modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0)
        return;
    boolean defaultToPrivate = levelIsExplicit ? false : Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.FIELD_DEFAULTS_PRIVATE_EVERYWHERE));
    boolean defaultToFinal = makeFinalIsExplicit ? false : Boolean.TRUE.equals(typeNode.getAst().readConfiguration(ConfigurationKeys.FIELD_DEFAULTS_FINAL_EVERYWHERE));
    if (!defaultToPrivate && !defaultToFinal && fieldDefaults == null)
        return;
    AccessLevel fdAccessLevel = (fieldDefaults != null && levelIsExplicit) ? fd.level() : defaultToPrivate ? AccessLevel.PRIVATE : null;
    boolean fdToFinal = (fieldDefaults != null && makeFinalIsExplicit) ? fd.makeFinal() : defaultToFinal;
    generateFieldDefaultsForType(typeNode, source, fdAccessLevel, fdToFinal, false);
}
Also used : SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) EclipseNode(lombok.eclipse.EclipseNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) AccessLevel(lombok.AccessLevel) FieldDefaults(lombok.experimental.FieldDefaults)

Aggregations

QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)33 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)26 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)16 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)13 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)12 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)11 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)11 ArrayList (java.util.ArrayList)10 ParameterizedSingleTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference)10 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)10 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)9 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)9 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)9 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)9 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)8 EclipseNode (lombok.eclipse.EclipseNode)7 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)7 ArrayQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference)7 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)7 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)7