Search in sources :

Example 1 with IntLiteral

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

the class HandleEqualsAndHashCode method generateCompareFloatOrDouble.

public IfStatement generateCompareFloatOrDouble(Expression thisRef, Expression otherRef, char[] floatOrDouble, ASTNode source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    /* if (Float.compare(fieldName, other.fieldName) != 0) return false */
    MessageSend floatCompare = new MessageSend();
    floatCompare.sourceStart = pS;
    floatCompare.sourceEnd = pE;
    setGeneratedBy(floatCompare, source);
    floatCompare.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.LANG, floatOrDouble);
    floatCompare.selector = "compare".toCharArray();
    floatCompare.arguments = new Expression[] { thisRef, otherRef };
    IntLiteral int0 = makeIntLiteral("0".toCharArray(), source);
    EqualExpression ifFloatCompareIsNot0 = new EqualExpression(floatCompare, int0, OperatorIds.NOT_EQUAL);
    ifFloatCompareIsNot0.sourceStart = pS;
    ifFloatCompareIsNot0.sourceEnd = pE;
    setGeneratedBy(ifFloatCompareIsNot0, source);
    FalseLiteral falseLiteral = new FalseLiteral(pS, pE);
    setGeneratedBy(falseLiteral, source);
    ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE);
    setGeneratedBy(returnFalse, source);
    IfStatement ifStatement = new IfStatement(ifFloatCompareIsNot0, returnFalse, pS, pE);
    setGeneratedBy(ifStatement, source);
    return ifStatement;
}
Also used : MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) IntLiteral(org.eclipse.jdt.internal.compiler.ast.IntLiteral) FalseLiteral(org.eclipse.jdt.internal.compiler.ast.FalseLiteral)

Example 2 with IntLiteral

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

the class EclipseHandlerUtil method makeIntLiteral.

/**
 * In eclipse 3.7+, IntLiterals are created using a factory-method
 * Unfortunately that means we need to use reflection as we want to be compatible
 * with eclipse versions before 3.7.
 */
public static IntLiteral makeIntLiteral(char[] token, ASTNode source) {
    int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd;
    IntLiteral result;
    try {
        if (intLiteralConstructor != null) {
            result = intLiteralConstructor.newInstance(token, pS, pE);
        } else {
            result = (IntLiteral) intLiteralFactoryMethod.invoke(null, token, pS, pE);
        }
    } catch (InvocationTargetException e) {
        throw Lombok.sneakyThrow(e.getCause());
    } catch (IllegalAccessException e) {
        throw Lombok.sneakyThrow(e);
    } catch (InstantiationException e) {
        throw Lombok.sneakyThrow(e);
    }
    if (source != null)
        setGeneratedBy(result, source);
    return result;
}
Also used : IntLiteral(org.eclipse.jdt.internal.compiler.ast.IntLiteral) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with IntLiteral

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

the class HandleEqualsAndHashCode method createHashCode.

