|
| 1 | +package com.baeldung.singleton.handlers; |
| 2 | + |
| 3 | +import com.baeldung.singleton.Singleton; |
| 4 | +import lombok.core.AnnotationValues; |
| 5 | +import lombok.eclipse.EclipseAnnotationHandler; |
| 6 | +import lombok.eclipse.EclipseNode; |
| 7 | +import lombok.eclipse.handlers.EclipseHandlerUtil; |
| 8 | +import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; |
| 9 | +import org.eclipse.jdt.internal.compiler.ast.Annotation; |
| 10 | +import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration; |
| 11 | +import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; |
| 12 | +import org.eclipse.jdt.internal.compiler.ast.FieldReference; |
| 13 | +import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; |
| 14 | +import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; |
| 15 | +import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; |
| 16 | +import org.eclipse.jdt.internal.compiler.ast.Statement; |
| 17 | +import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; |
| 18 | +import org.eclipse.jdt.internal.compiler.ast.TypeReference; |
| 19 | +import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
| 20 | +import org.kohsuke.MetaInfServices; |
| 21 | + |
| 22 | +import static lombok.eclipse.Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; |
| 23 | +import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccFinal; |
| 24 | +import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccPrivate; |
| 25 | +import static org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants.AccStatic; |
| 26 | + |
| 27 | +@MetaInfServices(EclipseAnnotationHandler.class) |
| 28 | +public class SingletonEclipseHandler extends EclipseAnnotationHandler<Singleton> { |
| 29 | + |
| 30 | + |
| 31 | + @Override |
| 32 | + public void handle(AnnotationValues<Singleton> annotation, Annotation ast, EclipseNode annotationNode) { |
| 33 | + |
| 34 | + //remove annotation |
| 35 | + EclipseHandlerUtil.unboxAndRemoveAnnotationParameter(ast, "onType", "@Singleton(onType=", annotationNode); |
| 36 | + |
| 37 | + //add private constructor |
| 38 | + EclipseNode singletonClass = annotationNode.up(); |
| 39 | + |
| 40 | + TypeDeclaration singletonClassType = (TypeDeclaration) singletonClass.get(); |
| 41 | + ConstructorDeclaration constructor = addConstructor(singletonClass, singletonClassType); |
| 42 | + |
| 43 | + TypeReference singletonTypeRef = EclipseHandlerUtil.cloneSelfType(singletonClass, singletonClassType); |
| 44 | + |
| 45 | + //add inner class |
| 46 | + |
| 47 | + //add instance field |
| 48 | + StringBuilder sb = new StringBuilder(); |
| 49 | + sb.append(singletonClass.getName()); |
| 50 | + sb.append("Holder"); |
| 51 | + String innerClassName = sb.toString(); |
| 52 | + TypeDeclaration innerClass = new TypeDeclaration(singletonClassType.compilationResult); |
| 53 | + innerClass.modifiers = AccPrivate | AccStatic; |
| 54 | + innerClass.name = innerClassName.toCharArray(); |
| 55 | + |
| 56 | + FieldDeclaration instanceVar = addInstanceVar(constructor, singletonTypeRef, innerClass); |
| 57 | + |
| 58 | + FieldDeclaration[] declarations = new FieldDeclaration[]{instanceVar}; |
| 59 | + innerClass.fields = declarations; |
| 60 | + |
| 61 | + EclipseHandlerUtil.injectType(singletonClass, innerClass); |
| 62 | + |
| 63 | + addFactoryMethod(singletonClass, singletonClassType, singletonTypeRef, innerClass, instanceVar); |
| 64 | + |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + private void addFactoryMethod(EclipseNode singletonClass, TypeDeclaration astNode, TypeReference typeReference, TypeDeclaration innerClass, FieldDeclaration field) { |
| 69 | + MethodDeclaration factoryMethod = new MethodDeclaration(astNode.compilationResult); |
| 70 | + factoryMethod.modifiers = AccStatic | ClassFileConstants.AccPublic; |
| 71 | + factoryMethod.returnType = typeReference; |
| 72 | + factoryMethod.sourceStart = astNode.sourceStart; |
| 73 | + factoryMethod.sourceEnd = astNode.sourceEnd; |
| 74 | + factoryMethod.selector = "getInstance".toCharArray(); |
| 75 | + factoryMethod.bits = ECLIPSE_DO_NOT_TOUCH_FLAG; |
| 76 | + |
| 77 | + long pS = factoryMethod.sourceStart; |
| 78 | + long pE = factoryMethod.sourceEnd; |
| 79 | + long p = (long) pS << 32 | pE; |
| 80 | + |
| 81 | + FieldReference ref = new FieldReference(field.name, p); |
| 82 | + ref.receiver = new SingleNameReference(innerClass.name, p); |
| 83 | + |
| 84 | + ReturnStatement statement = new ReturnStatement(ref, astNode.sourceStart, astNode.sourceEnd); |
| 85 | + |
| 86 | + factoryMethod.statements = new Statement[]{statement}; |
| 87 | + |
| 88 | + EclipseHandlerUtil.injectMethod(singletonClass, factoryMethod); |
| 89 | + } |
| 90 | + |
| 91 | + private FieldDeclaration addInstanceVar(ConstructorDeclaration constructor, TypeReference typeReference, TypeDeclaration innerClass) { |
| 92 | + FieldDeclaration field = new FieldDeclaration(); |
| 93 | + field.modifiers = AccPrivate | AccStatic | AccFinal; |
| 94 | + field.name = "INSTANCE".toCharArray(); |
| 95 | + |
| 96 | + field.type = typeReference; |
| 97 | + |
| 98 | + AllocationExpression exp = new AllocationExpression(); |
| 99 | + exp.type = typeReference; |
| 100 | + exp.binding = constructor.binding; |
| 101 | + exp.sourceStart = innerClass.sourceStart; |
| 102 | + exp.sourceEnd = innerClass.sourceEnd; |
| 103 | + |
| 104 | + field.initialization = exp; |
| 105 | + return field; |
| 106 | + } |
| 107 | + |
| 108 | + private ConstructorDeclaration addConstructor(EclipseNode singletonClass, TypeDeclaration astNode) { |
| 109 | + ConstructorDeclaration constructor = new ConstructorDeclaration(astNode.compilationResult); |
| 110 | + constructor.modifiers = AccPrivate; |
| 111 | + constructor.selector = astNode.name; |
| 112 | + constructor.sourceStart = astNode.sourceStart; |
| 113 | + constructor.sourceEnd = astNode.sourceEnd; |
| 114 | + constructor.thrownExceptions = null; |
| 115 | + constructor.typeParameters = null; |
| 116 | + constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; |
| 117 | + constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = astNode.sourceStart; |
| 118 | + constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = astNode.sourceEnd; |
| 119 | + constructor.arguments = null; |
| 120 | + |
| 121 | + EclipseHandlerUtil.injectMethod(singletonClass, constructor); |
| 122 | + return constructor; |
| 123 | + } |
| 124 | +} |
0 commit comments