use of org.eclipse.jdt.internal.compiler.ast.ThisReference 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;
}
use of org.eclipse.jdt.internal.compiler.ast.ThisReference in project lombok by rzwitserloot.
the class HandleBuilder method generateToBuilderMethod.
private MethodDeclaration generateToBuilderMethod(String methodName, String builderClassName, EclipseNode type, TypeParameter[] typeParams, List<BuilderFieldData> builderFields, boolean fluent, ASTNode source) {
// return new ThingieBuilder<A, B>().setA(this.a).setB(this.b);
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
MethodDeclaration out = new MethodDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
out.selector = methodName.toCharArray();
out.modifiers = ClassFileConstants.AccPublic;
out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
out.returnType = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p);
AllocationExpression invoke = new AllocationExpression();
invoke.type = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p);
Expression receiver = invoke;
for (BuilderFieldData bfd : builderFields) {
char[] setterName = fluent ? bfd.name : HandlerUtil.buildAccessorName("set", new String(bfd.name)).toCharArray();
MessageSend ms = new MessageSend();
if (bfd.obtainVia == null || !bfd.obtainVia.field().isEmpty()) {
char[] fieldName = bfd.obtainVia == null ? bfd.rawName : bfd.obtainVia.field().toCharArray();
FieldReference fr = new FieldReference(fieldName, 0);
fr.receiver = new ThisReference(0, 0);
ms.arguments = new Expression[] { fr };
} else {
String obtainName = bfd.obtainVia.method();
boolean obtainIsStatic = bfd.obtainVia.isStatic();
MessageSend obtainExpr = new MessageSend();
obtainExpr.receiver = obtainIsStatic ? new SingleNameReference(type.getName().toCharArray(), 0) : new ThisReference(0, 0);
obtainExpr.selector = obtainName.toCharArray();
if (obtainIsStatic)
obtainExpr.arguments = new Expression[] { new ThisReference(0, 0) };
ms.arguments = new Expression[] { obtainExpr };
}
ms.receiver = receiver;
ms.selector = setterName;
receiver = ms;
}
out.statements = new Statement[] { new ReturnStatement(receiver, pS, pE) };
out.traverse(new SetGeneratedByVisitor(source), ((TypeDeclaration) type.get()).scope);
return out;
}
use of org.eclipse.jdt.internal.compiler.ast.ThisReference in project lombok by rzwitserloot.
the class EclipseHandlerUtil method createFieldAccessor.
static Expression createFieldAccessor(EclipseNode field, FieldAccess fieldAccess, ASTNode source) {
int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd;
long p = (long) pS << 32 | pE;
boolean lookForGetter = lookForGetter(field, fieldAccess);
GetterMethod getter = lookForGetter ? findGetter(field) : null;
if (getter == null) {
FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
FieldReference ref = new FieldReference(fieldDecl.name, p);
if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) {
EclipseNode containerNode = field.up();
if (containerNode != null && containerNode.get() instanceof TypeDeclaration) {
ref.receiver = new SingleNameReference(((TypeDeclaration) containerNode.get()).name, p);
} else {
Expression smallRef = new FieldReference(field.getName().toCharArray(), p);
if (source != null)
setGeneratedBy(smallRef, source);
return smallRef;
}
} else {
ref.receiver = new ThisReference(pS, pE);
}
if (source != null) {
setGeneratedBy(ref, source);
setGeneratedBy(ref.receiver, source);
}
return ref;
}
MessageSend call = new MessageSend();
setGeneratedBy(call, source);
call.sourceStart = pS;
call.statementEnd = call.sourceEnd = pE;
call.receiver = new ThisReference(pS, pE);
setGeneratedBy(call.receiver, source);
call.selector = getter.name;
return call;
}
use of org.eclipse.jdt.internal.compiler.ast.ThisReference in project lombok by rzwitserloot.
the class HandleConstructor method createConstructor.
public static ConstructorDeclaration createConstructor(AccessLevel level, EclipseNode type, Collection<EclipseNode> fields, boolean allToDefault, EclipseNode sourceNode, List<Annotation> onConstructor) {
ASTNode source = sourceNode.get();
TypeDeclaration typeDeclaration = ((TypeDeclaration) type.get());
long p = (long) source.sourceStart << 32 | source.sourceEnd;
boolean isEnum = (((TypeDeclaration) type.get()).modifiers & ClassFileConstants.AccEnum) != 0;
if (isEnum)
level = AccessLevel.PRIVATE;
boolean suppressConstructorProperties;
if (fields.isEmpty()) {
suppressConstructorProperties = false;
} else {
suppressConstructorProperties = Boolean.TRUE.equals(type.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES));
}
ConstructorDeclaration constructor = new ConstructorDeclaration(((CompilationUnitDeclaration) type.top().get()).compilationResult);
constructor.modifiers = toEclipseModifier(level);
constructor.selector = typeDeclaration.name;
constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
constructor.constructorCall.sourceStart = source.sourceStart;
constructor.constructorCall.sourceEnd = source.sourceEnd;
constructor.thrownExceptions = null;
constructor.typeParameters = null;
constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
constructor.arguments = null;
List<Argument> params = new ArrayList<Argument>();
List<Statement> assigns = new ArrayList<Statement>();
List<Statement> nullChecks = new ArrayList<Statement>();
for (EclipseNode fieldNode : fields) {
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
char[] rawName = field.name;
char[] fieldName = removePrefixFromField(fieldNode);
FieldReference thisX = new FieldReference(rawName, p);
int s = (int) (p >> 32);
int e = (int) p;
thisX.receiver = new ThisReference(s, e);
Expression assignmentExpr = allToDefault ? getDefaultExpr(field.type, s, e) : new SingleNameReference(fieldName, p);
Assignment assignment = new Assignment(thisX, assignmentExpr, (int) p);
assignment.sourceStart = (int) (p >> 32);
assignment.sourceEnd = assignment.statementEnd = (int) (p >> 32);
assigns.add(assignment);
if (!allToDefault) {
long fieldPos = (((long) field.sourceStart) << 32) | field.sourceEnd;
Argument parameter = new Argument(fieldName, fieldPos, copyType(field.type, source), Modifier.FINAL);
Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
if (nonNulls.length != 0) {
Statement nullCheck = generateNullCheck(field, sourceNode);
if (nullCheck != null)
nullChecks.add(nullCheck);
}
parameter.annotations = copyAnnotations(source, nonNulls, nullables);
params.add(parameter);
}
}
nullChecks.addAll(assigns);
constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]);
constructor.arguments = params.isEmpty() ? null : params.toArray(new Argument[params.size()]);
/* Generate annotations that must be put on the generated method, and attach them. */
{
Annotation[] constructorProperties = null;
if (!allToDefault && !suppressConstructorProperties && level != AccessLevel.PRIVATE && level != AccessLevel.PACKAGE && !isLocalType(type)) {
constructorProperties = createConstructorProperties(source, fields);
}
constructor.annotations = copyAnnotations(source, onConstructor.toArray(new Annotation[0]), constructorProperties);
}
constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope);
return constructor;
}
use of org.eclipse.jdt.internal.compiler.ast.ThisReference in project lombok by rzwitserloot.
the class HandleHelper method handle.
@Override
public void handle(AnnotationValues<Helper> annotation, Annotation ast, EclipseNode annotationNode) {
handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.HELPER_FLAG_USAGE, "@Helper");
EclipseNode annotatedType = annotationNode.up();
EclipseNode containingBlock = annotatedType == null ? null : annotatedType.directUp();
Statement[] origStatements = getStatementsFromAstNode(containingBlock == null ? null : containingBlock.get());
if (annotatedType == null || annotatedType.getKind() != Kind.TYPE || origStatements == null) {
annotationNode.addError("@Helper is legal only on method-local classes.");
return;
}
TypeDeclaration annotatedType_ = (TypeDeclaration) annotatedType.get();
int indexOfType = -1;
for (int i = 0; i < origStatements.length; i++) {
if (origStatements[i] == annotatedType_) {
indexOfType = i;
break;
}
}
final List<String> knownMethodNames = new ArrayList<String>();
for (AbstractMethodDeclaration methodOfHelper : annotatedType_.methods) {
if (!(methodOfHelper instanceof MethodDeclaration))
continue;
char[] name = methodOfHelper.selector;
if (name != null && name.length > 0 && name[0] != '<')
knownMethodNames.add(new String(name));
}
Collections.sort(knownMethodNames);
final String[] knownMethodNames_ = knownMethodNames.toArray(new String[knownMethodNames.size()]);
final char[] helperName = new char[annotatedType_.name.length + 1];
final boolean[] helperUsed = new boolean[1];
helperName[0] = '$';
System.arraycopy(annotatedType_.name, 0, helperName, 1, helperName.length - 1);
ASTVisitor visitor = new ASTVisitor() {
@Override
public boolean visit(MessageSend messageSend, BlockScope scope) {
if (messageSend.receiver instanceof ThisReference) {
if ((((ThisReference) messageSend.receiver).bits & ASTNode.IsImplicitThis) == 0)
return true;
} else if (messageSend.receiver != null)
return true;
char[] name = messageSend.selector;
if (name == null || name.length == 0 || name[0] == '<')
return true;
String n = new String(name);
if (Arrays.binarySearch(knownMethodNames_, n) < 0)
return true;
messageSend.receiver = new SingleNameReference(helperName, messageSend.nameSourcePosition);
helperUsed[0] = true;
return true;
}
};
for (int i = indexOfType + 1; i < origStatements.length; i++) {
origStatements[i].traverse(visitor, null);
}
if (!helperUsed[0]) {
annotationNode.addWarning("No methods of this helper class are ever used.");
return;
}
Statement[] newStatements = new Statement[origStatements.length + 1];
System.arraycopy(origStatements, 0, newStatements, 0, indexOfType + 1);
System.arraycopy(origStatements, indexOfType + 1, newStatements, indexOfType + 2, origStatements.length - indexOfType - 1);
LocalDeclaration decl = new LocalDeclaration(helperName, 0, 0);
decl.modifiers |= ClassFileConstants.AccFinal;
AllocationExpression alloc = new AllocationExpression();
alloc.type = new SingleTypeReference(annotatedType_.name, 0L);
decl.initialization = alloc;
decl.type = new SingleTypeReference(annotatedType_.name, 0L);
SetGeneratedByVisitor sgbvVisitor = new SetGeneratedByVisitor(annotationNode.get());
decl.traverse(sgbvVisitor, null);
newStatements[indexOfType + 1] = decl;
setStatementsOfAstNode(containingBlock.get(), newStatements);
}
Aggregations