Java Class.getGenericInterfaces Examples

Java Class.getGenericInterfaces - 30 examples found. These are the top rated real world Java examples of Class.getGenericInterfaces extracted from open source projects. You can rate examples to help us improve the quality of examples.
 @Override
 public <T> void extract(ModelSchemaExtractionContext<T> extractionContext) {
   Type type = extractionContext.getType().getType();
   if (!(type instanceof Class)) {
     return;
   }
   Class<?> contractType = (Class<?>) type;
   if (!contractType.isInterface()) {
     return;
   }
   if (contractType.getGenericInterfaces().length != 1) {
     return;
   }
   Type superType = contractType.getGenericInterfaces()[0];
   if (!(superType instanceof ParameterizedType)) {
     return;
   }
   ParameterizedType parameterizedSuperType = (ParameterizedType) superType;
   if (!parameterizedSuperType.getRawType().equals(ModelMap.class)) {
     return;
   }
   ModelType<?> elementType = ModelType.of(parameterizedSuperType.getActualTypeArguments()[0]);
   Class<?> proxyImpl = generator.generate(ModelMapGroovyDecorator.Managed.class, contractType);
   extractionContext.found(
       new SpecializedMapSchema<T>(extractionContext.getType(), elementType, proxyImpl));
 }
Example #2
0
  /**
   * Returns the generic supertype for {@code supertype}. For example, given a class {@code
   * IntegerSet}, the result for when supertype is {@code Set.class} is {@code Set<Integer>} and the
   * result when the supertype is {@code Collection.class} is {@code Collection<Integer>}.
   */
  static Type getGenericSupertype(Type context, Class<?> rawType, Class<?> toResolve) {
    if (toResolve == rawType) {
      return context;
    }

    // we skip searching through interfaces if unknown is an interface
    if (toResolve.isInterface()) {
      Class<?>[] interfaces = rawType.getInterfaces();
      for (int i = 0, length = interfaces.length; i < length; i++) {
        if (interfaces[i] == toResolve) {
          return rawType.getGenericInterfaces()[i];
        } else if (toResolve.isAssignableFrom(interfaces[i])) {
          return getGenericSupertype(rawType.getGenericInterfaces()[i], interfaces[i], toResolve);
        }
      }
    }

    // check our supertypes
    if (!rawType.isInterface()) {
      while (rawType != Object.class) {
        Class<?> rawSupertype = rawType.getSuperclass();
        if (rawSupertype == toResolve) {
          return rawType.getGenericSuperclass();
        } else if (toResolve.isAssignableFrom(rawSupertype)) {
          return getGenericSupertype(rawType.getGenericSuperclass(), rawSupertype, toResolve);
        }
        rawType = rawSupertype;
      }
    }

    // we can't resolve this further
    return toResolve;
  }
 private static Type getClosestParentType(Class paramClass1, Class paramClass2) {
   Object localObject1;
   int j;
   Object localObject2;
   Class localClass;
   if (paramClass2.isInterface()) {
     Type[] arrayOfType = paramClass1.getGenericInterfaces();
     localObject1 = null;
     int i = arrayOfType.length;
     j = 0;
     if (j < i) {
       localObject2 = arrayOfType[j];
       if ((localObject2 instanceof ParameterizedType)) {
         localClass = getRawType((ParameterizedType) localObject2);
         label52:
         if ((!isAssignable(localClass, paramClass2))
             || (!isAssignable((Type) localObject1, localClass))) break label133;
       }
     }
   }
   while (true) {
     j++;
     localObject1 = localObject2;
     break;
     if ((localObject2 instanceof Class)) {
       localClass = (Class) localObject2;
       break label52;
     }
     throw new IllegalStateException("Unexpected generic interface type found: " + localObject2);
     if (localObject1 != null) return localObject1;
     return paramClass1.getGenericSuperclass();
     label133:
     localObject2 = localObject1;
   }
 }
Example #4
0
 private void makeInterfaceTypes(CompileUnit cu, ClassNode classNode, Class clazz) {
   Type[] interfaceTypes = clazz.getGenericInterfaces();
   if (interfaceTypes.length == 0) {
     classNode.setInterfaces(ClassNode.EMPTY_ARRAY);
   } else {
     ClassNode[] ret = new ClassNode[interfaceTypes.length];
     for (int i = 0; i < interfaceTypes.length; i++) {
       Type type = interfaceTypes[i];
       while (!(type instanceof Class)) {
         ParameterizedType pt = (ParameterizedType) type;
         Type t2 = pt.getRawType();
         if (t2 == type) {
           throw new GroovyBugError(
               "Cannot transform generic signature of "
                   + clazz
                   + " with generic interface "
                   + interfaceTypes[i]
                   + " to a class.");
         }
         type = t2;
       }
       ret[i] = makeClassNode(cu, interfaceTypes[i], (Class) type);
     }
     classNode.setInterfaces(ret);
   }
 }
Example #5
0
  /**
   * Determines the key type for a Map.
   *
   * @param type The parametrized type.
   * @param path The path to the type, used in exception message.
   * @return The key type.
   */
  public static Type[] mapTypes(Type type, String path) {
    if (type instanceof Class) {
      Class c = (Class) type;
      type = c.getGenericSuperclass();
      if (!(isGenericMap(type))) {
        Type[] types = c.getGenericInterfaces();
        if (types != null && types.length > 0) {
          for (Type t : types) {
            if (isGenericMap(t)) {
              type = t;
              break;
            }
          }
        }
      }
    }

    if (type instanceof ParameterizedType) {
      ParameterizedType parameterizedType = (ParameterizedType) type;
      Class<?> rawType = (Class<?>) parameterizedType.getRawType();
      if (Map.class.isAssignableFrom(rawType)) {
        return parameterizedType.getActualTypeArguments();
      }
    }

    throw new CollectionExpressionException(
        "The method or member ["
            + path
            + "] returns a simple Map. Unable to determine the "
            + "types of the Map. Please make this method or member generic so that the correct type can be determined.");
  }
Example #6
0
 private Class<?> getEventType(EventSubscriber<?> subscriber) {
   Class<?> eventType = null;
   Class<?> clazz = subscriber.getClass();
   while (clazz != null && eventType == null) {
     for (Type type : clazz.getGenericInterfaces()) {
       if (type instanceof ParameterizedType) {
         final ParameterizedType parameterizedType = (ParameterizedType) type;
         final Type rawType = parameterizedType.getRawType();
         if (EventSubscriber.class == rawType) {
           final Type[] typeArguments = parameterizedType.getActualTypeArguments();
           if (typeArguments.length == 1) {
             if (typeArguments[0] instanceof Class) {
               eventType = (Class) typeArguments[0];
             }
           }
         }
       }
     }
     clazz = clazz.getSuperclass();
   }
   if (eventType == null) {
     throw new IllegalArgumentException(
         String.format("Unable determine type of events processed by %s", subscriber));
   }
   return eventType;
 }