View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.weaver.model;
20  
21  import java.lang.reflect.Constructor;
22  import java.lang.reflect.Field;
23  import java.lang.reflect.Method;
24  import java.util.Collections;
25  import java.util.Comparator;
26  import java.util.concurrent.ConcurrentNavigableMap;
27  import java.util.concurrent.ConcurrentSkipListMap;
28  
29  import org.apache.commons.weaver.utils.Args;
30  
31  /**
32   * {@link Weavable} {@link Class}.
33   *
34   * @param <T> type
35   */
36  public class WeavableClass<T> extends NestedWeavable<WeavableClass<T>, Class<T>, WeavablePackage, Package> {
37      private static final Comparator<Class<?>> CLASS_COMPARATOR = Comparator.comparing(Class::getName);
38  
39      private final ConcurrentNavigableMap<String, WeavableField<T>> fields = new ConcurrentSkipListMap<>();
40  
41      private final ConcurrentNavigableMap<Constructor<T>, WeavableConstructor<T>> ctors = new ConcurrentSkipListMap<>(
42          (ctor1, ctor2) -> Args.compare(ctor1.getParameterTypes(), ctor2.getParameterTypes()));
43  
44      private final ConcurrentNavigableMap<Method, WeavableMethod<T>> methods =
45          new ConcurrentSkipListMap<>((methd1, methd2) -> {
46              final int result = methd1.getName().compareTo(methd2.getName());
47              return result == 0 ? Args.compare(methd1.getParameterTypes(), methd2.getParameterTypes()) : result;
48          });
49  
50      /**
51       * Create a new {@link WeavableClass} instance.
52       * @param target {@link Class}
53       * @param parent {@link WeavablePackage} enclosing
54       */
55      public WeavableClass(final Class<T> target, final WeavablePackage parent) {
56          super(target, parent);
57      }
58  
59      /**
60       * Get a {@link WeavableField} representing {@code fld}.
61       * @param fld to wrap
62       * @return {@link WeavableField}
63       */
64      public WeavableField<T> getWeavable(final Field fld) {
65          return fields.computeIfAbsent(fld.getName(), k -> new WeavableField<>(fld, this));
66      }
67  
68      /**
69       * Get a {@link WeavableMethod} representing {@code mt}.
70       * @param methd to wrap
71       * @return {@link WeavableMethod}
72       */
73      public WeavableMethod<T> getWeavable(final Method methd) {
74          return methods.computeIfAbsent(methd, k -> new WeavableMethod<>(methd, this));
75      }
76  
77      /**
78       * Get a {@link WeavableConstructor} representing {@code ctor}.
79       * @param ctor to wrap
80       * @return {@link WeavableConstructor}
81       */
82      public WeavableConstructor<T> getWeavable(final Constructor<T> ctor) {
83          return ctors.computeIfAbsent(ctor, k -> new WeavableConstructor<>(ctor, this));
84      }
85  
86      /**
87       * Get {@link WeavableField}s of this {@link WeavableClass}.
88       * @return {@link Iterable}
89       */
90      public Iterable<WeavableField<T>> getFields() {
91          return Collections.unmodifiableCollection(fields.values());
92      }
93  
94      /**
95       * Get {@link WeavableConstructor}s of this {@link WeavableClass}.
96       * @return {@link Iterable}
97       */
98      public Iterable<WeavableConstructor<T>> getConstructors() {
99          return Collections.unmodifiableCollection(ctors.values());
100     }
101 
102     /**
103      * Get {@link WeavableMethod}s of this {@link WeavableClass}.
104      * @return {@link Iterable}
105      */
106     public Iterable<WeavableMethod<T>> getMethods() {
107         return Collections.unmodifiableCollection(methods.values());
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     protected int localCompareTo(final WeavableClass<T> obj) {
115         return obj == null ? 1 : CLASS_COMPARATOR.compare(getTarget(), obj.getTarget());
116     }
117 }