Search in sources :

Example 31 with QualifiedTypeReference

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

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

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

the class HandleSynchronized method createLockField.

public char[] createLockField(AnnotationValues<Synchronized> annotation, EclipseNode annotationNode, boolean isStatic, boolean reportErrors) {
    char[] lockName = annotation.getInstance().value().toCharArray();
    Annotation source = (Annotation) annotationNode.get();
    boolean autoMake = false;
    if (lockName.length == 0) {
        autoMake = true;
        lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
    }
    if (fieldExists(new String(lockName), annotationNode) == MemberExistsResult.NOT_EXISTS) {
        if (!autoMake) {
            if (reportErrors)
                annotationNode.addError(String.format("The field %s does not exist.", new String(lockName)));
            return null;
        }
        FieldDeclaration fieldDecl = new FieldDeclaration(lockName, 0, -1);
        setGeneratedBy(fieldDecl, source);
        fieldDecl.declarationSourceEnd = -1;
        fieldDecl.modifiers = (isStatic ? Modifier.STATIC : 0) | Modifier.FINAL | Modifier.PRIVATE;
        //We use 'new Object[0];' because unlike 'new Object();', empty arrays *ARE* serializable!
        ArrayAllocationExpression arrayAlloc = new ArrayAllocationExpression();
        setGeneratedBy(arrayAlloc, source);
        arrayAlloc.dimensions = new Expression[] { makeIntLiteral("0".toCharArray(), source) };
        arrayAlloc.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 });
        setGeneratedBy(arrayAlloc.type, source);
        fieldDecl.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 });
        setGeneratedBy(fieldDecl.type, source);
        fieldDecl.initialization = arrayAlloc;
        // TODO temporary workaround for issue 217. http://code.google.com/p/projectlombok/issues/detail?id=217
        // injectFieldSuppressWarnings(annotationNode.up().up(), fieldDecl);
        injectField(annotationNode.up().up(), fieldDecl);
    }
    return lockName;
}
Also used : ArrayAllocationExpression(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

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