Java Code Examples for lombok.javac.JavacTreeMaker#MethodDef
The following examples show how to use
lombok.javac.JavacTreeMaker#MethodDef .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HandleTable.java From sqlitemagic with Apache License 2.0 | 6 votes |
private void generateMethod(JavacTreeMaker maker, JavacNode tableElement, Set<String> allMethodNames, String methodName, String callClassName, String tableClassName) { final String invokeKey = getCreateInvokeKey(callClassName, tableClassName); if (!allMethodNames.contains(methodName)) { final JCTree.JCAssign value = maker.Assign(maker.Ident(tableElement.toName("value")), maker.Literal(invokeKey)); final JCTree.JCAssign useThisAsOnlyParam = maker.Assign(maker.Ident(tableElement.toName("useThisAsOnlyParam")), maker.Literal(true)); final JCTree.JCAnnotation invokesAnnotation = maker.Annotation(chainDotsString(tableElement, Invokes.class.getCanonicalName()), List.<JCTree.JCExpression>of(value, useThisAsOnlyParam)); final JCTree.JCModifiers mods = maker.Modifiers(PUBLIC_FINAL, List.of(invokesAnnotation)); final Name name = tableElement.toName(methodName); final JCTree.JCExpression returnType = getMagicMethodReturnType(tableElement, callClassName, tableClassName); final JCTree.JCBlock body = defaultMagicMethodBody(maker, tableElement); final JCTree.JCMethodDecl method = maker.MethodDef(mods, name, returnType, List.<JCTree.JCTypeParameter>nil(), List.<JCTree.JCVariableDecl>nil(), List.<JCTree.JCExpression>nil(), body, null); injectMethod(tableElement, recursiveSetGeneratedBy(method, tableElement.get(), tableElement.getContext())); } }
Example 2
Source File: HandleTable.java From sqlitemagic with Apache License 2.0 | 6 votes |
private void generateBulkMethod(JavacTreeMaker maker, JavacNode tableElement, Set<String> allStaticMethodNames, String methodName, String callClassName, String tableClassName, CollectionType collectionType) { final String invokeKey = getCreateInvokeKey(callClassName, tableClassName); if (!allStaticMethodNames.contains(methodName)) { final JCTree.JCAnnotation invokesAnnotation = maker.Annotation(chainDotsString(tableElement, Invokes.class.getCanonicalName()), List.<JCTree.JCExpression>of(maker.Literal(invokeKey))); final JCTree.JCModifiers mods = maker.Modifiers(PUBLIC_STATIC, List.of(invokesAnnotation)); final Name name = tableElement.toName(methodName); final JCTree.JCExpression returnType = getMagicMethodReturnType(tableElement, callClassName, tableClassName); final JCTree.JCExpression tableClassType = chainDotsString(tableElement, tableElement.getPackageDeclaration() + "." + tableClassName); final JCTree.JCExpression paramType = maker.TypeApply(collectionType.genericType(tableElement), List.of(tableClassType)); long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, tableElement.getContext()); final JCTree.JCVariableDecl param = maker.VarDef(maker.Modifiers(flags), tableElement.toName("o"), paramType, null); final JCTree.JCBlock body = defaultMagicMethodBody(maker, tableElement); final JCTree.JCMethodDecl method = maker.MethodDef(mods, name, returnType, List.<JCTree.JCTypeParameter>nil(), List.of(param), List.<JCTree.JCExpression>nil(), body, null); injectMethod(tableElement, recursiveSetGeneratedBy(method, tableElement.get(), tableElement.getContext())); } }
Example 3
Source File: HandleTable.java From sqlitemagic with Apache License 2.0 | 6 votes |
private void generateDeleteTable(JavacTreeMaker maker, JavacNode tableElement, Set<String> allStaticMethodNames, String tableClassName) { final String callClassName = CLASS_DELETE_TABLE; final String methodName = METHOD_DELETE_TABLE; final String invokeKey = getCreateInvokeKey(callClassName, tableClassName); if (!allStaticMethodNames.contains(methodName)) { final JCTree.JCAnnotation invokesAnnotation = maker.Annotation(chainDotsString(tableElement, Invokes.class.getCanonicalName()), List.<JCTree.JCExpression>of(maker.Literal(invokeKey))); final JCTree.JCModifiers mods = maker.Modifiers(PUBLIC_STATIC, List.of(invokesAnnotation)); final Name name = tableElement.toName(methodName); final JCTree.JCExpression returnType = getMagicMethodReturnType(tableElement, callClassName, tableClassName); final JCTree.JCBlock body = defaultMagicMethodBody(maker, tableElement); final JCTree.JCMethodDecl method = maker.MethodDef(mods, name, returnType, List.<JCTree.JCTypeParameter>nil(), List.<JCTree.JCVariableDecl>nil(), List.<JCTree.JCExpression>nil(), body, null); injectMethod(tableElement, recursiveSetGeneratedBy(method, tableElement.get(), tableElement.getContext())); } }
Example 4
Source File: HandleBuilder.java From EasyMPermission with MIT License | 6 votes |
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; } */ }
Example 5
Source File: JavacJavaUtilListSetSingularizer.java From EasyMPermission with MIT License | 6 votes |
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); }
Example 6
Source File: JavacJavaUtilListSetSingularizer.java From EasyMPermission with MIT License | 6 votes |
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); }
Example 7
Source File: HandleUtilityClass.java From EasyMPermission with MIT License | 5 votes |
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); }
Example 8
Source File: HandleBuilder.java From EasyMPermission with MIT License | 5 votes |
public JCMethodDecl generateBuilderMethod(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)); return maker.MethodDef(maker.Modifiers(Flags.STATIC | Flags.PUBLIC), type.toName(builderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), copyTypeParams(maker, typeParams), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null); }
Example 9
Source File: JavacJavaUtilMapSingularizer.java From EasyMPermission with MIT License | 5 votes |
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); }
Example 10
Source File: JavacGuavaSingularizer.java From EasyMPermission with MIT License | 5 votes |
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(); boolean mapMode = isMap(); Name keyName = !mapMode ? data.getSingularName() : builderType.toName(data.getSingularName() + "$key"); Name valueName = !mapMode ? null : builderType.toName(data.getSingularName() + "$value"); JCModifiers mods = maker.Modifiers(Flags.PUBLIC); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, mapMode, source)); JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), mapMode ? "put" : "add"); List<JCExpression> invokeAddExpr; if (mapMode) { invokeAddExpr = List.<JCExpression>of(maker.Ident(keyName), maker.Ident(valueName)); } else { invokeAddExpr = List.<JCExpression>of(maker.Ident(keyName)); } 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(mapMode ? "put" : "add", methodName.toString())); List<JCVariableDecl> params; if (mapMode) { JCExpression keyType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source); JCExpression valueType = cloneParamType(1, maker, data.getTypeArgs(), builderType, source); JCVariableDecl paramKey = maker.VarDef(maker.Modifiers(paramFlags), keyName, keyType, null); JCVariableDecl paramValue = maker.VarDef(maker.Modifiers(paramFlags), valueName, valueType, null); params = List.of(paramKey, paramValue); } else { JCExpression paramType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source); params = List.of(maker.VarDef(maker.Modifiers(paramFlags), data.getSingularName(), paramType, null)); } JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null); injectMethod(builderType, method); }
Example 11
Source File: JavacGuavaSingularizer.java From EasyMPermission with MIT License | 5 votes |
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(); boolean mapMode = isMap(); JCModifiers mods = maker.Modifiers(Flags.PUBLIC); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, mapMode, source)); JCExpression thisDotFieldDotAddAll = chainDots(builderType, "this", data.getPluralName().toString(), mapMode ? "putAll" : "addAll"); 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(mapMode ? "putAll" : "addAll", methodName.toString())); JCExpression paramType; if (mapMode) { paramType = chainDots(builderType, "java", "util", "Map"); } else { paramType = genJavaLangTypeRef(builderType, "Iterable"); } paramType = addTypeArgs(mapMode ? 2 : 1, 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); }
Example 12
Source File: SingletonJavacHandler.java From tutorials with MIT License | 5 votes |
private void addFactoryMethod(JavacNode singletonClass, JavacTreeMaker singletonClassTreeMaker, JavacNode holderInnerClass) { JCTree.JCModifiers modifiers = singletonClassTreeMaker.Modifiers(Flags.PUBLIC | Flags.STATIC); JCTree.JCClassDecl singletonClassDecl = (JCTree.JCClassDecl) singletonClass.get(); JCTree.JCIdent singletonClassType = singletonClassTreeMaker.Ident(singletonClassDecl.name); JCTree.JCBlock block = addReturnBlock(singletonClassTreeMaker, holderInnerClass); JCTree.JCMethodDecl factoryMethod = singletonClassTreeMaker.MethodDef(modifiers, singletonClass.toName("getInstance"), singletonClassType, List.nil(), List.nil(), List.nil(), block, null); JavacHandlerUtil.injectMethod(singletonClass, factoryMethod); }
Example 13
Source File: SingletonJavacHandler.java From tutorials with MIT License | 5 votes |
private void addPrivateConstructor(JavacNode singletonClass, JavacTreeMaker singletonTM) { JCTree.JCModifiers modifiers = singletonTM.Modifiers(Flags.PRIVATE); JCTree.JCBlock block = singletonTM.Block(0L, List.nil()); JCTree.JCMethodDecl constructor = singletonTM.MethodDef(modifiers, singletonClass.toName("<init>"), null, List.nil(), List.nil(), List.nil(), block, null); JavacHandlerUtil.injectMethod(singletonClass, constructor); }