Decides what owner type to use for constructing ParameterizedType from a raw class.
| 109 | /** Decides what owner type to use for constructing {@link ParameterizedType} from a raw class. */ |
| 110 | |
| 111 | private enum ClassOwnership { |
| 112 | OWNED_BY_ENCLOSING_CLASS { |
| 113 | @Nullable |
| 114 | @Override |
| 115 | Class<?> getOwnerType(Class<?> rawType) { |
| 116 | return rawType.getEnclosingClass(); |
| 117 | } |
| 118 | }, |
| 119 | |
| 120 | LOCAL_CLASS_HAS_NO_OWNER { |
| 121 | @Nullable |
| 122 | @Override |
| 123 | Class<?> getOwnerType(Class<?> rawType) { |
| 124 | if (rawType.isLocalClass()) { |
| 125 | return null; |
| 126 | } else { |
| 127 | return rawType.getEnclosingClass(); |
| 128 | } |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | @Nullable |
| 133 | abstract Class<?> getOwnerType(Class<?> rawType); |
| 134 | static final ClassOwnership JVM_BEHAVIOR = detectJvmBehavior(); |
| 135 | |
| 136 | |
| 137 | private static ClassOwnership detectJvmBehavior() { |
| 138 | class LocalClass<T> {} |
| 139 | Class<?> subclass = new LocalClass<String>() {}.getClass(); |
| 140 | ParameterizedType parameterizedType = (ParameterizedType) subclass.getGenericSuperclass(); |
| 141 | for (ClassOwnership behavior : ClassOwnership.values()) { |
| 142 | if (behavior.getOwnerType(LocalClass.class) == parameterizedType.getOwnerType()) { |
| 143 | return behavior; |
| 144 | } |
| 145 | } |
| 146 | throw new AssertionError(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns a new {@link TypeVariable} that belongs to {@code declaration} with {@code name} and |
nothing calls this directly
no test coverage detected