protected int diffTypeParameter(JCTypeParameter oldT, JCTypeParameter newT, int[] bounds) { int localPointer = bounds[0]; copyTo(localPointer, getOldPos(oldT)); if (nameChanged(oldT.name, newT.name)) { printer.print(newT.name); localPointer += oldT.name.length(); } if (!listsMatch(oldT.bounds, newT.bounds)) { // todo (#pf): match it for rename only, other matching will be // finished later. PositionEstimator est = EstimatorFactory.implementz(oldT.getBounds(), newT.getBounds(), diffContext); int pos = oldT.bounds.nonEmpty() ? getOldPos(oldT.bounds.head) : -1; if (pos > -1) { copyTo(localPointer, pos); localPointer = diffList2(oldT.bounds, newT.bounds, pos, est); } } copyTo(localPointer, bounds[1]); return bounds[1]; }
public static JCExpression cloneSelfType(JavacNode childOfType) { JavacNode typeNode = childOfType; JavacTreeMaker maker = childOfType.getTreeMaker(); while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up(); if (typeNode != null && typeNode.get() instanceof JCClassDecl) { JCClassDecl type = (JCClassDecl) typeNode.get(); ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>(); if (!type.typarams.isEmpty()) { for (JCTypeParameter tp : type.typarams) { typeArgs.append(maker.Ident(tp.name)); } return maker.TypeApply(maker.Ident(type.name), typeArgs.toList()); } else { return maker.Ident(type.name); } } else { return null; } }
private JCMethodDecl generateCleanMethod(java.util.List<BuilderFieldData> builderFields, JavacNode type, JCTree source) { JavacTreeMaker maker = type.getTreeMaker(); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); for (BuilderFieldData bfd : builderFields) { if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) { bfd.singularData.getSingularizer().appendCleaningCode(bfd.singularData, type, source, statements); } } statements.append(maker.Exec(maker.Assign(maker.Select(maker.Ident(type.toName("this")), type.toName("$lombokUnclean")), maker.Literal(CTC_BOOLEAN, false)))); JCBlock body = maker.Block(0, statements.toList()); return maker.MethodDef(maker.Modifiers(Flags.PUBLIC), type.toName("$lombokClean"), maker.Type(Javac.createVoidType(maker, CTC_VOID)), List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null); /* * if (shouldReturnThis) { methodType = cloneSelfType(field); } if (methodType == null) { //WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6. methodType = treeMaker.Type(Javac.createVoidType(treeMaker, CTC_VOID)); shouldReturnThis = false; } */ }
public JCMethodDecl generateBuilderMethod(boolean isStatic, String builderMethodName, String builderClassName, JavacNode type, List<JCTypeParameter> typeParams) { JavacTreeMaker maker = type.getTreeMaker(); ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>(); for (JCTypeParameter typeParam : typeParams) { typeArgs.append(maker.Ident(typeParam.name)); } JCExpression call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.<JCExpression>nil(), null); JCStatement statement = maker.Return(call); JCBlock body = maker.Block(0, List.<JCStatement>of(statement)); int modifiers = Flags.PUBLIC; if (isStatic) modifiers |= Flags.STATIC; return maker.MethodDef(maker.Modifiers(modifiers), type.toName(builderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), copyTypeParams(maker, typeParams), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null); }
public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source, List<JCAnnotation> onParam) { /* protected boolean canEqual(final java.lang.Object other) { * return other instanceof Outer.Inner.MyType; * } */ JavacTreeMaker maker = typeNode.getTreeMaker(); JCModifiers mods = maker.Modifiers(Flags.PROTECTED, List.<JCAnnotation>nil()); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); Name canEqualName = typeNode.toName("canEqual"); JCExpression objectType = genJavaLangTypeRef(typeNode, "Object"); Name otherName = typeNode.toName("other"); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(flags, onParam), otherName, objectType, null)); JCBlock body = maker.Block(0, List.<JCStatement>of( maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode))))); return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source, typeNode.getContext()); }
private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) { JCModifiers mods = maker.Modifiers(Flags.PUBLIC); List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); List<JCVariableDecl> params = List.nil(); List<JCExpression> jceBlank = List.nil(); JCExpression thisDotKeyField = chainDots(builderType, "this", data.getPluralName() + "$key"); JCExpression thisDotKeyFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$key", "clear"); JCExpression thisDotValueFieldDotClear = chainDots(builderType, "this", data.getPluralName() + "$value", "clear"); JCStatement clearKeyCall = maker.Exec(maker.Apply(jceBlank, thisDotKeyFieldDotClear, jceBlank)); JCStatement clearValueCall = maker.Exec(maker.Apply(jceBlank, thisDotValueFieldDotClear, jceBlank)); JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotKeyField, maker.Literal(CTC_BOT, null)); JCBlock clearCalls = maker.Block(0, List.of(clearKeyCall, clearValueCall)); JCStatement ifSetCallClear = maker.If(cond, clearCalls, null); List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear); JCBlock body = maker.Block(0, statements); Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString())); JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null); injectMethod(builderType, method); }
private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) { JCModifiers mods = maker.Modifiers(Flags.PUBLIC); List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); List<JCVariableDecl> params = List.nil(); List<JCExpression> jceBlank = List.nil(); JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName()); JCExpression thisDotFieldDotClear = maker.Select(maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName()), builderType.toName("clear")); JCStatement clearCall = maker.Exec(maker.Apply(jceBlank, thisDotFieldDotClear, jceBlank)); JCExpression cond = maker.Binary(CTC_NOT_EQUAL, thisDotField, maker.Literal(CTC_BOT, null)); JCStatement ifSetCallClear = maker.If(cond, clearCall, null); List<JCStatement> statements = returnStatement != null ? List.of(ifSetCallClear, returnStatement) : List.of(ifSetCallClear); JCBlock body = maker.Block(0, statements); Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString())); JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null); injectMethod(builderType, method); }
void generateSingularMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source)); JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "add"); JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getSingularName()))); statements.append(maker.Exec(invokeAdd)); if (returnStatement != null) statements.append(returnStatement); JCBlock body = maker.Block(0, statements.toList()); Name name = data.getSingularName(); long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("add", name.toString())); JCExpression paramType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source); JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getSingularName(), paramType, null); JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null); injectMethod(builderType, method); }
void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source)); JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "addAll"); JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getPluralName()))); statements.append(maker.Exec(invokeAdd)); if (returnStatement != null) statements.append(returnStatement); JCBlock body = maker.Block(0, statements.toList()); Name name = data.getPluralName(); long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("addAll", name.toString())); JCExpression paramType = chainDots(builderType, "java", "util", "Collection"); paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs(), source); JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null); JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null); injectMethod(builderType, method); }
public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source, List<JCAnnotation> onParam) { /* public boolean canEqual(final java.lang.Object other) { * return other instanceof Outer.Inner.MyType; * } */ JavacTreeMaker maker = typeNode.getTreeMaker(); JCModifiers mods = maker.Modifiers(Flags.PROTECTED, List.<JCAnnotation>nil()); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); Name canEqualName = typeNode.toName("canEqual"); JCExpression objectType = genJavaLangTypeRef(typeNode, "Object"); Name otherName = typeNode.toName("other"); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext()); List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(flags, onParam), otherName, objectType, null)); JCBlock body = maker.Block(0, List.<JCStatement>of( maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode))))); return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source, typeNode.getContext()); }
@Override public boolean visitAnnotationDeclaration(AnnotationDeclaration node) { JCModifiers modifiers = (JCModifiers) toTree(node.astModifiers()); modifiers.flags |= Flags.INTERFACE | Flags.ANNOTATION; int start = posOfStructure(node, "interface", true); int end = node.getPosition().getEnd(); if (hasSourceStructures()) { if (modifiers.pos == -1) modifiers.pos = posOfStructure(node, "@", true); endPosTable.put(modifiers, posOfStructure(node, "@", false)); } return set(node, setPos(start, end, treeMaker.ClassDef( modifiers, toName(node.astName()), List.<JCTypeParameter>nil(), null, List.<JCExpression>nil(), node.astBody() == null ? List.<JCTree>nil() : toList(JCTree.class, node.astBody().astMembers()) ))); }
@Override public boolean visitAnnotationMethodDeclaration(AnnotationMethodDeclaration node) { JCMethodDecl methodDef = treeMaker.MethodDef( (JCModifiers)toTree(node.astModifiers()), toName(node.astMethodName()), toExpression(node.astReturnTypeReference()), List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, toExpression(node.astDefaultValue()) ); int start = node.astMethodName().getPosition().getStart(); int end = node.getPosition().getEnd(); return set(node, setPos(start, end, methodDef)); }
@Override public boolean visitMethodDeclaration(MethodDeclaration node) { JCMethodDecl methodDef = treeMaker.MethodDef( (JCModifiers)toTree(node.astModifiers()), toName(node.astMethodName()), toExpression(node.astReturnTypeReference()), toList(JCTypeParameter.class, node.astTypeVariables()), toList(JCVariableDecl.class, node.astParameters()), toList(JCExpression.class, node.astThrownTypeReferences()), (JCBlock)toTree(node.astBody()), null ); for (JCVariableDecl decl : methodDef.params) { decl.mods.flags |= Flags.PARAMETER; } int start = node.astMethodName().getPosition().getStart(); int end = node.getPosition().getEnd(); return set(node, setPos(start, end, methodDef)); }
@Override public boolean visitConstructorDeclaration(ConstructorDeclaration node) { JCMethodDecl constrDef = treeMaker.MethodDef( (JCModifiers)toTree(node.astModifiers()), table.init, null, toList(JCTypeParameter.class, node.astTypeVariables()), toList(JCVariableDecl.class, node.astParameters()), toList(JCExpression.class, node.astThrownTypeReferences()), (JCBlock)toTree(node.astBody()), null ); for (JCVariableDecl decl : constrDef.params) { decl.mods.flags |= Flags.PARAMETER; } int start = node.astTypeName().getPosition().getStart(); int end = node.getPosition().getEnd(); return set(node, setPos(start, end, constrDef)); }
/** * Gets the symbol declared by a tree. Returns null if {@code tree} does not declare a symbol or * is null. */ public static Symbol getDeclaredSymbol(Tree tree) { if (tree instanceof AnnotationTree) { return getSymbol(((AnnotationTree) tree).getAnnotationType()); } if (tree instanceof PackageTree) { return getSymbol((PackageTree) tree); } if (tree instanceof ParameterizedTypeTree) { return getSymbol(((ParameterizedTypeTree) tree).getType()); } if (tree instanceof TypeParameterTree) { Type type = ((JCTypeParameter) tree).type; return type == null ? null : type.tsym; } if (tree instanceof ClassTree) { return getSymbol((ClassTree) tree); } if (tree instanceof MethodTree) { return getSymbol((MethodTree) tree); } if (tree instanceof VariableTree) { return getSymbol((VariableTree) tree); } return null; }
/** ClassDeclaration = CLASS Ident TypeParametersOpt [EXTENDS Type] * [IMPLEMENTS TypeList] ClassBody * @param mods The modifiers starting the class declaration * @param dc The documentation comment for the class, or null. */ JCClassDecl classDeclaration(JCModifiers mods, String dc) { int pos = S.pos(); accept(CLASS); Name name = ident(); List<JCTypeParameter> typarams = typeParametersOpt(); JCExpression extending = null; if (S.token() == EXTENDS) { S.nextToken(); extending = parseType(); } List<JCExpression> implementing = List.nil(); if (S.token() == IMPLEMENTS) { S.nextToken(); implementing = typeList(); } List<JCTree> defs = classOrInterfaceBody(name, false); JCClassDecl result = toP(F.at(pos).ClassDef( mods, name, typarams, extending, implementing, defs)); attach(result, dc); return result; }
/** InterfaceDeclaration = INTERFACE Ident TypeParametersOpt * [EXTENDS TypeList] InterfaceBody * @param mods The modifiers starting the interface declaration * @param dc The documentation comment for the interface, or null. */ JCClassDecl interfaceDeclaration(JCModifiers mods, String dc) { int pos = S.pos(); accept(INTERFACE); Name name = ident(); List<JCTypeParameter> typarams = typeParametersOpt(); List<JCExpression> extending = List.nil(); if (S.token() == EXTENDS) { S.nextToken(); extending = typeList(); } List<JCTree> defs = classOrInterfaceBody(name, true); JCClassDecl result = toP(F.at(pos).ClassDef( mods, name, typarams, null, extending, defs)); attach(result, dc); return result; }
/** EnumDeclaration = ENUM Ident [IMPLEMENTS TypeList] EnumBody * @param mods The modifiers starting the enum declaration * @param dc The documentation comment for the enum, or null. */ JCClassDecl enumDeclaration(JCModifiers mods, String dc) { int pos = S.pos(); accept(ENUM); Name name = ident(); List<JCExpression> implementing = List.nil(); if (S.token() == IMPLEMENTS) { S.nextToken(); implementing = typeList(); } List<JCTree> defs = enumBody(name); mods.flags |= Flags.ENUM; JCClassDecl result = toP(F.at(pos). ClassDef(mods, name, List.<JCTypeParameter>nil(), null, implementing, defs)); attach(result, dc); return result; }
/** TypeParametersOpt = ["<" TypeParameter {"," TypeParameter} ">"] */ List<JCTypeParameter> typeParametersOpt() { if (S.token() == LT) { checkGenerics(); ListBuffer<JCTypeParameter> typarams = new ListBuffer<JCTypeParameter>(); S.nextToken(); typarams.append(typeParameter()); while (S.token() == COMMA) { S.nextToken(); typarams.append(typeParameter()); } accept(GT); return typarams.toList(); } else { return List.nil(); } }
public static JCExpression cloneSelfType(JavacNode field) { JavacNode typeNode = field; TreeMaker maker = field.getTreeMaker(); while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up(); if (typeNode != null && typeNode.get() instanceof JCClassDecl) { JCClassDecl type = (JCClassDecl) typeNode.get(); ListBuffer<JCExpression> typeArgs = ListBuffer.lb(); if (!type.typarams.isEmpty()) { for (JCTypeParameter tp : type.typarams) { typeArgs.append(maker.Ident(tp.name)); } return maker.TypeApply(maker.Ident(type.name), typeArgs.toList()); } else { return maker.Ident(type.name); } } else { return null; } }
private JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source) { /* public boolean canEqual(final java.lang.Object other) { * return other instanceof Outer.Inner.MyType; * } */ TreeMaker maker = typeNode.getTreeMaker(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.<JCAnnotation>nil()); JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN); Name canEqualName = typeNode.toName("canEqual"); JCExpression objectType = chainDots(typeNode, "java", "lang", "Object"); Name otherName = typeNode.toName("other"); List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(Flags.FINAL), otherName, objectType, null)); JCBlock body = maker.Block(0, List.<JCStatement>of( maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode))))); return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source); }
@Override public IndexedClassDecl ClassDef(JCModifiers mods, Name name, List<JCTypeParameter> typarams, JCExpression extending, List<JCExpression> implementing, List<JCTree> defs) { IndexedClassDecl result = new IndexedClassDecl(mods, name, typarams, extending, implementing, defs, null); result.pos = pos; return result; }
protected IndexedClassDecl(JCModifiers mods, Name name, List<JCTypeParameter> typarams, JCExpression extending, List<JCExpression> implementing, List<JCTree> defs, ClassSymbol sym) { super(mods, name, typarams, extending, implementing, defs, sym); this.index = -1; }
@Override protected JCTree methodDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name, List<JCTypeParameter> typarams, boolean isInterface, boolean isVoid, Comment dc) { if (cancelService != null) { cancelService.abortIfCanceled(); } return super.methodDeclaratorRest(pos, mods, type, name, typarams, isInterface, isVoid, dc); }
/** Class enter visitor method for type parameters. * Enter a symbol for type parameter in local scope, after checking that it * is unique. */ @Override public void visitTypeParameter(JCTypeParameter tree) { TypeVar a = (tree.type != null) ? (TypeVar)tree.type : new TypeVar(tree.name, env.info.scope.owner, syms.botType); tree.type = a; if (chk.checkUnique(tree.pos(), a.tsym, env.info.scope)) { env.info.scope.enter(a.tsym); } result = a; }
@Override public void visitTypeParameter(JCTypeParameter tree) { List<JCExpression> annotations = readObject(tree, "annotations", List.<JCExpression>nil()); if (!annotations.isEmpty()) { print(annotations, " "); print(" "); } print(tree.name); if (tree.bounds.nonEmpty()) { print(" extends "); print(tree.bounds, " & "); } consumeComments(tree); }
private void createPrivateDefaultConstructor(JavacNode typeNode) { JavacTreeMaker maker = typeNode.getTreeMaker(); JCModifiers mods = maker.Modifiers(Flags.PRIVATE, List.<JCAnnotation>nil()); Name name = typeNode.toName("<init>"); JCBlock block = maker.Block(0L, createThrowStatement(typeNode, maker)); JCMethodDecl methodDef = maker.MethodDef(mods, name, null, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), block, null); JCMethodDecl constructor = recursiveSetGeneratedBy(methodDef, typeNode.get(), typeNode.getContext()); JavacHandlerUtil.injectMethod(typeNode, constructor); }
public static JCExpression namePlusTypeParamsToTypeReference(JavacTreeMaker maker, Name typeName, List<JCTypeParameter> params) { ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>(); if (!params.isEmpty()) { for (JCTypeParameter param : params) { typeArgs.append(maker.Ident(param.name)); } return maker.TypeApply(maker.Ident(typeName), typeArgs.toList()); } return maker.Ident(typeName); }
public JavacNode makeBuilderClass(boolean isStatic, JavacNode tdParent, String builderClassName, List<JCTypeParameter> typeParams, JCAnnotation ast) { JavacTreeMaker maker = tdParent.getTreeMaker(); int modifiers = Flags.PUBLIC; if (isStatic) modifiers |= Flags.STATIC; JCModifiers mods = maker.Modifiers(modifiers); JCClassDecl builder = maker.ClassDef(mods, tdParent.toName(builderClassName), copyTypeParams(maker, typeParams), null, List.<JCExpression>nil(), List.<JCTree>nil()); return injectType(tdParent, builder); }
private JCMethodDecl createHelloWorld(JavacNode type) { //This is the artist/ bad guy JavacTreeMaker treeMaker = type.getTreeMaker(); //Define public JCModifiers modifiers = treeMaker.Modifiers(Modifier.PUBLIC); //Define Generic type List<JCTree.JCTypeParameter> methodGenericTypes = List.<JCTypeParameter>nil(); //Define return type JCExpression methodType = treeMaker.Type(Javac.createVoidType(treeMaker, CTC_VOID)); //Define method name Name methodName = type.toName("helloWorld"); //Define parameter List<JCTree.JCVariableDecl> methodParameters = List.<JCVariableDecl>nil(); //Define throw List<JCTree.JCExpression> methodThrows = List.<JCExpression>nil(); //Write our system.out.prinln("hello world") method JCExpression printlnMethod = JavacHandlerUtil.chainDots(type, "System", "out", "println"); List<JCTree.JCExpression> printlnArgs = List.<JCExpression>of(treeMaker.Literal("hello world")); JCMethodInvocation printlnInvocation = treeMaker.Apply(List.<JCExpression>nil(), printlnMethod, printlnArgs); //Define method JCBlock methodBody = treeMaker.Block(0, List.<JCStatement>of(treeMaker.Exec(printlnInvocation))); //Default value JCExpression defaultValue = null; return treeMaker.MethodDef( modifiers, methodName, methodType, methodGenericTypes, methodParameters, methodThrows, methodBody, defaultValue ); }
private void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> jceBlank = List.nil(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, true, source)); long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); long baseFlags = JavacHandlerUtil.addFinalIfNeeded(0, builderType.getContext()); Name entryName = builderType.toName("$lombokEntry"); JCExpression forEachType = chainDots(builderType, "java", "util", "Map", "Entry"); forEachType = addTypeArgs(2, true, builderType, forEachType, data.getTypeArgs(), source); JCExpression keyArg = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(entryName), builderType.toName("getKey")), List.<JCExpression>nil()); JCExpression valueArg = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(entryName), builderType.toName("getValue")), List.<JCExpression>nil()); JCExpression addKey = maker.Apply(List.<JCExpression>nil(), chainDots(builderType, "this", data.getPluralName() + "$key", "add"), List.of(keyArg)); JCExpression addValue = maker.Apply(List.<JCExpression>nil(), chainDots(builderType, "this", data.getPluralName() + "$value", "add"), List.of(valueArg)); JCBlock forEachBody = maker.Block(0, List.<JCStatement>of(maker.Exec(addKey), maker.Exec(addValue))); JCExpression entrySetInvocation = maker.Apply(jceBlank, maker.Select(maker.Ident(data.getPluralName()), builderType.toName("entrySet")), jceBlank); JCStatement forEach = maker.ForeachLoop(maker.VarDef(maker.Modifiers(baseFlags), entryName, forEachType, null), entrySetInvocation, forEachBody); statements.append(forEach); if (returnStatement != null) statements.append(returnStatement); JCBlock body = maker.Block(0, statements.toList()); Name name = data.getPluralName(); if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("putAll", name.toString())); JCExpression paramType = chainDots(builderType, "java", "util", "Map"); paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs(), source); JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null); JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), jceBlank, body, null); injectMethod(builderType, method); }
private void generateClearMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source) { JCModifiers mods = maker.Modifiers(Flags.PUBLIC); List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); List<JCVariableDecl> params = List.nil(); JCExpression thisDotField = maker.Select(maker.Ident(builderType.toName("this")), data.getPluralName()); JCStatement clearField = maker.Exec(maker.Assign(thisDotField, maker.Literal(CTC_BOT, null))); List<JCStatement> statements = returnStatement != null ? List.of(clearField, returnStatement) : List.of(clearField); JCBlock body = maker.Block(0, statements); Name methodName = builderType.toName(HandlerUtil.buildAccessorName("clear", data.getPluralName().toString())); JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null); injectMethod(builderType, method); }
void generateSingularMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); LombokImmutableList<String> suffixes = getArgumentSuffixes(); Name[] names = new Name[suffixes.size()]; for (int i = 0; i < suffixes.size(); i++) { String s = suffixes.get(i); Name n = data.getSingularName(); names[i] = s.isEmpty() ? n : builderType.toName(s); } JCModifiers mods = maker.Modifiers(Flags.PUBLIC); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, source)); JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), getAddMethodName()); ListBuffer<JCExpression> invokeAddExprBuilder = new ListBuffer<JCExpression>(); for (int i = 0; i < suffixes.size(); i++) { invokeAddExprBuilder.append(maker.Ident(names[i])); } List<JCExpression> invokeAddExpr = invokeAddExprBuilder.toList(); JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, invokeAddExpr); statements.append(maker.Exec(invokeAdd)); if (returnStatement != null) statements.append(returnStatement); JCBlock body = maker.Block(0, statements.toList()); Name methodName = data.getSingularName(); long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); if (!fluent) methodName = builderType.toName(HandlerUtil.buildAccessorName(getAddMethodName(), methodName.toString())); ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>(); for (int i = 0; i < suffixes.size(); i++) { JCExpression pt = cloneParamType(i, maker, data.getTypeArgs(), builderType, source); JCVariableDecl p = maker.VarDef(maker.Modifiers(paramFlags), names[i], pt, null); params.append(p); } JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params.toList(), thrown, body, null); injectMethod(builderType, method); }
protected void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) { List<JCTypeParameter> typeParams = List.nil(); List<JCExpression> thrown = List.nil(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, source)); JCExpression thisDotFieldDotAddAll = chainDots(builderType, "this", data.getPluralName().toString(), getAddMethodName() + "All"); JCExpression invokeAddAll = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAddAll, List.<JCExpression>of(maker.Ident(data.getPluralName()))); statements.append(maker.Exec(invokeAddAll)); if (returnStatement != null) statements.append(returnStatement); JCBlock body = maker.Block(0, statements.toList()); Name methodName = data.getPluralName(); long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext()); if (!fluent) methodName = builderType.toName(HandlerUtil.buildAccessorName(getAddMethodName() + "All", methodName.toString())); JCExpression paramType; String aaTypeName = getAddAllTypeName(); if (aaTypeName.startsWith("java.lang.") && aaTypeName.indexOf('.', 11) == -1) { paramType = genJavaLangTypeRef(builderType, aaTypeName.substring(10)); } else { paramType = chainDotsString(builderType, aaTypeName); } paramType = addTypeArgs(getTypeArgumentsCount(), true, builderType, paramType, data.getTypeArgs(), source); JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null); JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, List.of(param), thrown, body, null); injectMethod(builderType, method); }
public void visitTypeParameter(JCTypeParameter that) { try { print("JCTypeParameter:"); } catch (Exception e) { } super.visitTypeParameter(that); }
/** If type parameter list is non-empty, print it enclosed in "<...>" brackets. */ public void printTypeParameters(List<JCTypeParameter> trees) throws IOException { if (trees.nonEmpty()) { print("<"); printExprs(trees); print(">"); } }