private void addInstanceVar(JavacNode singletonClass, JavacTreeMaker singletonClassTM, JavacNode holderClass) { JCTree.JCModifiers fieldMod = singletonClassTM.Modifiers(Flags.PRIVATE | Flags.STATIC | Flags.FINAL); JCTree.JCClassDecl singletonClassDecl = (JCTree.JCClassDecl) singletonClass.get(); JCTree.JCIdent singletonClassType = singletonClassTM.Ident(singletonClassDecl.name); JCTree.JCNewClass newKeyword = singletonClassTM.NewClass(null, List.nil(), singletonClassType, List.nil(), null); JCTree.JCVariableDecl instanceVar = singletonClassTM.VarDef(fieldMod, singletonClass.toName("INSTANCE"), singletonClassType, newKeyword); JavacHandlerUtil.injectField(holderClass, instanceVar); }
private JCTree.JCBlock addReturnBlock(JavacTreeMaker singletonClassTreeMaker, JavacNode holderInnerClass) { JCTree.JCClassDecl holderInnerClassDecl = (JCTree.JCClassDecl) holderInnerClass.get(); JavacTreeMaker holderInnerClassTreeMaker = holderInnerClass.getTreeMaker(); JCTree.JCIdent holderInnerClassType = holderInnerClassTreeMaker.Ident(holderInnerClassDecl.name); JCTree.JCFieldAccess instanceVarAccess = holderInnerClassTreeMaker.Select(holderInnerClassType, holderInnerClass.toName("INSTANCE")); JCTree.JCReturn returnValue = singletonClassTreeMaker.Return(instanceVarAccess); ListBuffer<JCTree.JCStatement> statements = new ListBuffer<>(); statements.append(returnValue); return singletonClassTreeMaker.Block(0L, statements.toList()); }
private JavacNode addInnerClass(JavacNode singletonClass, JavacTreeMaker singletonTM) { JCTree.JCModifiers modifiers = singletonTM.Modifiers(Flags.PRIVATE | Flags.STATIC); String innerClassName = singletonClass.getName() + "Holder"; JCTree.JCClassDecl innerClassDecl = singletonTM.ClassDef(modifiers, singletonClass.toName(innerClassName), List.nil(), null, List.nil(), List.nil()); return JavacHandlerUtil.injectType(singletonClass, innerClassDecl); }
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); }
@Override public void handle(AnnotationValues<Singleton> annotation, JCTree.JCAnnotation ast, JavacNode annotationNode) { Context context = annotationNode.getContext(); Javac8BasedLombokOptions options = Javac8BasedLombokOptions.replaceWithDelombokOptions(context); options.deleteLombokAnnotations(); //remove annotation deleteAnnotationIfNeccessary(annotationNode, Singleton.class); //remove import deleteImportFromCompilationUnit(annotationNode, "lombok.AccessLevel"); //private constructor JavacNode singletonClass = annotationNode.up(); JavacTreeMaker singletonClassTreeMaker = singletonClass.getTreeMaker(); addPrivateConstructor(singletonClass, singletonClassTreeMaker); //singleton holder JavacNode holderInnerClass = addInnerClass(singletonClass, singletonClassTreeMaker); //inject static field to this addInstanceVar(singletonClass, singletonClassTreeMaker, holderInnerClass); //add factory method addFactoryMethod(singletonClass, singletonClassTreeMaker, holderInnerClass); }
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); }
public static TypeTag typeTag(JCTree o) { try { return new TypeTag(getFieldCached(FIELD_CACHE, o, "typetag")); } catch (NoSuchFieldException e) { throw Javac.sneakyThrow(e); } }
public static TreeTag treeTag(String identifier) { return new TreeTag(getFieldCached(TREE_TAG_CACHE, Javac.getJavaCompilerVersion() < 8 ? "com.sun.tools.javac.tree.JCTree" : "com.sun.tools.javac.tree.JCTree$Tag", identifier)); }
public static TypeTag typeTag(String identifier) { return new TypeTag(getFieldCached(TYPE_TAG_CACHE, Javac.getJavaCompilerVersion() < 8 ? "com.sun.tools.javac.code.TypeTags" : "com.sun.tools.javac.code.TypeTag", identifier)); } }
public JCEnhancedForLoop ForeachLoop(JCVariableDecl var, JCExpression expr, JCStatement body) { return invoke(ForeachLoop, var, expr, body); }
/** * Creates a new method ID based on the name of the method to invoke, the return type of that method, and the types of the parameters. * * A method matches if the return type matches, and for each parameter the following holds: * * Either (A) the type listed here is the same as, or a subtype of, the type of the method in javac's TreeMaker, or * (B) the type listed here is a subtype of SchroedingerType. */ static <J> MethodId<J> MethodId(String name, Class<J> returnType, Class<?>... types) { return new MethodId<J>(TreeMaker.class, name, returnType, types); }
public TreeMirrorMaker(JavacTreeMaker maker, Context context) { super(maker.getUnderlyingTreeMaker()); }
public JCExpression Ident(JCVariableDecl param) { return invoke(IdentVarDecl, param); }
static <J> MethodId<J> MethodId(Class<?> owner, String name, Class<J> returnType, Class<?>... types) { return new MethodId<J>(owner, name, returnType, types); }
public JCMethodInvocation App(JCExpression meth) { return invoke(App1, meth); }
public JCAnnotation TypeAnnotation(Attribute a) { return invoke(TypeAnnotationWithAttributeOnly, a); }
public JCExpression Type(Type type) { return invoke(Type, type); } }
public JCDoWhileLoop DoLoop(JCStatement body, JCExpression cond) { return invoke(DoLoop, body, cond); }
public JCArrayAccess Indexed(JCExpression indexed, JCExpression index) { return invoke(Indexed, indexed, index); }