Tabnine Logo
TypeUtil
Code IndexAdd Tabnine to your IDE (free)

How to use
TypeUtil
in
com.owlike.genson.reflect

Best Java code snippets using com.owlike.genson.reflect.TypeUtil (Showing top 20 results out of 315)

origin: com.owlike/genson

@SuppressWarnings("unchecked")
protected Class<? extends BeanView<T>> findViewFor(Type type,
                          List<Class<? extends BeanView<?>>> views) {
 for (Class<? extends BeanView<?>> v : views) {
  Type searchedType = TypeUtil.lookupGenericType(BeanView.class, v);
  searchedType = TypeUtil.expandType(searchedType, v);
  searchedType = TypeUtil.typeOf(0, searchedType);
  if (TypeUtil.match(type, searchedType, false)) {
   return (Class<? extends BeanView<T>>) v;
  }
 }
 return null;
}
origin: com.owlike/genson

public PropertyMutator createMutator(String name, Method method, Type ofType, Genson genson) {
 // the target bean must be second parameter for beanview mutators
 BeanView<?> beanview = views.get(getRawClass(ofType));
 if (beanview != null) {
  Type superTypeWithParameter =
   TypeUtil.lookupGenericType(BeanView.class, beanview.getClass());
  Class<?> tClass =
   getRawClass(typeOf(0,
    expandType(superTypeWithParameter, beanview.getClass())));
  Type type = expandType(method.getGenericParameterTypes()[0], ofType);
  return new BeanViewPropertyMutator(name, method, type, beanview, tClass);
 } else return null;
}
origin: com.owlike/genson

public final static Class<?> getRawClass(Type type) {
 if (type instanceof Class<?>)
  return (Class<?>) type;
 else if (type instanceof ParameterizedType) {
  ParameterizedType pType = (ParameterizedType) type;
  return (Class<?>) pType.getRawType();
 } else if (type instanceof GenericArrayType) {
  Type componentType = ((GenericArrayType) type).getGenericComponentType();
  return Array.newInstance(getRawClass(componentType), 0).getClass();
 } else
  return getRawClass(expand(type, null));
}
origin: com.owlike/genson

/**
 * Searches for the typevariable definition in the inClass hierarchy.
 *
 * @param type
 * @param inClass
 * @return the resolved type or type if unable to resolve it.
 */
public final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> inClass) {
 return resolveTypeVariable(type, genericDeclarationToClass(type.getGenericDeclaration()), inClass);
}
origin: com.owlike/genson

private void addDefaultSerializers(List<? extends Serializer<?>> serializers) {
 if (serializers != null) {
  for (Serializer<?> serializer : serializers) {
   Type typeOfConverter = TypeUtil.typeOf(0,
    TypeUtil.lookupGenericType(Serializer.class, serializer.getClass()));
   typeOfConverter = TypeUtil.expandType(typeOfConverter, serializer.getClass());
   if (!serializersMap.containsKey(typeOfConverter))
    serializersMap.put(typeOfConverter, serializer);
  }
 }
}
origin: owlike/genson

public PropertyMutator createMutator(String name, Field field, Type ofType, Genson genson) {
 Class<?> ofClass = getRawClass(ofType);
 Type expandedType = TypeUtil.expandType(field.getGenericType(), ofType);
 return new PropertyMutator.FieldMutator(name, field, expandedType, ofClass);
}
origin: com.owlike/genson

