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.
This commit is contained in:
peichhorn committed 2011-08-06 23:36:16 +02:00
1 parent 87fd168073
commit 6e23117126
4 files changed
+62 -21

No files matched your search

@@ -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) {
@@ -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;
@@ -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);
@@ -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 });