use of org.eclipse.jdt.internal.compiler.ast.SingleNameReference in project lombok by rzwitserloot.
the class HandleSetter method createSetter.
static MethodDeclaration createSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, boolean shouldReturnThis, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, List<Annotation> onParam) {
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
ASTNode source = sourceNode.get();
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
method.modifiers = modifier;
if (shouldReturnThis) {
method.returnType = cloneSelfType(fieldNode, source);
}
if (method.returnType == null) {
method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
method.returnType.sourceStart = pS;
method.returnType.sourceEnd = pE;
shouldReturnThis = false;
}
Annotation[] deprecated = null;
if (isFieldDeprecated(fieldNode)) {
deprecated = new Annotation[] { generateDeprecatedAnnotation(source) };
}
method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), deprecated);
Argument param = new Argument(field.name, p, copyType(field.type, source), Modifier.FINAL);
param.sourceStart = pS;
param.sourceEnd = pE;
method.arguments = new Argument[] { param };
method.selector = name.toCharArray();
method.binding = null;
method.thrownExceptions = null;
method.typeParameters = null;
method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
NameReference fieldNameRef = new SingleNameReference(field.name, p);
Assignment assignment = new Assignment(fieldRef, fieldNameRef, (int) p);
assignment.sourceStart = pS;
assignment.sourceEnd = assignment.statementEnd = pE;
method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
List<Statement> statements = new ArrayList<Statement>(5);
if (nonNulls.length == 0) {
statements.add(assignment);
} else {
Statement nullCheck = generateNullCheck(field, sourceNode);
if (nullCheck != null)
statements.add(nullCheck);
statements.add(assignment);
}
if (shouldReturnThis) {
ThisReference thisRef = new ThisReference(pS, pE);
ReturnStatement returnThis = new ReturnStatement(thisRef, pS, pE);
statements.add(returnThis);
}
method.statements = statements.toArray(new Statement[0]);
param.annotations = copyAnnotations(source, nonNulls, nullables, onParam.toArray(new Annotation[0]));
method.traverse(new SetGeneratedByVisitor(source), parent.scope);
return method;
}
use of org.eclipse.jdt.internal.compiler.ast.SingleNameReference in project lombok by rzwitserloot.
the class HandleSneakyThrows method buildTryCatchBlock.
public Statement buildTryCatchBlock(Statement[] contents, DeclaredException exception, ASTNode source, AbstractMethodDeclaration method) {
int methodStart = method.bodyStart;
int methodEnd = method.bodyEnd;
long methodPosEnd = ((long) methodEnd) << 32 | (methodEnd & 0xFFFFFFFFL);
TryStatement tryStatement = new TryStatement();
setGeneratedBy(tryStatement, source);
tryStatement.tryBlock = new Block(0);
// Positions for in-method generated nodes are special
tryStatement.tryBlock.sourceStart = methodStart;
tryStatement.tryBlock.sourceEnd = methodEnd;
setGeneratedBy(tryStatement.tryBlock, source);
tryStatement.tryBlock.statements = contents;
TypeReference typeReference;
if (exception.exceptionName.indexOf('.') == -1) {
typeReference = new SingleTypeReference(exception.exceptionName.toCharArray(), methodPosEnd);
typeReference.statementEnd = methodEnd;
} else {
String[] x = exception.exceptionName.split("\\.");
char[][] elems = new char[x.length][];
long[] poss = new long[x.length];
Arrays.fill(poss, methodPosEnd);
for (int i = 0; i < x.length; i++) {
elems[i] = x[i].trim().toCharArray();
}
typeReference = new QualifiedTypeReference(elems, poss);
}
setGeneratedBy(typeReference, source);
Argument catchArg = new Argument("$ex".toCharArray(), methodPosEnd, typeReference, Modifier.FINAL);
setGeneratedBy(catchArg, source);
catchArg.declarationSourceEnd = catchArg.declarationEnd = catchArg.sourceEnd = methodEnd;
catchArg.declarationSourceStart = catchArg.modifiersSourceStart = catchArg.sourceStart = methodEnd;
tryStatement.catchArguments = new Argument[] { catchArg };
MessageSend sneakyThrowStatement = new MessageSend();
setGeneratedBy(sneakyThrowStatement, source);
sneakyThrowStatement.receiver = new QualifiedNameReference(new char[][] { "lombok".toCharArray(), "Lombok".toCharArray() }, new long[2], methodEnd, methodEnd);
setGeneratedBy(sneakyThrowStatement.receiver, source);
sneakyThrowStatement.receiver.statementEnd = methodEnd;
sneakyThrowStatement.selector = "sneakyThrow".toCharArray();
SingleNameReference exRef = new SingleNameReference("$ex".toCharArray(), methodPosEnd);
setGeneratedBy(exRef, source);
exRef.statementEnd = methodEnd;
sneakyThrowStatement.arguments = new Expression[] { exRef };
// This is the magic fix for rendering issues
// In org.eclipse.jdt.core.dom.ASTConverter#convert(org.eclipse.jdt.internal.compiler.ast.MessageSend)
// a new SimpleName is created and the setSourceRange should receive -1, 0. That's why we provide -2L :-)
sneakyThrowStatement.nameSourcePosition = -2L;
sneakyThrowStatement.sourceStart = methodEnd;
sneakyThrowStatement.sourceEnd = sneakyThrowStatement.statementEnd = methodEnd;
Statement rethrowStatement = new ThrowStatement(sneakyThrowStatement, methodEnd, methodEnd);
setGeneratedBy(rethrowStatement, source);
Block block = new Block(0);
block.sourceStart = methodEnd;
block.sourceEnd = methodEnd;
setGeneratedBy(block, source);
block.statements = new Statement[] { rethrowStatement };
tryStatement.catchBlocks = new Block[] { block };
// Positions for in-method generated nodes are special
tryStatement.sourceStart = method.bodyStart;
tryStatement.sourceEnd = method.bodyEnd;
return tryStatement;
}
use of org.eclipse.jdt.internal.compiler.ast.SingleNameReference in project lombok by rzwitserloot.
the class HandleToString method generateQualifiedNameRef.
public static NameReference generateQualifiedNameRef(ASTNode source, char[]... varNames) {
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
NameReference ref;
if (varNames.length > 1)
ref = new QualifiedNameReference(varNames, new long[varNames.length], pS, pE);
else
ref = new SingleNameReference(varNames[0], p);
setGeneratedBy(ref, source);
return ref;
}
use of org.eclipse.jdt.internal.compiler.ast.SingleNameReference in project lombok by rzwitserloot.
the class EclipseJavaUtilSingularizer method createJavaUtilSimpleCreationAndFillStatements.
protected List<Statement> createJavaUtilSimpleCreationAndFillStatements(SingularData data, EclipseNode builderType, boolean mapMode, boolean defineVar, boolean addInitialCapacityArg, boolean nullGuard, String targetType) {
char[] varName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName();
Statement createStat;
{
// pluralName = new java.util.TargetType(initialCap);
Expression[] constructorArgs = null;
if (addInitialCapacityArg) {
// this.varName.size() < MAX_POWER_OF_2 ? 1 + this.varName.size() + (this.varName.size() - 3) / 3 : Integer.MAX_VALUE;
// lessThanCutOff = this.varName.size() < MAX_POWER_OF_2
Expression lessThanCutoff = new BinaryExpression(getSize(builderType, varName, nullGuard), makeIntLiteral("0x40000000".toCharArray(), null), OperatorIds.LESS);
FieldReference integerMaxValue = new FieldReference("MAX_VALUE".toCharArray(), 0L);
integerMaxValue.receiver = new QualifiedNameReference(TypeConstants.JAVA_LANG_INTEGER, NULL_POSS, 0, 0);
Expression sizeFormulaLeft = new BinaryExpression(makeIntLiteral(new char[] { '1' }, null), getSize(builderType, varName, nullGuard), OperatorIds.PLUS);
Expression sizeFormulaRightLeft = new BinaryExpression(getSize(builderType, varName, nullGuard), makeIntLiteral(new char[] { '3' }, null), OperatorIds.MINUS);
Expression sizeFormulaRight = new BinaryExpression(sizeFormulaRightLeft, makeIntLiteral(new char[] { '3' }, null), OperatorIds.DIVIDE);
Expression sizeFormula = new BinaryExpression(sizeFormulaLeft, sizeFormulaRight, OperatorIds.PLUS);
Expression cond = new ConditionalExpression(lessThanCutoff, sizeFormula, integerMaxValue);
constructorArgs = new Expression[] { cond };
}
TypeReference targetTypeRef = new QualifiedTypeReference(new char[][] { TypeConstants.JAVA, TypeConstants.UTIL, targetType.toCharArray() }, NULL_POSS);
targetTypeRef = addTypeArgs(mapMode ? 2 : 1, false, builderType, targetTypeRef, data.getTypeArgs());
AllocationExpression constructorCall = new AllocationExpression();
constructorCall.type = targetTypeRef;
constructorCall.arguments = constructorArgs;
if (defineVar) {
TypeReference localShadowerType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()), NULL_POSS);
localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs());
LocalDeclaration localShadowerDecl = new LocalDeclaration(data.getPluralName(), 0, 0);
localShadowerDecl.type = localShadowerType;
localShadowerDecl.initialization = constructorCall;
createStat = localShadowerDecl;
} else {
createStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), constructorCall, 0);
}
}
Statement fillStat;
{
if (mapMode) {
// for (int $i = 0; $i < this.pluralname$key.size(); i++) pluralname.put(this.pluralname$key.get($i), this.pluralname$value.get($i));
char[] iVar = new char[] { '$', 'i' };
MessageSend pluralnameDotPut = new MessageSend();
pluralnameDotPut.selector = new char[] { 'p', 'u', 't' };
pluralnameDotPut.receiver = new SingleNameReference(data.getPluralName(), 0L);
FieldReference thisDotKey = new FieldReference(varName, 0L);
thisDotKey.receiver = new ThisReference(0, 0);
FieldReference thisDotValue = new FieldReference((new String(data.getPluralName()) + "$value").toCharArray(), 0L);
thisDotValue.receiver = new ThisReference(0, 0);
MessageSend keyArg = new MessageSend();
keyArg.receiver = thisDotKey;
keyArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) };
keyArg.selector = new char[] { 'g', 'e', 't' };
MessageSend valueArg = new MessageSend();
valueArg.receiver = thisDotValue;
valueArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) };
valueArg.selector = new char[] { 'g', 'e', 't' };
pluralnameDotPut.arguments = new Expression[] { keyArg, valueArg };
LocalDeclaration forInit = new LocalDeclaration(iVar, 0, 0);
forInit.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
forInit.initialization = makeIntLiteral(new char[] { '0' }, null);
Expression checkExpr = new BinaryExpression(new SingleNameReference(iVar, 0L), getSize(builderType, varName, nullGuard), OperatorIds.LESS);
Expression incrementExpr = new PostfixExpression(new SingleNameReference(iVar, 0L), IntLiteral.One, OperatorIds.PLUS, 0);
fillStat = new ForStatement(new Statement[] { forInit }, checkExpr, new Statement[] { incrementExpr }, pluralnameDotPut, true, 0, 0);
} else {
// pluralname.addAll(this.pluralname);
MessageSend pluralnameDotAddAll = new MessageSend();
pluralnameDotAddAll.selector = new char[] { 'a', 'd', 'd', 'A', 'l', 'l' };
pluralnameDotAddAll.receiver = new SingleNameReference(data.getPluralName(), 0L);
FieldReference thisDotPluralname = new FieldReference(varName, 0L);
thisDotPluralname.receiver = new ThisReference(0, 0);
pluralnameDotAddAll.arguments = new Expression[] { thisDotPluralname };
fillStat = pluralnameDotAddAll;
}
if (nullGuard) {
FieldReference thisDotField = new FieldReference(varName, 0L);
thisDotField.receiver = new ThisReference(0, 0);
Expression cond = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL);
fillStat = new IfStatement(cond, fillStat, 0, 0);
}
}
Statement unmodifiableStat;
{
// pluralname = Collections.unmodifiableInterfaceType(pluralname);
Expression arg = new SingleNameReference(data.getPluralName(), 0L);
MessageSend invoke = new MessageSend();
invoke.arguments = new Expression[] { arg };
invoke.selector = ("unmodifiable" + data.getTargetSimpleType()).toCharArray();
invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
unmodifiableStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), invoke, 0);
}
return Arrays.asList(createStat, fillStat, unmodifiableStat);
}
use of org.eclipse.jdt.internal.compiler.ast.SingleNameReference 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);
}
Aggregations