use of org.eclipse.jdt.internal.compiler.ast.FieldReference in project lombok by rzwitserloot.
the class EclipseJavaUtilMapSingularizer method generatePluralMethod.
private void generatePluralMethod(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;
String pN = new String(data.getPluralName());
char[] keyFieldName = (pN + "$key").toCharArray();
char[] valueFieldName = (pN + "$value").toCharArray();
List<Statement> statements = new ArrayList<Statement>();
statements.add(createConstructBuilderVarIfNeeded(data, builderType, true));
char[] entryName = "$lombokEntry".toCharArray();
TypeReference forEachType = new QualifiedTypeReference(JAVA_UTIL_MAP_ENTRY, NULL_POSS);
forEachType = addTypeArgs(2, true, builderType, forEachType, data.getTypeArgs());
MessageSend keyArg = new MessageSend();
keyArg.receiver = new SingleNameReference(entryName, 0L);
keyArg.selector = "getKey".toCharArray();
MessageSend addKey = new MessageSend();
FieldReference thisDotKeyField = new FieldReference(keyFieldName, 0L);
thisDotKeyField.receiver = new ThisReference(0, 0);
addKey.receiver = thisDotKeyField;
addKey.selector = new char[] { 'a', 'd', 'd' };
addKey.arguments = new Expression[] { keyArg };
MessageSend valueArg = new MessageSend();
valueArg.receiver = new SingleNameReference(entryName, 0L);
valueArg.selector = "getValue".toCharArray();
MessageSend addValue = new MessageSend();
FieldReference thisDotValueField = new FieldReference(valueFieldName, 0L);
thisDotValueField.receiver = new ThisReference(0, 0);
addValue.receiver = thisDotValueField;
addValue.selector = new char[] { 'a', 'd', 'd' };
addValue.arguments = new Expression[] { valueArg };
LocalDeclaration elementVariable = new LocalDeclaration(entryName, 0, 0);
elementVariable.type = forEachType;
ForeachStatement forEach = new ForeachStatement(elementVariable, 0);
MessageSend invokeEntrySet = new MessageSend();
invokeEntrySet.selector = new char[] { 'e', 'n', 't', 'r', 'y', 'S', 'e', 't' };
invokeEntrySet.receiver = new SingleNameReference(data.getPluralName(), 0L);
forEach.collection = invokeEntrySet;
Block forEachContent = new Block(0);
forEachContent.statements = new Statement[] { addKey, addValue };
forEach.action = forEachContent;
statements.add(forEach);
if (returnStatement != null)
statements.add(returnStatement);
md.statements = statements.toArray(new Statement[statements.size()]);
TypeReference paramType = new QualifiedTypeReference(JAVA_UTIL_MAP, NULL_POSS);
paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs());
Argument param = new Argument(data.getPluralName(), 0, paramType, 0);
md.arguments = new Argument[] { param };
md.returnType = returnType;
md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName("putAll", new String(data.getPluralName())).toCharArray();
data.setGeneratedByRecursive(md);
injectMethod(builderType, md);
}
use of org.eclipse.jdt.internal.compiler.ast.FieldReference in project lombok by rzwitserloot.
the class EclipseJavaUtilSingularizer method createJavaUtilSetMapInitialCapacitySwitchStatements.
protected List<Statement> createJavaUtilSetMapInitialCapacitySwitchStatements(SingularData data, EclipseNode builderType, boolean mapMode, String emptyCollectionMethod, String singletonCollectionMethod, String targetType) {
List<Statement> switchContents = new ArrayList<Statement>();
char[] keyName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName();
if (emptyCollectionMethod != null) {
// case 0: (empty); break;
switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '0' }, null), 0, 0));
/* pluralName = java.util.Collections.emptyCollectionMethod(); */
{
MessageSend invoke = new MessageSend();
invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
invoke.selector = emptyCollectionMethod.toCharArray();
switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
}
switchContents.add(new BreakStatement(null, 0, 0));
}
if (singletonCollectionMethod != null) {
// case 1: (singleton); break;
switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '1' }, null), 0, 0));
/* !mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName.get(0));
mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName$key.get(0), this.pluralName$value.get(0)); */
{
FieldReference thisDotKey = new FieldReference(keyName, 0L);
thisDotKey.receiver = new ThisReference(0, 0);
MessageSend thisDotKeyGet0 = new MessageSend();
thisDotKeyGet0.receiver = thisDotKey;
thisDotKeyGet0.selector = new char[] { 'g', 'e', 't' };
thisDotKeyGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) };
Expression[] args;
if (mapMode) {
char[] valueName = (new String(data.getPluralName()) + "$value").toCharArray();
FieldReference thisDotValue = new FieldReference(valueName, 0L);
thisDotValue.receiver = new ThisReference(0, 0);
MessageSend thisDotValueGet0 = new MessageSend();
thisDotValueGet0.receiver = thisDotValue;
thisDotValueGet0.selector = new char[] { 'g', 'e', 't' };
thisDotValueGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) };
args = new Expression[] { thisDotKeyGet0, thisDotValueGet0 };
} else {
args = new Expression[] { thisDotKeyGet0 };
}
MessageSend invoke = new MessageSend();
invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
invoke.selector = singletonCollectionMethod.toCharArray();
invoke.arguments = args;
switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0));
}
switchContents.add(new BreakStatement(null, 0, 0));
}
{
// default:
switchContents.add(new CaseStatement(null, 0, 0));
switchContents.addAll(createJavaUtilSimpleCreationAndFillStatements(data, builderType, mapMode, false, true, emptyCollectionMethod == null, targetType));
}
SwitchStatement switchStat = new SwitchStatement();
switchStat.statements = switchContents.toArray(new Statement[switchContents.size()]);
switchStat.expression = getSize(builderType, keyName, true);
TypeReference localShadowerType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()), NULL_POSS);
localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs());
LocalDeclaration varDefStat = new LocalDeclaration(data.getPluralName(), 0, 0);
varDefStat.type = localShadowerType;
return Arrays.asList(varDefStat, switchStat);
}
use of org.eclipse.jdt.internal.compiler.ast.FieldReference in project lombok by rzwitserloot.
the class EclipseGuavaSingularizer method generateClearMethod.
void generateClearMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) {
MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
md.modifiers = ClassFileConstants.AccPublic;
FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
thisDotField.receiver = new ThisReference(0, 0);
Assignment a = new Assignment(thisDotField, new NullLiteral(0, 0), 0);
md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray();
md.statements = returnStatement != null ? new Statement[] { a, returnStatement } : new Statement[] { a };
md.returnType = returnType;
injectMethod(builderType, md);
}
use of org.eclipse.jdt.internal.compiler.ast.FieldReference in project lombok by rzwitserloot.
the class HandleBuilder method generateCleanMethod.
private MethodDeclaration generateCleanMethod(List<BuilderFieldData> builderFields, EclipseNode builderType, ASTNode source) {
List<Statement> statements = new ArrayList<Statement>();
for (BuilderFieldData bfd : builderFields) {
if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
bfd.singularData.getSingularizer().appendCleaningCode(bfd.singularData, builderType, statements);
}
}
FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0);
thisUnclean.receiver = new ThisReference(0, 0);
statements.add(new Assignment(thisUnclean, new FalseLiteral(0, 0), 0));
MethodDeclaration decl = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
decl.selector = CLEAN_METHOD_NAME;
decl.modifiers = ClassFileConstants.AccPrivate;
decl.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
decl.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
decl.statements = statements.toArray(new Statement[0]);
decl.traverse(new SetGeneratedByVisitor(source), (ClassScope) null);
return decl;
}
use of org.eclipse.jdt.internal.compiler.ast.FieldReference in project lombok by rzwitserloot.
the class HandleBuilder method generateBuildMethod.
public MethodDeclaration generateBuildMethod(boolean isStatic, String name, char[] staticName, TypeReference returnType, List<BuilderFieldData> builderFields, EclipseNode type, TypeReference[] thrownExceptions, boolean addCleaning, ASTNode source) {
MethodDeclaration out = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
List<Statement> statements = new ArrayList<Statement>();
if (addCleaning) {
FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0);
thisUnclean.receiver = new ThisReference(0, 0);
Expression notClean = new UnaryExpression(thisUnclean, OperatorIds.NOT);
MessageSend invokeClean = new MessageSend();
invokeClean.selector = CLEAN_METHOD_NAME;
statements.add(new IfStatement(notClean, invokeClean, 0, 0));
}
for (BuilderFieldData bfd : builderFields) {
if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
bfd.singularData.getSingularizer().appendBuildCode(bfd.singularData, type, statements, bfd.name);
}
}
List<Expression> args = new ArrayList<Expression>();
for (BuilderFieldData bfd : builderFields) {
args.add(new SingleNameReference(bfd.name, 0L));
}
if (addCleaning) {
FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0);
thisUnclean.receiver = new ThisReference(0, 0);
statements.add(new Assignment(thisUnclean, new TrueLiteral(0, 0), 0));
}
out.modifiers = ClassFileConstants.AccPublic;
out.selector = name.toCharArray();
out.thrownExceptions = copyTypes(thrownExceptions);
out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
out.returnType = returnType;
if (staticName == null) {
AllocationExpression allocationStatement = new AllocationExpression();
allocationStatement.type = copyType(out.returnType);
allocationStatement.arguments = args.isEmpty() ? null : args.toArray(new Expression[args.size()]);
statements.add(new ReturnStatement(allocationStatement, 0, 0));
} else {
MessageSend invoke = new MessageSend();
invoke.selector = staticName;
if (isStatic)
invoke.receiver = new SingleNameReference(type.up().getName().toCharArray(), 0);
else
invoke.receiver = new QualifiedThisReference(new SingleTypeReference(type.up().getName().toCharArray(), 0), 0, 0);
TypeParameter[] tps = ((TypeDeclaration) type.get()).typeParameters;
if (tps != null) {
TypeReference[] trs = new TypeReference[tps.length];
for (int i = 0; i < trs.length; i++) {
trs[i] = new SingleTypeReference(tps[i].name, 0);
}
invoke.typeArguments = trs;
}
invoke.arguments = args.isEmpty() ? null : args.toArray(new Expression[args.size()]);
if (returnType instanceof SingleTypeReference && Arrays.equals(TypeConstants.VOID, ((SingleTypeReference) returnType).token)) {
statements.add(invoke);
} else {
statements.add(new ReturnStatement(invoke, 0, 0));
}
}
out.statements = statements.isEmpty() ? null : statements.toArray(new Statement[statements.size()]);
out.traverse(new SetGeneratedByVisitor(source), (ClassScope) null);
return out;
}
Aggregations