All Downloads are FREE. Search and download functionalities are using the official Maven repository.

groovy.transform.Final Maven / Gradle / Ivy

There is a newer version: 5.0.0-alpha-11
Show newest version
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package groovy.transform;

import org.codehaus.groovy.transform.GroovyASTTransformationClass;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Annotation to add the final modifier to classes, methods, constructors, and fields.
 * Using `{@code final}` and directly using `{@code @Final}` will have the same result.
 * However, the intention is almost never to use `{@code @Final}` directly but rather as part
 * of an annotation collector (meta-annotation).
 *
 * If you like the behavior of an existing annotation but would really like a version that
 * also ensured the respective annotated node was final, you can create such an element, e.g.:
 * 
 * @AnnotationCollector
 * @Canonical
 * @Final
 * @interface MyCanonical {}
 *
 * @MyCanonical class Foo {}
 * 
* Here, class {@code Foo} will be final as well as having all the normal {@code Canonical} enhancements. * *

* Similarly, if you wanted to, you could define: *

 * @AnnotationCollector([Singleton, Final]) @interface MySingleton {}
 * 
* Classes annotated with @MySingleton would be final as well as have all the {@code Singleton} enhancements. *

* *

* As another example, you could define: *

 * @AnnotationCollector([NullCheck, Final, AutoFinal]) @interface MyNullCheck {}
 * 
* Methods annotated with @MyNullCheck would be final (from @Final), * would have all parameters marked final (from @AutoFinal), and * would have all parameters checked against {@code null} (from @NullCheck). *

* In general, it would be bad style to have an explicit {@code final} modifier and a {@code @Final} annotation * (or more than one {@code @Final} annotation), * but in that scenario if there is an explicit {@code final} or at least one enabled {@code @Final}, * then the annotated class or member will be final. * * @since 4.0.0 */ @java.lang.annotation.Documented @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD}) @GroovyASTTransformationClass("org.codehaus.groovy.transform.FinalASTTransformation") public @interface Final { /** * When disabled, this annotation effectively becomes a no-op. * Typically only used to override an annotation collector already containing an enabled {@code @Final} annotation. * Care must be taken when disabling final in this way since the annotation collector probably had good reason for enabling final. */ boolean enabled() default true; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy