From 10bced0f856db15571c83e4e928dbe7c521f1e72 Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Mon, 8 Dec 2014 09:29:14 -0200 Subject: Added @UtilityClass annotation and javac handler --- src/core/lombok/ConfigurationKeys.java | 8 ++ src/core/lombok/experimental/UtilityClass.java | 37 ++++++ .../lombok/javac/handlers/HandleUtilityClass.java | 134 +++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 src/core/lombok/experimental/UtilityClass.java create mode 100644 src/core/lombok/javac/handlers/HandleUtilityClass.java diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index 301563b8..27507657 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -376,6 +376,14 @@ public class ConfigurationKeys { */ public static final ConfigurationKey WITHER_FLAG_USAGE = new ConfigurationKey("lombok.wither.flagUsage", "Emit a warning or error if @Wither is used.") {}; + // ----- UtilityClass ----- + + /** + * lombok configuration: {@code lombok.utilityClass.flagUsage} = {@code WARNING} | {@code ERROR}. + * + * If set, any usage of {@code @UtilityClass} results in a warning / error. + */ + public static final ConfigurationKey UTLITY_CLASS_FLAG_USAGE = new ConfigurationKey("lombok.utilityClass.flagUsage", "Emit a warning or error if @UtilityClass is used.") {}; // ----- Configuration System ----- diff --git a/src/core/lombok/experimental/UtilityClass.java b/src/core/lombok/experimental/UtilityClass.java new file mode 100644 index 00000000..778cfb80 --- /dev/null +++ b/src/core/lombok/experimental/UtilityClass.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2012-2013 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.experimental; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * A helper to create utilities classes. + *

+ * It makes the class final, makes all its methods static, creates a private default constructor that throws a RuntimeException when called and yields an error when other constructors are defined. + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.SOURCE) +public @interface UtilityClass { +} diff --git a/src/core/lombok/javac/handlers/HandleUtilityClass.java b/src/core/lombok/javac/handlers/HandleUtilityClass.java new file mode 100644 index 00000000..4ab3f8dc --- /dev/null +++ b/src/core/lombok/javac/handlers/HandleUtilityClass.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2012-2014 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.javac.handlers; + +import static lombok.core.handlers.HandlerUtil.handleExperimentalFlagUsage; +import static lombok.javac.handlers.JavacHandlerUtil.*; +import lombok.ConfigurationKeys; +import lombok.core.AST.Kind; +import lombok.core.AnnotationValues; +import lombok.experimental.UtilityClass; +import lombok.javac.JavacAnnotationHandler; +import lombok.javac.JavacNode; +import lombok.javac.JavacTreeMaker; + +import org.mangosdk.spi.ProviderFor; + +import com.sun.tools.javac.code.Flags; +import com.sun.tools.javac.tree.JCTree.JCAnnotation; +import com.sun.tools.javac.tree.JCTree.JCBlock; +import com.sun.tools.javac.tree.JCTree.JCClassDecl; +import com.sun.tools.javac.tree.JCTree.JCExpression; +import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.JCTree.JCModifiers; +import com.sun.tools.javac.tree.JCTree.JCStatement; +import com.sun.tools.javac.tree.JCTree.JCTypeParameter; +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; +import com.sun.tools.javac.util.List; +import com.sun.tools.javac.util.ListBuffer; +import com.sun.tools.javac.util.Name; + +/** + * Handles the {@code lombok.experimental.UtilityClass} annotation for javac. + */ +@ProviderFor(JavacAnnotationHandler.class) public class HandleUtilityClass extends JavacAnnotationHandler { + + @Override public void handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { + handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.UTLITY_CLASS_FLAG_USAGE, "@UtilityClass"); + deleteAnnotationIfNeccessary(annotationNode, UtilityClass.class); + + parse(annotationNode.up(), annotationNode); + } + + private JCClassDecl getClassDecl(JavacNode typeNode) { + JCClassDecl typeDecl = null; + if (typeNode.get() instanceof JCClassDecl) { + typeDecl = (JCClassDecl) typeNode.get(); + } + + long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags; + boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0; + return notAClass ? null : typeDecl; + } + + public void parse(JavacNode typeNode, JavacNode errorNode) { + JCClassDecl classDecl = getClassDecl(typeNode); + if (classDecl == null) { + errorNode.addError("@UtilityClass is only supported on a class (can't be interface, enum or annotation)."); + return; + } + + if (!is(classDecl.getModifiers(), Flags.FINAL)) { + classDecl.mods.flags |= Flags.FINAL; + } + + for (JavacNode element : typeNode.down()) { + if (element.getKind() == Kind.FIELD) { + JCVariableDecl fieldDecl = (JCVariableDecl) element.get(); + if (!is(fieldDecl.mods, Flags.STATIC)) { + fieldDecl.mods.flags |= Flags.STATIC; + } + } else if (element.getKind() == Kind.METHOD) { + JCMethodDecl methodDecl = (JCMethodDecl) element.get(); + if (methodDecl.name.contentEquals("")) { + if (!is(methodDecl.mods, Flags.GENERATEDCONSTR)) { + errorNode.addError("@UtilityClasses cannot have declared constructors."); + continue; + } + } else if (!is(methodDecl.mods, Flags.STATIC)) { + methodDecl.mods.flags |= Flags.STATIC; + } + } else if (element.getKind() == Kind.TYPE) { + JCClassDecl innerClassDecl = (JCClassDecl) typeNode.get(); + if (!is(innerClassDecl.mods, Flags.STATIC)) { + innerClassDecl.mods.flags |= Flags.STATIC; + } + } + } + + createPrivateDefaultConstructor(typeNode); + } + + private static boolean is(JCModifiers mods, long f) { + return (mods.flags & f) != 0; + } + + private void createPrivateDefaultConstructor(JavacNode typeNode) { + JavacTreeMaker maker = typeNode.getTreeMaker(); + JCModifiers mods = maker.Modifiers(Flags.PRIVATE, List.nil()); + + Name name = typeNode.toName(""); + JCBlock block = maker.Block(0L, createThrowStatement(typeNode, maker)); + JCMethodDecl methodDef = maker.MethodDef(mods, name, null, List.nil(), List.nil(), List.nil(), block, null); + JCMethodDecl constructor = recursiveSetGeneratedBy(methodDef, typeNode.get(), typeNode.getContext()); + JavacHandlerUtil.injectMethod(typeNode, constructor); + } + + private List createThrowStatement(JavacNode typeNode, JavacTreeMaker maker) { + ListBuffer statements = new ListBuffer(); + JCExpression exceptionType = genTypeRef(typeNode, "java.lang.RuntimeException"); + JCExpression exceptionInstance = maker.NewClass(null, List.nil(), exceptionType, List.of(maker.Literal("Should not be instanciated!")), null); + JCStatement throwStatement = maker.Throw(exceptionInstance); + statements.add(throwStatement); + return statements.toList(); + } +} -- cgit From b31709e8cc3157d402b5255dca83ce682c8ce545 Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Sat, 31 Jan 2015 07:51:11 -0200 Subject: Adding my name to the authors file --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 9af2bdb1..1b164f06 100644 --- a/AUTHORS +++ b/AUTHORS @@ -2,6 +2,7 @@ Lombok contributors in alphabetical order: Christian Sterzl Jappe van der Hel +Luan Nico Maarten Mulders Philipp Eichhorn Reinier Zwitserloot @@ -10,5 +11,4 @@ Roel Spilker Sander Koning Taiki Sugawara - By adding your name to this list, you grant full and irrevocable copyright and patent indemnity to Project Lombok and all use of Project Lombok, and you certify that you have the right to do so for all commits you add to Project Lombok. -- cgit