use of org.eclipse.jdt.internal.compiler.ast.Expression in project lombok by rzwitserloot.
the class EclipseJavaUtilMapSingularizer method generateSingularMethod.
private void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
md.modifiers = ClassFileConstants.AccPublic;
List<Statement> statements = new ArrayList<Statement>();
statements.add(createConstructBuilderVarIfNeeded(data, builderType, true));
String sN = new String(data.getSingularName());
String pN = new String(data.getPluralName());
char[] keyParamName = (sN + "Key").toCharArray();
char[] valueParamName = (sN + "Value").toCharArray();
char[] keyFieldName = (pN + "$key").toCharArray();
char[] valueFieldName = (pN + "$value").toCharArray();
/* this.pluralname$key.add(singularnameKey); */
{
FieldReference thisDotKeyField = new FieldReference(keyFieldName, 0L);
thisDotKeyField.receiver = new ThisReference(0, 0);
MessageSend thisDotKeyFieldDotAdd = new MessageSend();
thisDotKeyFieldDotAdd.arguments = new Expression[] { new SingleNameReference(keyParamName, 0L) };
thisDotKeyFieldDotAdd.receiver = thisDotKeyField;
thisDotKeyFieldDotAdd.selector = "add".toCharArray();
statements.add(thisDotKeyFieldDotAdd);
}
/* this.pluralname$value.add(singularnameValue); */
{
FieldReference thisDotValueField = new FieldReference(valueFieldName, 0L);
thisDotValueField.receiver = new ThisReference(0, 0);
MessageSend thisDotValueFieldDotAdd = new MessageSend();
thisDotValueFieldDotAdd.arguments = new Expression[] { new SingleNameReference(valueParamName, 0L) };
thisDotValueFieldDotAdd.receiver = thisDotValueField;
thisDotValueFieldDotAdd.selector = "add".toCharArray();
statements.add(thisDotValueFieldDotAdd);
}
if (returnStatement != null)
statements.add(returnStatement);
md.statements = statements.toArray(new Statement[statements.size()]);
TypeReference keyParamType = cloneParamType(0, data.getTypeArgs(), builderType);
Argument keyParam = new Argument(keyParamName, 0, keyParamType, 0);
TypeReference valueParamType = cloneParamType(1, data.getTypeArgs(), builderType);
Argument valueParam = new Argument(valueParamName, 0, valueParamType, 0);
md.arguments = new Argument[] { keyParam, valueParam };
md.returnType = returnType;
md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("put", new String(data.getSingularName())).toCharArray();
data.setGeneratedByRecursive(md);
injectMethod(builderType, md);
}
use of org.eclipse.jdt.internal.compiler.ast.Expression 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.Expression in project lombok by rzwitserloot.
the class EclipseHandlerUtil method createAnnotation.
/**
* Provides AnnotationValues with the data it needs to do its thing.
*/
public static <A extends java.lang.annotation.Annotation> AnnotationValues<A> createAnnotation(Class<A> type, final EclipseNode annotationNode) {
final Annotation annotation = (Annotation) annotationNode.get();
Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
MemberValuePair[] memberValuePairs = annotation.memberValuePairs();
if (memberValuePairs != null)
for (final MemberValuePair pair : memberValuePairs) {
List<String> raws = new ArrayList<String>();
List<Object> expressionValues = new ArrayList<Object>();
List<Object> guesses = new ArrayList<Object>();
Expression[] expressions = null;
char[] n = pair.name;
String mName = (n == null || n.length == 0) ? "value" : new String(pair.name);
final Expression rhs = pair.value;
if (rhs instanceof ArrayInitializer) {
expressions = ((ArrayInitializer) rhs).expressions;
} else if (rhs != null) {
expressions = new Expression[] { rhs };
}
if (expressions != null)
for (Expression ex : expressions) {
StringBuffer sb = new StringBuffer();
ex.print(0, sb);
raws.add(sb.toString());
expressionValues.add(ex);
guesses.add(calculateValue(ex));
}
final Expression[] exprs = expressions;
values.put(mName, new AnnotationValue(annotationNode, raws, expressionValues, guesses, true) {
@Override
public void setError(String message, int valueIdx) {
Expression ex;
if (valueIdx == -1)
ex = rhs;
else
ex = exprs != null ? exprs[valueIdx] : null;
if (ex == null)
ex = annotation;
int sourceStart = ex.sourceStart;
int sourceEnd = ex.sourceEnd;
annotationNode.addError(message, sourceStart, sourceEnd);
}
@Override
public void setWarning(String message, int valueIdx) {
Expression ex;
if (valueIdx == -1)
ex = rhs;
else
ex = exprs != null ? exprs[valueIdx] : null;
if (ex == null)
ex = annotation;
int sourceStart = ex.sourceStart;
int sourceEnd = ex.sourceEnd;
annotationNode.addWarning(message, sourceStart, sourceEnd);
}
});
}
for (Method m : type.getDeclaredMethods()) {
if (!Modifier.isPublic(m.getModifiers()))
continue;
String name = m.getName();
if (!values.containsKey(name)) {
values.put(name, new AnnotationValue(annotationNode, new ArrayList<String>(), new ArrayList<Object>(), new ArrayList<Object>(), false) {
@Override
public void setError(String message, int valueIdx) {
annotationNode.addError(message);
}
@Override
public void setWarning(String message, int valueIdx) {
annotationNode.addWarning(message);
}
});
}
}
return new AnnotationValues<A>(type, values, annotationNode);
}
use of org.eclipse.jdt.internal.compiler.ast.Expression 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.Expression in project lombok by rzwitserloot.
the class HandleSneakyThrows method handle.
@Override
public void handle(AnnotationValues<SneakyThrows> annotation, Annotation source, EclipseNode annotationNode) {
handleFlagUsage(annotationNode, ConfigurationKeys.SNEAKY_THROWS_FLAG_USAGE, "@SneakyThrows");
List<String> exceptionNames = annotation.getRawExpressions("value");
List<DeclaredException> exceptions = new ArrayList<DeclaredException>();
MemberValuePair[] memberValuePairs = source.memberValuePairs();
if (memberValuePairs == null || memberValuePairs.length == 0) {
exceptions.add(new DeclaredException("java.lang.Throwable", source));
} else {
Expression arrayOrSingle = memberValuePairs[0].value;
final Expression[] exceptionNameNodes;
if (arrayOrSingle instanceof ArrayInitializer) {
exceptionNameNodes = ((ArrayInitializer) arrayOrSingle).expressions;
} else
exceptionNameNodes = new Expression[] { arrayOrSingle };
if (exceptionNames.size() != exceptionNameNodes.length) {
annotationNode.addError("LOMBOK BUG: The number of exception classes in the annotation isn't the same pre- and post- guessing.");
}
int idx = 0;
for (String exceptionName : exceptionNames) {
if (exceptionName.endsWith(".class"))
exceptionName = exceptionName.substring(0, exceptionName.length() - 6);
exceptions.add(new DeclaredException(exceptionName, exceptionNameNodes[idx++]));
}
}
EclipseNode owner = annotationNode.up();
switch(owner.getKind()) {
// return handleField(annotationNode, (FieldDeclaration)owner.get(), exceptions);
case METHOD:
handleMethod(annotationNode, (AbstractMethodDeclaration) owner.get(), exceptions);
break;
default:
annotationNode.addError("@SneakyThrows is legal only on methods and constructors.");
}
}
Aggregations