use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by rzwitserloot.
the class EclipseJavaUtilListSetSingularizer method generatePluralMethod.
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;
List<Statement> statements = new ArrayList<Statement>();
statements.add(createConstructBuilderVarIfNeeded(data, builderType, false));
FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
thisDotField.receiver = new ThisReference(0, 0);
MessageSend thisDotFieldDotAddAll = new MessageSend();
thisDotFieldDotAddAll.arguments = new Expression[] { new SingleNameReference(data.getPluralName(), 0L) };
thisDotFieldDotAddAll.receiver = thisDotField;
thisDotFieldDotAddAll.selector = "addAll".toCharArray();
statements.add(thisDotFieldDotAddAll);
if (returnStatement != null)
statements.add(returnStatement);
md.statements = statements.toArray(new Statement[statements.size()]);
TypeReference paramType = new QualifiedTypeReference(TypeConstants.JAVA_UTIL_COLLECTION, NULL_POSS);
paramType = addTypeArgs(1, 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("addAll", new String(data.getPluralName())).toCharArray();
data.setGeneratedByRecursive(md);
injectMethod(builderType, md);
}
use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by rzwitserloot.
the class EclipseJavaUtilListSetSingularizer method generateSingularMethod.
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, false));
FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
thisDotField.receiver = new ThisReference(0, 0);
MessageSend thisDotFieldDotAdd = new MessageSend();
thisDotFieldDotAdd.arguments = new Expression[] { new SingleNameReference(data.getSingularName(), 0L) };
thisDotFieldDotAdd.receiver = thisDotField;
thisDotFieldDotAdd.selector = "add".toCharArray();
statements.add(thisDotFieldDotAdd);
if (returnStatement != null)
statements.add(returnStatement);
md.statements = statements.toArray(new Statement[statements.size()]);
TypeReference paramType = cloneParamType(0, data.getTypeArgs(), builderType);
Argument param = new Argument(data.getSingularName(), 0, paramType, 0);
md.arguments = new Argument[] { param };
md.returnType = returnType;
md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("add", new String(data.getSingularName())).toCharArray();
data.setGeneratedByRecursive(md);
injectMethod(builderType, md);
}
use of org.eclipse.jdt.internal.compiler.ast.MessageSend 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.MessageSend 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.MessageSend in project lombok by rzwitserloot.
the class HandleGetter method createLazyGetterBody.
public Statement[] createLazyGetterBody(ASTNode source, EclipseNode fieldNode) {
/*
java.lang.Object value = this.fieldName.get();
if (value == null) {
synchronized (this.fieldName) {
value = this.fieldName.get();
if (value == null) {
final RawValueType actualValue = INITIALIZER_EXPRESSION;
[IF PRIMITIVE]
value = actualValue;
[ELSE]
value = actualValue == null ? this.fieldName : actualValue;
[END IF]
this.fieldName.set(value);
}
}
}
[IF PRIMITIVE]
return (BoxedValueType) value;
[ELSE]
return (BoxedValueType) (value == this.fieldName ? null : value);
[END IF]
*/
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
TypeReference rawComponentType = copyType(field.type, source);
TypeReference boxedComponentType = null;
boolean isPrimitive = false;
if (field.type instanceof SingleTypeReference && !(field.type instanceof ArrayTypeReference)) {
char[][] newType = TYPE_MAP.get(new String(((SingleTypeReference) field.type).token));
if (newType != null) {
boxedComponentType = new QualifiedTypeReference(newType, poss(source, 3));
isPrimitive = true;
}
}
if (boxedComponentType == null)
boxedComponentType = copyType(field.type, source);
boxedComponentType.sourceStart = pS;
boxedComponentType.sourceEnd = boxedComponentType.statementEnd = pE;
Statement[] statements = new Statement[3];
/* java.lang.Object value = this.fieldName.get(); */
{
LocalDeclaration valueDecl = new LocalDeclaration(valueName, pS, pE);
valueDecl.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(source, 3));
valueDecl.type.sourceStart = pS;
valueDecl.type.sourceEnd = valueDecl.type.statementEnd = pE;
MessageSend getter = new MessageSend();
getter.sourceStart = pS;
getter.statementEnd = getter.sourceEnd = pE;
getter.selector = new char[] { 'g', 'e', 't' };
getter.receiver = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
valueDecl.initialization = getter;
statements[0] = valueDecl;
}
/*
if (value == null) {
synchronized (this.fieldName) {
value = this.fieldName.get();
if (value == null) {
final ValueType actualValue = INITIALIZER_EXPRESSION;
[IF PRIMITIVE]
value = actualValue;
[ELSE]
value = actualValue == null ? this.fieldName : actualValue;
[END IF]
this.fieldName.set(value);
}
}
}
*/
{
EqualExpression cond = new EqualExpression(new SingleNameReference(valueName, p), new NullLiteral(pS, pE), BinaryExpression.EQUAL_EQUAL);
Block then = new Block(0);
Expression lock = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
Block inner = new Block(0);
inner.statements = new Statement[2];
/* value = this.fieldName.get(); */
{
MessageSend getter = new MessageSend();
getter.sourceStart = pS;
getter.sourceEnd = getter.statementEnd = pE;
getter.selector = new char[] { 'g', 'e', 't' };
getter.receiver = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
Assignment assign = new Assignment(new SingleNameReference(valueName, p), getter, pE);
assign.sourceStart = pS;
assign.statementEnd = assign.sourceEnd = pE;
inner.statements[0] = assign;
}
/* if (value == null) */
{
EqualExpression innerCond = new EqualExpression(new SingleNameReference(valueName, p), new NullLiteral(pS, pE), BinaryExpression.EQUAL_EQUAL);
innerCond.sourceStart = pS;
innerCond.sourceEnd = innerCond.statementEnd = pE;
Block innerThen = new Block(0);
innerThen.statements = new Statement[3];
/* final ValueType actualValue = INITIALIZER_EXPRESSION */
{
LocalDeclaration actualValueDecl = new LocalDeclaration(actualValueName, pS, pE);
actualValueDecl.type = rawComponentType;
actualValueDecl.type.sourceStart = pS;
actualValueDecl.type.sourceEnd = actualValueDecl.type.statementEnd = pE;
actualValueDecl.initialization = field.initialization;
actualValueDecl.modifiers = ClassFileConstants.AccFinal;
innerThen.statements[0] = actualValueDecl;
}
/* [IF PRIMITIVE] value = actualValue; */
{
if (isPrimitive) {
Assignment innerAssign = new Assignment(new SingleNameReference(valueName, p), new SingleNameReference(actualValueName, p), pE);
innerAssign.sourceStart = pS;
innerAssign.statementEnd = innerAssign.sourceEnd = pE;
innerThen.statements[1] = innerAssign;
}
}
/* [ELSE] value = actualValue == null ? this.fieldName : actualValue; */
{
if (!isPrimitive) {
EqualExpression avIsNull = new EqualExpression(new SingleNameReference(actualValueName, p), new NullLiteral(pS, pE), BinaryExpression.EQUAL_EQUAL);
avIsNull.sourceStart = pS;
avIsNull.sourceEnd = avIsNull.statementEnd = pE;
Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
ConditionalExpression ternary = new ConditionalExpression(avIsNull, fieldRef, new SingleNameReference(actualValueName, p));
ternary.sourceStart = pS;
ternary.sourceEnd = ternary.statementEnd = pE;
Assignment innerAssign = new Assignment(new SingleNameReference(valueName, p), ternary, pE);
innerAssign.sourceStart = pS;
innerAssign.statementEnd = innerAssign.sourceEnd = pE;
innerThen.statements[1] = innerAssign;
}
}
/* this.fieldName.set(value); */
{
MessageSend setter = new MessageSend();
setter.sourceStart = pS;
setter.sourceEnd = setter.statementEnd = pE;
setter.receiver = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
setter.selector = new char[] { 's', 'e', 't' };
setter.arguments = new Expression[] { new SingleNameReference(valueName, p) };
innerThen.statements[2] = setter;
}
IfStatement innerIf = new IfStatement(innerCond, innerThen, pS, pE);
inner.statements[1] = innerIf;
}
SynchronizedStatement sync = new SynchronizedStatement(lock, inner, pS, pE);
then.statements = new Statement[] { sync };
IfStatement ifStatement = new IfStatement(cond, then, pS, pE);
statements[1] = ifStatement;
}
/* [IF PRIMITIVE] return (BoxedValueType)value; */
{
if (isPrimitive) {
CastExpression cast = makeCastExpression(new SingleNameReference(valueName, p), boxedComponentType, source);
statements[2] = new ReturnStatement(cast, pS, pE);
}
}
/* [ELSE] return (BoxedValueType)(value == this.fieldName ? null : value); */
{
if (!isPrimitive) {
EqualExpression vIsThisFieldName = new EqualExpression(new SingleNameReference(valueName, p), createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source), BinaryExpression.EQUAL_EQUAL);
vIsThisFieldName.sourceStart = pS;
vIsThisFieldName.sourceEnd = vIsThisFieldName.statementEnd = pE;
ConditionalExpression ternary = new ConditionalExpression(vIsThisFieldName, new NullLiteral(pS, pE), new SingleNameReference(valueName, p));
ternary.sourceStart = pS;
ternary.sourceEnd = ternary.statementEnd = pE;
ternary.bits |= PARENTHESIZED;
CastExpression cast = makeCastExpression(ternary, boxedComponentType, source);
statements[2] = new ReturnStatement(cast, pS, pE);
}
}
// update the field type and init last
/* private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> fieldName = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>(); */
{
TypeReference innerType = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(source, 3));
TypeReference[][] typeParams = new TypeReference[5][];
typeParams[4] = new TypeReference[] { innerType };
TypeReference type = new ParameterizedQualifiedTypeReference(AR, typeParams, 0, poss(source, 5));
// Some magic here
type.sourceStart = -1;
type.sourceEnd = -2;
field.type = type;
AllocationExpression init = new AllocationExpression();
// Some magic here
init.sourceStart = field.initialization.sourceStart;
init.sourceEnd = init.statementEnd = field.initialization.sourceEnd;
init.type = copyType(type, source);
field.initialization = init;
}
return statements;
}
Aggregations