Search in sources :

Example 11 with QualifiedNameReference

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

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

the class EclipseHandlerUtil method createFieldAccessor.

static Expression createFieldAccessor(EclipseNode field, FieldAccess fieldAccess, ASTNode source, char[] receiver) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    boolean lookForGetter = lookForGetter(field, fieldAccess);
    GetterMethod getter = lookForGetter ? findGetter(field) : null;
    if (getter == null) {
        NameReference ref;
        char[][] tokens = new char[2][];
        tokens[0] = receiver;
        tokens[1] = field.getName().toCharArray();
        long[] poss = { p, p };
        ref = new QualifiedNameReference(tokens, poss, pS, pE);
        setGeneratedBy(ref, source);
        return ref;
    }
    MessageSend call = new MessageSend();
    setGeneratedBy(call, source);
    call.sourceStart = pS;
    call.statementEnd = call.sourceEnd = pE;
    call.receiver = new SingleNameReference(receiver, p);
    setGeneratedBy(call.receiver, source);
    call.selector = getter.name;
    return call;
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference) NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Aggregations

QualifiedNameReference (org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)12 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)8 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)7 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)7 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)6 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)6 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)6 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)5 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)5 AllocationExpression (org.eclipse.jdt.internal.compiler.ast.AllocationExpression)4 Assignment (org.eclipse.jdt.internal.compiler.ast.Assignment)4 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)4 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)4 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)4 BreakStatement (org.eclipse.jdt.internal.compiler.ast.BreakStatement)3 CaseStatement (org.eclipse.jdt.internal.compiler.ast.CaseStatement)3 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)3 NameReference (org.eclipse.jdt.internal.compiler.ast.NameReference)3 NullLiteral (org.eclipse.jdt.internal.compiler.ast.NullLiteral)3 SwitchStatement (org.eclipse.jdt.internal.compiler.ast.SwitchStatement)3