@SuppressWarnings("unchecked")
private GenericType(Type type) {
 this.type = type;
 this.rawClass = (Class<T>) TypeUtil.getRawClass(type);
}
origin: owlike/genson

} else {
 try {
  circularTypes.put(type, getRawClass(type));
  TypeAndRootClassKey key = new TypeAndRootClassKey(type, rootType);
  Type expandedType = _cache.get(key);
    Type[] expandedArgs = new Type[len];
    for (int i = 0; i < len; i++) {
     expandedArgs[i] = expandType(args[i], rootType);
    expandedType = new ExpandedParameterizedType(pType, getRawClass(rootType), expandedArgs);
   } else if (type instanceof TypeVariable) {
    @SuppressWarnings("unchecked")
     String typeName = tvType.getName();
     int idx = 0;
     for (TypeVariable<?> parameter : genericDeclarationToClass(tvType.getGenericDeclaration())
      .getTypeParameters()) {
      if (typeName.equals(parameter.getName())) {
     expandedType = resolveTypeVariable(tvType, getRawClass(rootType));
     expandedType = expandType(tvType.getBounds()[0], rootType);
   } else if (type instanceof GenericArrayType) {
    GenericArrayType genArrType = (GenericArrayType) type;
    Type cType = expandType(genArrType.getGenericComponentType(), rootType);
    if (genArrType.getGenericComponentType() == cType)
     cType = Object.class;
    expandedType = new ExpandedGenericArrayType(genArrType, cType, getRawClass(rootType));
origin: com.owlike/genson

 @SuppressWarnings({"rawtypes", "unchecked"})
 public Converter<Object> create(Type forType, Genson genson) {
  if (forType instanceof GenericArrayType
   || (forType instanceof Class<?> && ((Class<?>) forType).isArray())) {
   if (byte.class.equals(getCollectionType(forType))) {
    return (Converter) ByteArrayConverter.instance;
   } else {
    Converter<?> elementConverter = genson.provideConverter(TypeUtil
     .getCollectionType(forType));
    return new ArrayConverter(TypeUtil.getRawClass(TypeUtil
     .getCollectionType(forType)), elementConverter);
   }
  }
  return null;
 }
}
origin: com.owlike/genson

/**
 * Deep comparison between type and oType. If parameter strictMatch is true, then type and oType will be strictly
 * compared otherwise this method checks whether oType is assignable from type.
 */
public final static boolean match(Type type, Type oType, boolean strictMatch) {
 if (type == null || oType == null)
  return type == null && oType == null;
 Class<?> clazz = getRawClass(type);
 Class<?> oClazz = getRawClass(oType);
 boolean match = strictMatch ? oClazz.equals(clazz) : oClazz.isAssignableFrom(clazz);
 if (Object.class.equals(oClazz) && !strictMatch)
  return match;
 if (clazz.isArray() && !oClazz.isArray())
  return match;
 Type[] types = getTypes(type);
 Type[] oTypes = getTypes(oType);
 match = match && (types.length == oTypes.length || types.length == 0);
 for (int i = 0; i < types.length && match; i++)
  match = match(types[i], oTypes[i], strictMatch);
 return match;
}
origin: com.owlike/genson

/**
 * Returns the type of this Collection or Array.
 *
 * @throws IllegalArgumentException if type is not a Collection, not a generic array and not a primitive array.
 */
public final static Type getCollectionType(Type type) {
 if (type instanceof GenericArrayType) {
  return ((GenericArrayType) type).getGenericComponentType();
 } else if (type instanceof Class<?>) {
  Class<?> clazz = (Class<?>) type;
  if (clazz.isArray())
   return clazz.getComponentType();
  else if (Collection.class.isAssignableFrom(clazz)) {
   return Object.class;
  }
 } else if (type instanceof ParameterizedType && Collection.class.isAssignableFrom(getRawClass(type))) {
  return typeOf(0, type);
 }
 throw new IllegalArgumentException(
  "Could not extract parametrized type, are you sure it is a Collection or an Array?");
}
origin: com.owlike/genson

/**
 * Resolves all public methods starting with get/is (boolean) and parameter less as
 * accessors.
 */
public Trilean isAccessor(Method method, Class<?> fromClass) {
 if (!method.isBridge()) {
  String name = method.getName();
  int len = name.length();
  if (methodVisibilityFilter.isVisible(method)
   && ((len > 3 && name.startsWith("get")) || (len > 2 && name.startsWith("is") && (TypeUtil
   .match(TypeUtil.expandType(method.getGenericReturnType(), fromClass),
    Boolean.class, false) || TypeUtil.match(
   method.getGenericReturnType(), boolean.class, false))))
   && method.getParameterTypes().length == 0)
   return TRUE;
 }
 return FALSE;
}
origin: owlike/genson

final static Type expand(Type type, Class<?> inClass) {
 Type expandedType = null;
 if (type instanceof TypeVariable) {
  @SuppressWarnings("unchecked")
  // for the moment we assume it is a class, we can later handle ctr and methods
   TypeVariable<GenericDeclaration> tvType = (TypeVariable<GenericDeclaration>) type;
  if (inClass == null)
   inClass = genericDeclarationToClass(tvType.getGenericDeclaration());
  expandedType = resolveTypeVariable(tvType, inClass);
  if (type.equals(expandedType))
   expandedType = tvType.getBounds()[0];
 } else if (type instanceof WildcardType) {
  WildcardType wType = (WildcardType) type;
  expandedType = wType.getUpperBounds().length > 0 ? expand(wType.getUpperBounds()[0], inClass)
   : Object.class;
 } else
  return type;
 return expandedType == null || type.equals(expandedType) ? Object.class : expandedType;
}
origin: com.owlike/genson

 public Type[] expandTypes(Type[] typesToExpand, Type inContext) {
  Type[] expandedTypes = new Type[typesToExpand.length];
  for (int i = 0; i < typesToExpand.length; i++) {
   expandedTypes[i] = TypeUtil.expandType(typesToExpand[i], inContext);
  }
  return expandedTypes;
 }
}
origin: com.owlike/genson

 @Override
 public Converter<Optional<Object>> create(Type type, Genson genson) {
  Type typeOfValue = TypeUtil.typeOf(0, type);
  return new OptionalConverter<Object>(genson.provideConverter(typeOfValue));
 }
}
origin: owlike/genson

   return inClass.getGenericInterfaces()[i];
  } else {
   Type superType = lookupGenericType(ofClass, interfaces[i]);
   if (superType != null)
    return superType;
if (ofClass.equals(superClass))
 return inClass.getGenericSuperclass();
return lookupGenericType(ofClass, inClass.getSuperclass());
origin: com.owlike/genson

 public Converter<Object> create(Type type, Genson genson) {
  if (TypeUtil.match(type, Object.class, true)) {
   return UntypedConverter.instance;
  }
  return null;
 }
}
origin: com.owlike/genson

for (int i = 0; i < interfaces.length && resolvedType == null; i++) {
 superClass = interfaces[i];
 resolvedType = resolveTypeVariable(type, declaringClass, superClass);
 genericSuperClass = inClass.getGenericInterfaces()[i];
resolvedType = resolveTypeVariable(type, declaringClass, superClass);
genericSuperClass = inClass.getGenericSuperclass();
origin: com.owlike/genson

private void addDefaultDeserializers(List<? extends Deserializer<?>> deserializers) {
 if (deserializers != null) {
  for (Deserializer<?> deserializer : deserializers) {
   Type typeOfConverter = TypeUtil.typeOf(0, TypeUtil.lookupGenericType(Deserializer.class, deserializer.getClass()));
   typeOfConverter = TypeUtil.expandType(typeOfConverter, deserializer.getClass());
   if (!deserializersMap.containsKey(typeOfConverter))
    deserializersMap.put(typeOfConverter, deserializer);
  }
 }
}
origin: com.owlike/genson

public PropertyMutator createMutator(String name, Field field, Type ofType, Genson genson) {
 Class<?> ofClass = getRawClass(ofType);
 Type expandedType = TypeUtil.expandType(field.getGenericType(), ofType);
 return new PropertyMutator.FieldMutator(name, field, expandedType, ofClass);
}
com.owlike.genson.reflectTypeUtil

Javadoc

This class provides utilities to work with java Types. Its main goal is to provide tools for working with generic types.

Most used methods

  • expandType
    Expands type in the type rootType to Class, ParameterizedType or GenericArrayType. Useful for generi
  • getRawClass
  • lookupGenericType
    Searches for ofClass in the inherited classes and interfaces of inClass. If ofClass has been found i
  • typeOf
    Convenient method that returns the type of the parameter at position parameterIdx in the type fromTy
  • expand
  • genericDeclarationToClass
  • getCollectionType
    Returns the type of this Collection or Array.
  • getTypes
  • match
    Deep comparison between type and oType. If parameter strictMatch is true, then type and oType will b
  • resolveTypeVariable
  • wrap
  • wrap

Popular in Java

  • Making http post requests using okhttp
  • requestLocationUpdates (LocationManager)
  • compareTo (BigDecimal)
  • putExtra (Intent)
  • BorderLayout (java.awt)
    A border layout lays out a container, arranging and resizing its components to fit in five regions:
  • Menu (java.awt)
  • InputStreamReader (java.io)
    A class for turning a byte stream into a character stream. Data read from the source input stream is
  • GregorianCalendar (java.util)
    GregorianCalendar is a concrete subclass of Calendarand provides the standard calendar used by most
  • Executor (java.util.concurrent)
    An object that executes submitted Runnable tasks. This interface provides a way of decoupling task s
  • SAXParseException (org.xml.sax)
    Encapsulate an XML parse error or warning.> This module, both source code and documentation, is in t
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now