public MethodDeclaration createHashCode(EclipseNode type, Collection<EclipseNode> fields, boolean callSuper, ASTNode source, FieldAccess fieldAccess) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    MethodDeclaration method = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
    setGeneratedBy(method, source);
    method.modifiers = toEclipseModifier(AccessLevel.PUBLIC);
    method.returnType = TypeReference.baseTypeReference(TypeIds.T_int, 0);
    setGeneratedBy(method.returnType, source);
    method.annotations = new Annotation[] { makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source) };
    method.selector = "hashCode".toCharArray();
    method.thrownExceptions = null;
    method.typeParameters = null;
    method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
    method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
    method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
    method.arguments = null;
    List<Statement> statements = new ArrayList<Statement>();
    final boolean isEmpty = fields.isEmpty();
    /* final int PRIME = X; */
    {
        /* Without fields, PRIME isn't used, and that would trigger a 'local variable not used' warning. */
        if (!isEmpty) {
            LocalDeclaration primeDecl = new LocalDeclaration(PRIME, pS, pE);
            setGeneratedBy(primeDecl, source);
            primeDecl.modifiers |= Modifier.FINAL;
            primeDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
            primeDecl.type.sourceStart = pS;
            primeDecl.type.sourceEnd = pE;
            setGeneratedBy(primeDecl.type, source);
            primeDecl.initialization = makeIntLiteral(String.valueOf(HandlerUtil.primeForHashcode()).toCharArray(), source);
            statements.add(primeDecl);
        }
    }
    /*int result = ... */
    {
        LocalDeclaration resultDecl = new LocalDeclaration(RESULT, pS, pE);
        setGeneratedBy(resultDecl, source);
        final Expression init;
        if (callSuper) {
            /* ... super.hashCode(); */
            MessageSend callToSuper = new MessageSend();
            setGeneratedBy(callToSuper, source);
            callToSuper.sourceStart = pS;
            callToSuper.sourceEnd = pE;
            callToSuper.receiver = new SuperReference(pS, pE);
            setGeneratedBy(callToSuper.receiver, source);
            callToSuper.selector = "hashCode".toCharArray();
            init = callToSuper;
        } else {
            /* ... 1; */
            init = makeIntLiteral("1".toCharArray(), source);
        }
        resultDecl.initialization = init;
        resultDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
        resultDecl.type.sourceStart = pS;
        resultDecl.type.sourceEnd = pE;
        setGeneratedBy(resultDecl.type, source);
        statements.add(resultDecl);
    }
    for (EclipseNode field : fields) {
        TypeReference fType = getFieldType(field, fieldAccess);
        char[] dollarFieldName = ("$" + field.getName()).toCharArray();
        char[] token = fType.getLastToken();
        Expression fieldAccessor = createFieldAccessor(field, fieldAccess, source);
        if (fType.dimensions() == 0 && token != null) {
            if (Arrays.equals(TypeConstants.BOOLEAN, token)) {
                /* booleanField ? X : Y */
                IntLiteral intTrue = makeIntLiteral(String.valueOf(HandlerUtil.primeForTrue()).toCharArray(), source);
                IntLiteral intFalse = makeIntLiteral(String.valueOf(HandlerUtil.primeForFalse()).toCharArray(), source);
                ConditionalExpression intForBool = new ConditionalExpression(fieldAccessor, intTrue, intFalse);
                setGeneratedBy(intForBool, source);
                statements.add(createResultCalculation(source, intForBool));
            } else if (Arrays.equals(TypeConstants.LONG, token)) {
                statements.add(createLocalDeclaration(source, dollarFieldName, TypeReference.baseTypeReference(TypeIds.T_long, 0), fieldAccessor));
                SingleNameReference copy1 = new SingleNameReference(dollarFieldName, p);
                setGeneratedBy(copy1, source);
                SingleNameReference copy2 = new SingleNameReference(dollarFieldName, p);
                setGeneratedBy(copy2, source);
                statements.add(createResultCalculation(source, longToIntForHashCode(copy1, copy2, source)));
            } else if (Arrays.equals(TypeConstants.FLOAT, token)) {
                /* Float.floatToIntBits(fieldName) */
                MessageSend floatToIntBits = new MessageSend();
                floatToIntBits.sourceStart = pS;
                floatToIntBits.sourceEnd = pE;
                setGeneratedBy(floatToIntBits, source);
                floatToIntBits.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_FLOAT);
                floatToIntBits.selector = "floatToIntBits".toCharArray();
                floatToIntBits.arguments = new Expression[] { fieldAccessor };
                statements.add(createResultCalculation(source, floatToIntBits));
            } else if (Arrays.equals(TypeConstants.DOUBLE, token)) {
                /* longToIntForHashCode(Double.doubleToLongBits(fieldName)) */
                MessageSend doubleToLongBits = new MessageSend();
                doubleToLongBits.sourceStart = pS;
                doubleToLongBits.sourceEnd = pE;
                setGeneratedBy(doubleToLongBits, source);
                doubleToLongBits.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_DOUBLE);
                doubleToLongBits.selector = "doubleToLongBits".toCharArray();
                doubleToLongBits.arguments = new Expression[] { fieldAccessor };
                statements.add(createLocalDeclaration(source, dollarFieldName, TypeReference.baseTypeReference(TypeIds.T_long, 0), doubleToLongBits));
                SingleNameReference copy1 = new SingleNameReference(dollarFieldName, p);
                setGeneratedBy(copy1, source);
                SingleNameReference copy2 = new SingleNameReference(dollarFieldName, p);
                setGeneratedBy(copy2, source);
                statements.add(createResultCalculation(source, longToIntForHashCode(copy1, copy2, source)));
            } else if (BUILT_IN_TYPES.contains(new String(token))) {
                statements.add(createResultCalculation(source, fieldAccessor));
            } else /* objects */
            {
                /* final java.lang.Object $fieldName = this.fieldName; */
                /* $fieldName == null ? NULL_PRIME : $fieldName.hashCode() */
                statements.add(createLocalDeclaration(source, dollarFieldName, generateQualifiedTypeRef(source, TypeConstants.JAVA_LANG_OBJECT), fieldAccessor));
                SingleNameReference copy1 = new SingleNameReference(dollarFieldName, p);
                setGeneratedBy(copy1, source);
                SingleNameReference copy2 = new SingleNameReference(dollarFieldName, p);
                setGeneratedBy(copy2, source);
                MessageSend hashCodeCall = new MessageSend();
                hashCodeCall.sourceStart = pS;
                hashCodeCall.sourceEnd = pE;
                setGeneratedBy(hashCodeCall, source);
                hashCodeCall.receiver = copy1;
                hashCodeCall.selector = "hashCode".toCharArray();
                NullLiteral nullLiteral = new NullLiteral(pS, pE);
                setGeneratedBy(nullLiteral, source);
                EqualExpression objIsNull = new EqualExpression(copy2, nullLiteral, OperatorIds.EQUAL_EQUAL);
                setGeneratedBy(objIsNull, source);
                IntLiteral intMagic = makeIntLiteral(String.valueOf(HandlerUtil.primeForNull()).toCharArray(), source);
                ConditionalExpression nullOrHashCode = new ConditionalExpression(objIsNull, intMagic, hashCodeCall);
                nullOrHashCode.sourceStart = pS;
                nullOrHashCode.sourceEnd = pE;
                setGeneratedBy(nullOrHashCode, source);
                statements.add(createResultCalculation(source, nullOrHashCode));
            }
        } else if (fType.dimensions() > 0 && token != null) {
            /* Arrays.deepHashCode(array)  //just hashCode for simple arrays */
            MessageSend arraysHashCodeCall = new MessageSend();
            arraysHashCodeCall.sourceStart = pS;
            arraysHashCodeCall.sourceEnd = pE;
            setGeneratedBy(arraysHashCodeCall, source);
            arraysHashCodeCall.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray());
            if (fType.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(token))) {
                arraysHashCodeCall.selector = "deepHashCode".toCharArray();
            } else {
                arraysHashCodeCall.selector = "hashCode".toCharArray();
            }
            arraysHashCodeCall.arguments = new Expression[] { fieldAccessor };
            statements.add(createResultCalculation(source, arraysHashCodeCall));
        }
    }
    /* return result; */
    {
        SingleNameReference resultRef = new SingleNameReference(RESULT, p);
        setGeneratedBy(resultRef, source);
        ReturnStatement returnStatement = new ReturnStatement(resultRef, pS, pE);
        setGeneratedBy(returnStatement, source);
        statements.add(returnStatement);
    }
    method.statements = statements.toArray(new Statement[statements.size()]);
    return method;
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) ArrayList(java.util.ArrayList) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) SuperReference(org.eclipse.jdt.internal.compiler.ast.SuperReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) InstanceOfExpression(org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression) UnaryExpression(org.eclipse.jdt.internal.compiler.ast.UnaryExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) EclipseNode(lombok.eclipse.EclipseNode) IntLiteral(org.eclipse.jdt.internal.compiler.ast.IntLiteral) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral)

