View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.internal.classGeneration;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   
9   import java.lang.reflect.ParameterizedType;
10  import java.lang.reflect.Type;
11  
12  import mockit.internal.reflection.GenericTypeReflection;
13  import mockit.internal.util.Utilities;
14  
15  public final class MockedTypeInfo {
16      @NonNull
17      public final GenericTypeReflection genericTypeMap;
18      @NonNull
19      public final String implementationSignature;
20  
21      public MockedTypeInfo(@NonNull Type mockedType) {
22          Class<?> mockedClass = Utilities.getClassType(mockedType);
23          genericTypeMap = new GenericTypeReflection(mockedClass, mockedType);
24  
25          String signature = getGenericClassSignature(mockedType);
26          String classDesc = mockedClass.getName().replace('.', '/');
27          implementationSignature = 'L' + classDesc + signature;
28      }
29  
30      @NonNull
31      private static String getGenericClassSignature(@NonNull Type mockedType) {
32          StringBuilder signature = new StringBuilder(100);
33  
34          if (mockedType instanceof ParameterizedType) {
35              ParameterizedType parameterizedType = (ParameterizedType) mockedType;
36              Type[] typeArguments = parameterizedType.getActualTypeArguments();
37  
38              if (typeArguments.length > 0) {
39                  signature.append('<');
40  
41                  for (Type typeArg : typeArguments) {
42                      if (typeArg instanceof Class<?>) {
43                          Class<?> classArg = (Class<?>) typeArg;
44                          signature.append('L').append(classArg.getName().replace('.', '/')).append(';');
45                      } else {
46                          signature.append('*');
47                      }
48                  }
49  
50                  signature.append('>');
51              }
52          }
53  
54          signature.append(';');
55          return signature.toString();
56      }
57  }