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);
}
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;
}
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;
}
Aggregations