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.maven.di;
20  
21  import java.lang.reflect.ParameterizedType;
22  import java.lang.reflect.Type;
23  import java.util.Objects;
24  
25  import org.apache.maven.api.annotations.Nullable;
26  import org.apache.maven.di.impl.ReflectionUtils;
27  import org.apache.maven.di.impl.Types;
28  
29  /**
30   * A binding key that uniquely identifies a dependency in the injection system.
31   * <p>
32   * Keys combine a type with an optional qualifier to uniquely identify dependencies
33   * within the injection system. They also serve as type tokens, allowing preservation
34   * of generic type information at runtime.
35   * <p>
36   * Example usage:
37   * <pre>
38   * // Simple key for a type
39   * Key&lt;Service&gt; simple = Key.of(Service.class);
40   *
41   * // Key with generic type information
42   * Key&lt;List&lt;String&gt;&gt; generic = new Key&lt;List&lt;String&gt;&gt;(){};
43   *
44   * // Key with qualifier
45   * Key&lt;Service&gt; qualified = Key.of(Service.class, "primary");
46   * </pre>
47   *
48   * @param <T> The type this key represents
49   * @since 4.0.0
50   */
51  public abstract class Key<T> {
52      private final Type type;
53      private final @Nullable Object qualifier;
54  
55      private int hash;
56  
57      protected Key() {
58          this(null);
59      }
60  
61      protected Key(@Nullable Object qualifier) {
62          this.type = Types.simplifyType(getTypeParameter());
63          this.qualifier = qualifier;
64      }
65  
66      protected Key(Type type, @Nullable Object qualifier) {
67          this.type = Types.simplifyType(type);
68          this.qualifier = qualifier;
69      }
70  
71      static final class KeyImpl<T> extends Key<T> {
72          KeyImpl(Type type, Object qualifier) {
73              super(type, qualifier);
74          }
75      }
76  
77      /**
78       * Creates a new Key instance for the specified type.
79       *
80       * @param <T> the type parameter
81       * @param type the Class object representing the type
82       * @return a new Key instance
83       * @throws NullPointerException if type is null
84       */
85      public static <T> Key<T> of(Class<T> type) {
86          return new KeyImpl<>(type, null);
87      }
88  
89      /**
90       * Creates a new Key instance for the specified type with a qualifier.
91       *
92       * @param <T> the type parameter
93       * @param type the Class object representing the type
94       * @param qualifier the qualifier object (typically an annotation instance)
95       * @return a new Key instance
96       * @throws NullPointerException if type is null
97       */
98      public static <T> Key<T> of(Class<T> type, @Nullable Object qualifier) {
99          return new KeyImpl<>(type, qualifier);
100     }
101 
102     public static <T> Key<T> ofType(Type type) {
103         return new KeyImpl<>(type, null);
104     }
105 
106     public static <T> Key<T> ofType(Type type, @Nullable Object qualifier) {
107         return new KeyImpl<>(type, qualifier);
108     }
109 
110     private Type getTypeParameter() {
111         // this cannot possibly fail so not even a check here
112         Type typeArgument = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
113         Object outerInstance = ReflectionUtils.getOuterClassInstance(this);
114         //		// the outer instance is null in static context
115         return outerInstance != null
116                 ? Types.bind(typeArgument, Types.getAllTypeBindings(outerInstance.getClass()))
117                 : typeArgument;
118     }
119 
120     /**
121      * Returns the actual type represented by this key.
122      * <p>
123      * This includes full generic type information if available.
124      *
125      * @return the type represented by this key
126      */
127     public Type getType() {
128         return type;
129     }
130 
131     /**
132      * A shortcut for <code>{@link Types#getRawType(Type)}(key.getType())</code>.
133      * Also casts the result to a properly parameterized class.
134      */
135     @SuppressWarnings("unchecked")
136     public Class<T> getRawType() {
137         return (Class<T>) Types.getRawType(type);
138     }
139 
140     /**
141      * Returns a type parameter of the underlying type wrapped as a key with no qualifier.
142      *
143      * @throws IllegalStateException when underlying type is not a parameterized one.
144      */
145     public <U> Key<U> getTypeParameter(int index) {
146         if (type instanceof ParameterizedType parameterizedType) {
147             return new KeyImpl<>(parameterizedType.getActualTypeArguments()[index], null);
148         }
149         throw new IllegalStateException("Expected type from key " + getDisplayString() + " to be parameterized");
150     }
151 
152     /**
153      * Returns the qualifier associated with this key, if any.
154      *
155      * @return the qualifier object or null if none exists
156      */
157     public @Nullable Object getQualifier() {
158         return qualifier;
159     }
160 
161     /**
162      * Returns an underlying type with display string formatting (package names stripped)
163      * and prepended qualifier display string if this key has a qualifier.
164      */
165     public String getDisplayString() {
166         StringBuilder result = new StringBuilder();
167         if (qualifier instanceof String s) {
168             if (s.isEmpty()) {
169                 result.append("@Named ");
170             } else {
171                 result.append("@Named(\"").append(s).append("\") ");
172             }
173         } else if (qualifier != null) {
174             ReflectionUtils.getDisplayString(result, qualifier);
175             result.append(" ");
176         }
177         result.append(ReflectionUtils.getDisplayName(type));
178         return result.toString();
179     }
180 
181     @Override
182     public boolean equals(Object o) {
183         if (this == o) {
184             return true;
185         }
186         if (o instanceof Key<?> that) {
187             return type.equals(that.type) && Objects.equals(qualifier, that.qualifier);
188         } else {
189             return false;
190         }
191     }
192 
193     @Override
194     public int hashCode() {
195         int hashCode = hash;
196         if (hashCode == 0) {
197             hash = 31 * type.hashCode() + (qualifier == null ? 0 : qualifier.hashCode());
198         }
199         return hash;
200     }
201 
202     @Override
203     public String toString() {
204         return getDisplayString();
205     }
206 }