001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020import static org.apache.juneau.commons.utils.ThrowableUtils.*;
021
022import java.lang.annotation.*;
023import java.lang.reflect.*;
024import java.util.*;
025import java.util.stream.*;
026
027import org.apache.juneau.commons.bean.*;
028import org.apache.juneau.commons.reflect.*;
029import org.apache.juneau.commons.svl.*;
030
031/**
032 * A list of {@link AnnotationWork} objects.
033 *
034 * @serial exclude
035 */
036@SuppressWarnings({
037   "java:S1192" // Duplicated literals (argument/property names) read more clearly inline than as constants
038})
039public class AnnotationWorkList extends ArrayList<AnnotationWork> {
040
041   private static final long serialVersionUID = 1L;
042
043   /**
044    * Static creator.
045    *
046    * @return A new list.
047    */
048   public static AnnotationWorkList create() {
049      return new AnnotationWorkList(VarResolver.DEFAULT.createSession());
050   }
051
052   /**
053    * Static creator.
054    *
055    * @param vrs The variable resolver. Must not be <jk>null</jk>.
056    * @return A new list.
057    */
058   public static AnnotationWorkList create(VarResolverSession vrs) {
059      return new AnnotationWorkList(vrs);
060   }
061
062   /**
063    * Static creator.
064    *
065    * @param annotations The annotations to create work from. Must not be <jk>null</jk>.
066    * @return A new list.
067    */
068   public static AnnotationWorkList of(Stream<AnnotationInfo<? extends Annotation>> annotations) {
069      return create().add(annotations);
070   }
071
072   /**
073    * Static creator.
074    *
075    * @param vrs The variable resolver. Must not be <jk>null</jk>.
076    * @param annotations The annotations to create work from. Must not be <jk>null</jk>.
077    * @return A new list.
078    */
079   public static AnnotationWorkList of(VarResolverSession vrs, Stream<AnnotationInfo<? extends Annotation>> annotations) {
080      return create(vrs).add(annotations);
081   }
082
083   private final transient VarResolverSession vrs;
084
085   private AnnotationWorkList(VarResolverSession vrs) {
086      this.vrs = reqnn("vrs", vrs);
087   }
088
089   @Override /* Overridden from Object */
090   public boolean equals(Object o) {
091      return this == o || (o instanceof AnnotationWorkList o2 && super.equals(o2)); // HTT: super.equals(false) requires populating lists with matching AnnotationWork instances; not practical to test
092   }
093
094   @Override /* Overridden from Object */
095   public int hashCode() {
096      return super.hashCode();
097   }
098
099   /**
100    * Adds an entry to this list.
101    *
102    * @param ai The annotation being applied.
103    * @param aa The applier for the annotation.
104    * @return This object.
105    */
106   public AnnotationWorkList add(AnnotationInfo<?> ai, AnnotationApplier<Annotation,Object> aa) {
107      add(new AnnotationWork(ai, aa));
108      return this;
109   }
110
111   /**
112    * Adds entries for the specified annotations to this work list.
113    *
114    * @param annotations The annotations to create work from. Must not be <jk>null</jk>.
115    * @return This object.
116    */
117   public AnnotationWorkList add(Stream<AnnotationInfo<? extends Annotation>> annotations) {
118      annotations.sorted(Comparator.comparingInt(AnnotationInfo::getRank)).forEach(this::applyAnnotation);
119      return this;
120   }
121
122   /**
123    * Helper method to extract and apply annotation appliers for a given annotation.
124    *
125    * @param ai The annotation info to process.
126    */
127   @SuppressWarnings({
128      "unchecked" // Type erasure requires cast for annotation applier instantiation
129   })
130   private void applyAnnotation(AnnotationInfo<?> ai) {
131      var a = ai.inner();
132      // Special-case @BeanConfig from juneau-commons. The annotation cannot reference @ContextApply
133      // (which lives in juneau-marshall), so we route it to its Applier directly.
134      if (a.annotationType() == BeanConfig.class) {
135         add(ai, (AnnotationApplier<Annotation,Object>)(AnnotationApplier<?,?>) new BeanConfigAnnotation.Applier(vrs));
136         return;
137      }
138      var cpa = chknn(a.annotationType().getAnnotation(ContextApply.class), "Annotation found without @ContextApply: %s", cn(ai.annotationType()));
139      Arrays.stream(cpa.value())
140         .map(x -> safe(() -> (Constructor<? extends AnnotationApplier<?,?>>)x.getConstructor(VarResolverSession.class)))
141         .forEach(applyConstructor -> {
142            var applier = safe(() -> (AnnotationApplier<Annotation,Object>)applyConstructor.newInstance(vrs));
143            add(ai, applier);
144         });
145   }
146}