Example 4 with IntLiteral

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

the class HandleEqualsAndHashCode method longToIntForHashCode.

/**
 * Give 2 clones!
 */
public Expression longToIntForHashCode(Expression ref1, Expression ref2, ASTNode source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    /* (int)(ref >>> 32 ^ ref) */
    IntLiteral int32 = makeIntLiteral("32".toCharArray(), source);
    BinaryExpression higherBits = new BinaryExpression(ref1, int32, OperatorIds.UNSIGNED_RIGHT_SHIFT);
    setGeneratedBy(higherBits, source);
    BinaryExpression xorParts = new BinaryExpression(ref2, higherBits, OperatorIds.XOR);
    setGeneratedBy(xorParts, source);
    TypeReference intRef = TypeReference.baseTypeReference(TypeIds.T_int, 0);
    intRef.sourceStart = pS;
    intRef.sourceEnd = pE;
    setGeneratedBy(intRef, source);
    CastExpression expr = makeCastExpression(xorParts, intRef, source);
    expr.sourceStart = pS;
    expr.sourceEnd = pE;
    return expr;
}
Also used : BinaryExpression(org.eclipse.jdt.internal.compiler.ast.BinaryExpression) IntLiteral(org.eclipse.jdt.internal.compiler.ast.IntLiteral) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) CastExpression(org.eclipse.jdt.internal.compiler.ast.CastExpression)

Aggregations

IntLiteral (org.eclipse.jdt.internal.compiler.ast.IntLiteral)4 BinaryExpression (org.eclipse.jdt.internal.compiler.ast.BinaryExpression)2 CastExpression (org.eclipse.jdt.internal.compiler.ast.CastExpression)2 EqualExpression (org.eclipse.jdt.internal.compiler.ast.EqualExpression)2 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)2 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)2 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)2 ParameterizedSingleTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference)2 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)2 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)2 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)2 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 EclipseNode (lombok.eclipse.EclipseNode)1 ConditionalExpression (org.eclipse.jdt.internal.compiler.ast.ConditionalExpression)1 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)1 FalseLiteral (org.eclipse.jdt.internal.compiler.ast.FalseLiteral)1 InstanceOfExpression (org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression)1 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)1