diff options
author | peichhorn <peichhor@web.de> | 2011-08-06 23:36:16 +0200 |
---|---|---|
committer | peichhorn <peichhor@web.de> | 2011-08-06 23:36:16 +0200 |
commit | 6e23117126fddfaa08a9d5b8e1bd482d453475f8 (patch) | |
tree | 92ed673742489c1ba80608edce9d32290f2a4734 /src/core/lombok/eclipse/handlers | |
parent | 87fd16807317d7a3e0f9131a9342b620a9b1a5f0 (diff) | |
download | lombok-6e23117126fddfaa08a9d5b8e1bd482d453475f8.tar.gz lombok-6e23117126fddfaa08a9d5b8e1bd482d453475f8.tar.bz2 lombok-6e23117126fddfaa08a9d5b8e1bd482d453475f8.zip |
fixed Issue 248:
To support the new numeric literal syntax of Java 7, the constructor of IntLiteral got replaced by a factory-method. So we updated the affected handlers to work in both worlds.
Diffstat (limited to 'src/core/lombok/eclipse/handlers')
4 files changed, 62 insertions, 21 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 6dbdfb18..35bf9f93 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -25,6 +25,7 @@ import static lombok.eclipse.Eclipse.*; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; @@ -55,6 +56,7 @@ import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.FieldReference; import org.eclipse.jdt.internal.compiler.ast.IfStatement; +import org.eclipse.jdt.internal.compiler.ast.IntLiteral; import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation; import org.eclipse.jdt.internal.compiler.ast.MemberValuePair; import org.eclipse.jdt.internal.compiler.ast.MessageSend; @@ -663,13 +665,61 @@ public class EclipseHandlerUtil { } @SuppressWarnings("unchecked") - Constructor<CastExpression> constructor_ = (Constructor<CastExpression>) constructor; - castExpressionConstructor = constructor_; + Constructor<CastExpression> castExpressionConstructor_ = (Constructor<CastExpression>) constructor; + castExpressionConstructor = castExpressionConstructor_; castExpressionConstructorIsTypeRefBased = (castExpressionConstructor.getParameterTypes()[1] == TypeReference.class); } + /** + * 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. + * + * @param token + */ + public static IntLiteral makeIntLiteral(char[] token, ASTNode source) { + int pS = source.sourceStart, pE = 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); + } + Eclipse.setGeneratedBy(result, source); + return result; + } + + private static final Constructor<IntLiteral> intLiteralConstructor; + private static final Method intLiteralFactoryMethod; + + static { + Class<?>[] parameterTypes = {char[].class, int.class, int.class}; + Constructor<IntLiteral> intLiteralConstructor_ = null; + Method intLiteralFactoryMethod_ = null; + try { + intLiteralConstructor_ = IntLiteral.class.getConstructor(parameterTypes); + } catch (Exception ignore) { + // probably eclipse 3.7++ + } + try { + intLiteralFactoryMethod_ = IntLiteral.class.getMethod("buildIntLiteral", parameterTypes); + } catch (Exception ignore) { + // probably eclipse versions before 3.7 + } + intLiteralConstructor = intLiteralConstructor_; + intLiteralFactoryMethod = intLiteralFactoryMethod_; + } + private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0]; static Annotation[] getAndRemoveAnnotationParameter(Annotation annotation, String annotationName) { diff --git a/src/core/lombok/eclipse/handlers/HandleCleanup.java b/src/core/lombok/eclipse/handlers/HandleCleanup.java index bbe209d5..21f7ed41 100644 --- a/src/core/lombok/eclipse/handlers/HandleCleanup.java +++ b/src/core/lombok/eclipse/handlers/HandleCleanup.java @@ -22,6 +22,7 @@ package lombok.eclipse.handlers; import static lombok.eclipse.handlers.EclipseHandlerUtil.createNameReference; +import static lombok.eclipse.handlers.EclipseHandlerUtil.makeIntLiteral; import java.util.Arrays; @@ -42,7 +43,6 @@ import org.eclipse.jdt.internal.compiler.ast.CastExpression; import org.eclipse.jdt.internal.compiler.ast.EqualExpression; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.IfStatement; -import org.eclipse.jdt.internal.compiler.ast.IntLiteral; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; import org.eclipse.jdt.internal.compiler.ast.MemberValuePair; import org.eclipse.jdt.internal.compiler.ast.MessageSend; @@ -228,7 +228,7 @@ public class HandleCleanup extends EclipseAnnotationHandler<Cleanup> { preventNullAnalysis.receiver = singletonList; preventNullAnalysis.selector = "get".toCharArray(); - preventNullAnalysis.arguments = new Expression[] { new IntLiteral(new char[] { '0' }, pS, pE) }; + preventNullAnalysis.arguments = new Expression[] { makeIntLiteral("0".toCharArray(), ast) }; preventNullAnalysis.nameSourcePosition = p; preventNullAnalysis.sourceStart = pS; preventNullAnalysis.sourceEnd = singletonList.statementEnd = pE; diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index db45c087..bbb1d56b 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -271,8 +271,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH primeDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); primeDecl.type.sourceStart = pS; primeDecl.type.sourceEnd = pE; Eclipse.setGeneratedBy(primeDecl.type, source); - primeDecl.initialization = new IntLiteral("31".toCharArray(), pS, pE); - Eclipse.setGeneratedBy(primeDecl.initialization, source); + primeDecl.initialization = makeIntLiteral("31".toCharArray(), source); statements.add(primeDecl); } } @@ -280,8 +279,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH /* int result = 1; */ { LocalDeclaration resultDecl = new LocalDeclaration(RESULT, pS, pE); Eclipse.setGeneratedBy(resultDecl, source); - resultDecl.initialization = new IntLiteral("1".toCharArray(), pS, pE); - Eclipse.setGeneratedBy(resultDecl.initialization, source); + resultDecl.initialization = makeIntLiteral("1".toCharArray(), source); resultDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); resultDecl.type.sourceStart = pS; resultDecl.type.sourceEnd = pE; Eclipse.setGeneratedBy(resultDecl.type, source); @@ -337,10 +335,8 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH intoResult.add(longToIntForHashCode(copy1, copy2, source)); } else if (Arrays.equals(TypeConstants.BOOLEAN, token)) { /* booleanField ? 1231 : 1237 */ - IntLiteral int1231 = new IntLiteral("1231".toCharArray(), pS, pE); - Eclipse.setGeneratedBy(int1231, source); - IntLiteral int1237 = new IntLiteral("1237".toCharArray(), pS, pE); - Eclipse.setGeneratedBy(int1237, source); + IntLiteral int1231 = makeIntLiteral("1231".toCharArray(), source); + IntLiteral int1237 = makeIntLiteral("1237".toCharArray(), source); ConditionalExpression int1231or1237 = new ConditionalExpression(fieldAccessor, int1231, int1237); Eclipse.setGeneratedBy(int1231or1237, source); intoResult.add(int1231or1237); @@ -359,8 +355,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH Eclipse.setGeneratedBy(nullLiteral, source); EqualExpression objIsNull = new EqualExpression(fieldAccessor, nullLiteral, OperatorIds.EQUAL_EQUAL); Eclipse.setGeneratedBy(objIsNull, source); - IntLiteral int0 = new IntLiteral("0".toCharArray(), pS, pE); - Eclipse.setGeneratedBy(int0, source); + IntLiteral int0 = makeIntLiteral("0".toCharArray(), source); ConditionalExpression nullOrHashCode = new ConditionalExpression(objIsNull, int0, hashCodeCall); nullOrHashCode.sourceStart = pS; nullOrHashCode.sourceEnd = pE; Eclipse.setGeneratedBy(nullOrHashCode, source); @@ -714,8 +709,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH floatCompare.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.LANG, floatOrDouble); floatCompare.selector = "compare".toCharArray(); floatCompare.arguments = new Expression[] {thisRef, otherRef}; - IntLiteral int0 = new IntLiteral(new char[] {'0'}, pS, pE); - Eclipse.setGeneratedBy(int0, source); + IntLiteral int0 = makeIntLiteral("0".toCharArray(), source); EqualExpression ifFloatCompareIsNot0 = new EqualExpression(floatCompare, int0, OperatorIds.NOT_EQUAL); ifFloatCompareIsNot0.sourceStart = pS; ifFloatCompareIsNot0.sourceEnd = pE; Eclipse.setGeneratedBy(ifFloatCompareIsNot0, source); @@ -732,8 +726,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler<EqualsAndH private Expression longToIntForHashCode(Expression ref1, Expression ref2, ASTNode source) { int pS = source.sourceStart, pE = source.sourceEnd; /* (int)(ref >>> 32 ^ ref) */ - IntLiteral int32 = new IntLiteral("32".toCharArray(), pS, pE); - Eclipse.setGeneratedBy(int32, source); + IntLiteral int32 = makeIntLiteral("32".toCharArray(), source); BinaryExpression higherBits = new BinaryExpression(ref1, int32, OperatorIds.UNSIGNED_RIGHT_SHIFT); Eclipse.setGeneratedBy(higherBits, source); BinaryExpression xorParts = new BinaryExpression(ref2, higherBits, OperatorIds.XOR); diff --git a/src/core/lombok/eclipse/handlers/HandleSynchronized.java b/src/core/lombok/eclipse/handlers/HandleSynchronized.java index 620e7864..3f545966 100644 --- a/src/core/lombok/eclipse/handlers/HandleSynchronized.java +++ b/src/core/lombok/eclipse/handlers/HandleSynchronized.java @@ -39,7 +39,6 @@ import org.eclipse.jdt.internal.compiler.ast.Block; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.FieldReference; -import org.eclipse.jdt.internal.compiler.ast.IntLiteral; import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference; @@ -93,8 +92,7 @@ public class HandleSynchronized extends EclipseAnnotationHandler<Synchronized> { //We use 'new Object[0];' because unlike 'new Object();', empty arrays *ARE* serializable! ArrayAllocationExpression arrayAlloc = new ArrayAllocationExpression(); Eclipse.setGeneratedBy(arrayAlloc, source); - arrayAlloc.dimensions = new Expression[] { new IntLiteral(new char[] { '0' }, 0, 0) }; - Eclipse.setGeneratedBy(arrayAlloc.dimensions[0], source); + arrayAlloc.dimensions = new Expression[] { makeIntLiteral("0".toCharArray(), source) }; arrayAlloc.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 }); Eclipse.setGeneratedBy(arrayAlloc.type, source); fieldDecl.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 }); |