• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.turbine.binder.bound;
18 
19 import static com.google.common.base.MoreObjects.firstNonNull;
20 
21 import com.google.common.collect.ImmutableSet;
22 import com.google.turbine.binder.sym.ClassSymbol;
23 import com.google.turbine.model.TurbineElementType;
24 import java.lang.annotation.RetentionPolicy;
25 import java.util.EnumSet;
26 import org.jspecify.annotations.Nullable;
27 
28 /**
29  * Annotation metadata, e.g. from {@link java.lang.annotation.Target}, {@link
30  * java.lang.annotation.Retention}, and {@link java.lang.annotation.Repeatable}.
31  */
32 public class AnnotationMetadata {
33 
34   public static final ImmutableSet<TurbineElementType> DEFAULT_TARGETS = getDefaultElements();
35 
getDefaultElements()36   private static ImmutableSet<TurbineElementType> getDefaultElements() {
37     EnumSet<TurbineElementType> values = EnumSet.allOf(TurbineElementType.class);
38     values.remove(TurbineElementType.TYPE_PARAMETER);
39     values.remove(TurbineElementType.TYPE_USE);
40     return ImmutableSet.copyOf(values);
41   }
42 
43   private final RetentionPolicy retention;
44   private final ImmutableSet<TurbineElementType> target;
45   private final @Nullable ClassSymbol repeatable;
46 
AnnotationMetadata( @ullable RetentionPolicy retention, @Nullable ImmutableSet<TurbineElementType> annotationTarget, @Nullable ClassSymbol repeatable)47   public AnnotationMetadata(
48       @Nullable RetentionPolicy retention,
49       @Nullable ImmutableSet<TurbineElementType> annotationTarget,
50       @Nullable ClassSymbol repeatable) {
51     this.retention = firstNonNull(retention, RetentionPolicy.CLASS);
52     this.target = firstNonNull(annotationTarget, DEFAULT_TARGETS);
53     this.repeatable = repeatable;
54   }
55 
56   /** The retention policy specified by the {@code @Retention} meta-annotation. */
retention()57   public RetentionPolicy retention() {
58     return retention;
59   }
60 
61   /** Target element types specified by the {@code @Target} meta-annotation. */
target()62   public ImmutableSet<TurbineElementType> target() {
63     return target;
64   }
65 
66   /** The container annotation for {@code @Repeated} annotations. */
repeatable()67   public @Nullable ClassSymbol repeatable() {
68     return repeatable;
69   }
70 }
71