use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by projectlombok.
the class EclipseJavaUtilMapSingularizer method generatePluralMethod.
private void generatePluralMethod(CheckerFrameworkVersion cfv, boolean deprecate, TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent, AccessLevel access) {
MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
md.modifiers = toEclipseModifier(access);
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);
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, ClassFileConstants.AccFinal);
nullBehaviorize(builderType, data, statements, param, md);
if (returnStatement != null)
statements.add(returnStatement);
md.statements = statements.toArray(new Statement[0]);
md.arguments = new Argument[] { param };
md.returnType = returnType;
addCheckerFrameworkReturnsReceiver(md.returnType, data.getSource(), cfv);
String name = new String(data.getPluralName());
String setterPrefix = data.getSetterPrefix().length > 0 ? new String(data.getSetterPrefix()) : fluent ? "" : "put";
String setterName = HandlerUtil.buildAccessorName(builderType, setterPrefix, name);
md.selector = setterName.toCharArray();
Annotation[] selfReturnAnnotations = generateSelfReturnAnnotations(deprecate, data.getSource());
Annotation[] copyToSetterAnnotations = copyAnnotations(md, findCopyableToSetterAnnotations(data.getAnnotation().up()));
md.annotations = concat(selfReturnAnnotations, copyToSetterAnnotations, Annotation.class);
if (returnStatement != null)
createRelevantNonNullAnnotation(builderType, md);
data.setGeneratedByRecursive(md);
injectMethod(builderType, md);
}
use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by projectlombok.
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[0]);
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);
}
use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by projectlombok.
the class HandleLog method createFactoryParameters.
private static final Expression[] createFactoryParameters(ClassLiteralAccess loggingType, Annotation source, List<LogFactoryParameter> parameters, Expression loggerTopic) {
Expression[] expressions = new Expression[parameters.size()];
int pS = source.sourceStart, pE = source.sourceEnd;
for (int i = 0; i < parameters.size(); i++) {
LogFactoryParameter parameter = parameters.get(i);
switch(parameter) {
case TYPE:
expressions[i] = createFactoryTypeParameter(loggingType, source);
break;
case NAME:
long p = (long) pS << 32 | pE;
MessageSend factoryParameterCall = new MessageSend();
setGeneratedBy(factoryParameterCall, source);
factoryParameterCall.receiver = createFactoryTypeParameter(loggingType, source);
factoryParameterCall.selector = "getName".toCharArray();
factoryParameterCall.nameSourcePosition = p;
factoryParameterCall.sourceStart = pS;
factoryParameterCall.sourceEnd = factoryParameterCall.statementEnd = pE;
expressions[i] = factoryParameterCall;
break;
case TOPIC:
expressions[i] = EclipseHandlerUtil.copyAnnotationMemberValue(loggerTopic);
break;
case NULL:
expressions[i] = new NullLiteral(pS, pE);
break;
default:
throw new IllegalStateException("Unknown logger factory parameter type: " + parameter);
}
}
return expressions;
}
use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by projectlombok.
the class HandleWithBy method createWithBy.
public MethodDeclaration createWithBy(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, boolean makeAbstract) {
ASTNode source = sourceNode.get();
if (name == null)
return null;
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
int pS = source.sourceStart, pE = source.sourceEnd;
long p = (long) pS << 32 | pE;
MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
AnnotationValues<Accessors> accessors = getAccessorsForField(fieldNode);
if (makeAbstract)
modifier |= ClassFileConstants.AccAbstract | ExtraCompilerModifiers.AccSemicolonBody;
if (shouldMakeFinal(fieldNode, accessors))
modifier |= ClassFileConstants.AccFinal;
method.modifiers = modifier;
method.returnType = cloneSelfType(fieldNode, source);
if (method.returnType == null)
return null;
Annotation[] deprecated = null, checkerFramework = null;
if (isFieldDeprecated(fieldNode))
deprecated = new Annotation[] { generateDeprecatedAnnotation(source) };
if (getCheckerFrameworkVersion(fieldNode).generateSideEffectFree())
checkerFramework = new Annotation[] { generateNamedAnnotation(source, CheckerFrameworkVersion.NAME__SIDE_EFFECT_FREE) };
char[][] functionalInterfaceName = null;
int requiredCast = -1;
TypeReference parameterizer = null;
boolean superExtendsStyle = true;
char[] applyMethodName = NAME_APPLY;
if (field.type instanceof SingleTypeReference) {
char[] token = ((SingleTypeReference) field.type).token;
if (Arrays.equals(token, NAME_CHAR)) {
requiredCast = TypeIds.T_char;
functionalInterfaceName = NAME_JUF_INTOP;
} else if (Arrays.equals(token, NAME_SHORT)) {
requiredCast = TypeIds.T_short;
functionalInterfaceName = NAME_JUF_INTOP;
} else if (Arrays.equals(token, NAME_BYTE)) {
requiredCast = TypeIds.T_byte;
functionalInterfaceName = NAME_JUF_INTOP;
} else if (Arrays.equals(token, NAME_INT)) {
functionalInterfaceName = NAME_JUF_INTOP;
} else if (Arrays.equals(token, NAME_LONG)) {
functionalInterfaceName = NAME_JUF_LONGOP;
} else if (Arrays.equals(token, NAME_FLOAT)) {
requiredCast = TypeIds.T_float;
functionalInterfaceName = NAME_JUF_DOUBLEOP;
} else if (Arrays.equals(token, NAME_DOUBLE)) {
functionalInterfaceName = NAME_JUF_DOUBLEOP;
} else if (Arrays.equals(token, NAME_BOOLEAN)) {
functionalInterfaceName = NAME_JUF_OP;
parameterizer = new QualifiedTypeReference(NAME_JAVA_LANG_BOOLEAN, new long[] { 0, 0, 0 });
superExtendsStyle = false;
}
}
if (functionalInterfaceName == NAME_JUF_INTOP)
applyMethodName = NAME_APPLY_AS_INT;
if (functionalInterfaceName == NAME_JUF_LONGOP)
applyMethodName = NAME_APPLY_AS_LONG;
if (functionalInterfaceName == NAME_JUF_DOUBLEOP)
applyMethodName = NAME_APPLY_AS_DOUBLE;
if (functionalInterfaceName == null) {
functionalInterfaceName = NAME_JUF_FUNCTION;
parameterizer = copyType(field.type, source);
}
method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), checkerFramework, deprecated);
TypeReference fType = null;
if (parameterizer != null && superExtendsStyle) {
Wildcard w1 = new Wildcard(Wildcard.SUPER);
w1.bound = parameterizer;
Wildcard w2 = new Wildcard(Wildcard.EXTENDS);
w2.bound = copyType(field.type, source);
TypeReference[][] ta = new TypeReference[functionalInterfaceName.length][];
ta[functionalInterfaceName.length - 1] = new TypeReference[] { w1, w2 };
long[] ps = new long[functionalInterfaceName.length];
fType = new ParameterizedQualifiedTypeReference(functionalInterfaceName, ta, 0, ps);
}
if (parameterizer != null && !superExtendsStyle) {
TypeReference[][] ta = new TypeReference[functionalInterfaceName.length][];
ta[functionalInterfaceName.length - 1] = new TypeReference[] { parameterizer };
long[] ps = new long[functionalInterfaceName.length];
fType = new ParameterizedQualifiedTypeReference(functionalInterfaceName, ta, 0, ps);
}
if (parameterizer == null) {
long[] ps = new long[functionalInterfaceName.length];
fType = new QualifiedTypeReference(functionalInterfaceName, ps);
}
Argument param = new Argument(NAME_TRANSFORMER, p, fType, ClassFileConstants.AccFinal);
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;
if (!makeAbstract) {
List<Expression> args = new ArrayList<Expression>();
for (EclipseNode child : fieldNode.up().down()) {
if (child.getKind() != Kind.FIELD)
continue;
FieldDeclaration childDecl = (FieldDeclaration) child.get();
// Skip fields that start with $
if (childDecl.name != null && childDecl.name.length > 0 && childDecl.name[0] == '$')
continue;
long fieldFlags = childDecl.modifiers;
// Skip static fields.
if ((fieldFlags & ClassFileConstants.AccStatic) != 0)
continue;
// Skip initialized final fields.
if (((fieldFlags & ClassFileConstants.AccFinal) != 0) && childDecl.initialization != null)
continue;
if (child.get() == fieldNode.get()) {
MessageSend ms = new MessageSend();
ms.receiver = new SingleNameReference(NAME_TRANSFORMER, 0);
ms.selector = applyMethodName;
ms.arguments = new Expression[] { createFieldAccessor(child, FieldAccess.ALWAYS_FIELD, source) };
if (requiredCast != -1) {
args.add(makeCastExpression(ms, TypeReference.baseTypeReference(requiredCast, 0), source));
} else {
args.add(ms);
}
} else {
args.add(createFieldAccessor(child, FieldAccess.ALWAYS_FIELD, source));
}
}
AllocationExpression constructorCall = new AllocationExpression();
constructorCall.arguments = args.toArray(new Expression[0]);
constructorCall.type = cloneSelfType(fieldNode, source);
Statement returnStatement = new ReturnStatement(constructorCall, pS, pE);
method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
List<Statement> statements = new ArrayList<Statement>(5);
if (hasNonNullAnnotations(fieldNode)) {
Statement nullCheck = generateNullCheck(field, sourceNode, null);
if (nullCheck != null)
statements.add(nullCheck);
}
statements.add(returnStatement);
method.statements = statements.toArray(new Statement[0]);
}
createRelevantNonNullAnnotation(sourceNode, param, method);
createRelevantNonNullAnnotation(fieldNode, method);
method.traverse(new SetGeneratedByVisitor(source), parent.scope);
copyJavadoc(fieldNode, method, CopyJavadoc.WITH_BY);
return method;
}
use of org.eclipse.jdt.internal.compiler.ast.MessageSend in project lombok by projectlombok.
the class EclipseJavaUtilSingularizer method createJavaUtilSetMapInitialCapacitySwitchStatements.
protected List<Statement> createJavaUtilSetMapInitialCapacitySwitchStatements(SingularData data, EclipseNode builderType, boolean mapMode, String emptyCollectionMethod, String singletonCollectionMethod, String targetType, String builderVariable) {
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(Eclipse.createCaseStatement(makeIntLiteral(new char[] { '0' }, null)));
/* 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(Eclipse.createCaseStatement(makeIntLiteral(new char[] { '1' }, null)));
/* !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 = getBuilderReference(builderVariable);
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 = getBuilderReference(builderVariable);
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(Eclipse.createCaseStatement(null));
switchContents.addAll(createJavaUtilSimpleCreationAndFillStatements(data, builderType, mapMode, false, true, emptyCollectionMethod == null, targetType, builderVariable));
}
SwitchStatement switchStat = new SwitchStatement();
switchStat.statements = switchContents.toArray(new Statement[0]);
switchStat.expression = getSize(builderType, keyName, true, builderVariable);
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