diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index ce31bddf7..75e159b9c 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -205,7 +205,16 @@ private ConfigurationKeys() {} * If set, any usage of {@code @Data} results in a warning / error. */ public static final ConfigurationKey DATA_FLAG_USAGE = new ConfigurationKey("lombok.data.flagUsage", "Emit a warning or error if @Data is used.") {}; - + + // ----- GSet ----- + + /** + * lombok configuration: {@code lombok.gset.flagUsage} = {@code WARNING} | {@code ERROR}. + * + * If set, any usage of {@code @GSet} results in a warning / error. + */ + public static final ConfigurationKey GSET_FLAG_USAGE = new ConfigurationKey("lombok.gset.flagUsage", "Emit a warning or error if @GSet is used.") {}; + // ----- Value ----- /** diff --git a/src/core/lombok/GSet.java b/src/core/lombok/GSet.java new file mode 100644 index 000000000..46ff6a65a --- /dev/null +++ b/src/core/lombok/GSet.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2009-2017 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; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/* + * Replaces the need to use both Getter and Setter annotations together + * + * This annotation can be applied directly to a class + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.SOURCE) +public @interface GSet { } diff --git a/src/core/lombok/javac/handlers/HandleGSet.java b/src/core/lombok/javac/handlers/HandleGSet.java new file mode 100644 index 000000000..f29a84c78 --- /dev/null +++ b/src/core/lombok/javac/handlers/HandleGSet.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2009-2025 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.*; +import static lombok.javac.handlers.JavacHandlerUtil.*; +import lombok.AccessLevel; +import lombok.ConfigurationKeys; +import lombok.GSet; +import lombok.core.AnnotationValues; +import lombok.javac.JavacAnnotationHandler; +import lombok.javac.JavacNode; +import lombok.spi.Provides; + +import com.sun.tools.javac.tree.JCTree.JCAnnotation; +import com.sun.tools.javac.util.List; + +/** + * Handles the {@code lombok.GSet} annotation for javac. + */ +@Provides +public class HandleGSet extends JavacAnnotationHandler{ + + private HandleGetter handleGetter = new HandleGetter(); + private HandleSetter handleSetter = new HandleSetter(); + + + @Override + public void handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode){ + handleFlagUsage(annotationNode, ConfigurationKeys.GSET_FLAG_USAGE, "@GSet"); + + deleteAnnotationIfNeccessary(annotationNode, GSet.class); + JavacNode typeNode = annotationNode.up(); + + if (!isClass(typeNode)) { + annotationNode.addError("@GSet is only supported on a class."); + return; + } + + String staticConstructorName = annotation.getInstance().staticConstructor(); + + handleGetter.generateGetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true, List.nil()); + handleSetter.generateSetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true, List.nil(), List.nil()); + } +}