Java Code Examples for org.springframework.core.GenericTypeResolver#resolveTypeArguments()

The following examples show how to use org.springframework.core.GenericTypeResolver#resolveTypeArguments() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomConversions.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private void registerConversion(Object converter) {
	Class<?> type = converter.getClass();
	boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
	boolean isReading = type.isAnnotationPresent(ReadingConverter.class);

	if (!isReading && !isWriting) {
		isReading = true;
		isWriting = true;
	}

	if (converter instanceof GenericConverter) {
		GenericConverter genericConverter = (GenericConverter) converter;
		for (ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
			register(new ConvertibleContext(pair, isReading, isWriting));
		}
	} else if (converter instanceof Converter) {
		Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
		register(new ConvertibleContext(arguments[0], arguments[1], isReading, isWriting));
	} else {
		throw new IllegalArgumentException(
				"Unsupported Converter type! Expected either GenericConverter if Converter.");
	}
}
 
Example 2
Source File: CustomConversions.java    From spring-data-crate with Apache License 2.0 6 votes vote down vote up
/**
 * Registers a conversion for the given converter. Inspects either generics or the convertible pairs returned
 * by a {@link GenericConverter}.
 *
 * @param converter the converter to register.
 */
private void registerConversion(final Object converter) {
  Class<?> type = converter.getClass();
  boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
  boolean isReading = type.isAnnotationPresent(ReadingConverter.class);

  if (converter instanceof GenericConverter) {
    GenericConverter genericConverter = (GenericConverter) converter;
    for (GenericConverter.ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
      register(new ConverterRegistration(pair, isReading, isWriting));
    }
  } else if (converter instanceof Converter) {
    Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
    register(new ConverterRegistration(arguments[0], arguments[1], isReading, isWriting));
  } else {
    throw new IllegalArgumentException("Unsupported Converter type!");
  }
}
 
Example 3
Source File: ConvertingEncoderDecoderSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Class<?>[] resolveTypeArguments() {
	Class<?>[] resolved = GenericTypeResolver.resolveTypeArguments(getClass(), ConvertingEncoderDecoderSupport.class);
	if (resolved == null) {
		throw new IllegalStateException("ConvertingEncoderDecoderSupport's generic types T and M " +
				"need to be substituted in subclass: " + getClass());
	}
	return resolved;
}
 
Example 4
Source File: ConvertingEncoderDecoderSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Class<?>[] resolveTypeArguments() {
	Class<?>[] resolved = GenericTypeResolver.resolveTypeArguments(getClass(), ConvertingEncoderDecoderSupport.class);
	if (resolved == null) {
		throw new IllegalStateException("ConvertingEncoderDecoderSupport's generic types T and M " +
				"need to be substituted in subclass: " + getClass());
	}
	return resolved;
}
 
Example 5
Source File: TProtobufProcessor.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
private Class<?> getParameterizedType(Object target) throws TException {
	Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(
			target.getClass(), Controller.class);
	if (arguments.length != 2)
		throw new TException(
				"Error to resolve request type, please make sure "
						+ target.getClass()
						+ " has provided type arguments for Controller class.");
	return arguments[0];
}
 
Example 6
Source File: ConvertingEncoderDecoderSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Class<?>[] resolveTypeArguments() {
	Class<?>[] resolved = GenericTypeResolver.resolveTypeArguments(getClass(), ConvertingEncoderDecoderSupport.class);
	if (resolved == null) {
		throw new IllegalStateException("ConvertingEncoderDecoderSupport's generic types T and M " +
				"need to be substituted in subclass: " + getClass());
	}
	return resolved;
}
 
Example 7
Source File: RepoBasedConverter.java    From spring-data-jpa-extra with Apache License 2.0 5 votes vote down vote up
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
    Class<?>[] classes = GenericTypeResolver.resolveTypeArguments(this.getClass(), RepoBasedConverter.class);
    Class<?> clazz = classes[0];
    this.repositories = new Repositories(context);
    this.entityInformation = repositories.getEntityInformationFor(clazz);
    this.genericJpaRepository = (GenericJpaRepository<S, ID>) repositories.getRepositoryFor(clazz).orElse(null);
    this.useCache = genericJpaRepository instanceof CachingJpaRepository;
}
 
Example 8
Source File: UserAgentAnnotationAnalyzer.java    From yauaa with Apache License 2.0 4 votes vote down vote up
public void initialize(UserAgentAnnotationMapper<T> theMapper) {
    mapper = theMapper;

    if (mapper == null) {
        throw new InvalidParserConfigurationException("[Initialize] The mapper instance is null.");
    }

    Class<?>[] classOfTArray = GenericTypeResolver.resolveTypeArguments(mapper.getClass(), UserAgentAnnotationMapper.class);
    if (classOfTArray == null) {
        throw new InvalidParserConfigurationException("Couldn't find the used generic type of the UserAgentAnnotationMapper.");
    }

    Class<?> classOfT = classOfTArray[0];

    // Get all methods of the correct signature that have been annotated with YauaaField
    for (final Method method : mapper.getClass().getDeclaredMethods()) {
        final YauaaField field = method.getAnnotation(YauaaField.class);
        if (field != null) {
            final Class<?> returnType = method.getReturnType();
            final Class<?>[] parameters = method.getParameterTypes();
            if (returnType.getCanonicalName().equals("void") &&
                parameters.length == 2 &&
                parameters[0] == classOfT &&
                parameters[1] == String.class) {

                if (!Modifier.isPublic(classOfT.getModifiers())) {
                    throw new InvalidParserConfigurationException("The class " + classOfT.getCanonicalName() + " is not public.");
                }

                if (!Modifier.isPublic(method.getModifiers())) {
                    throw new InvalidParserConfigurationException("Method annotated with YauaaField is not public: " +
                        method.getName());
                }

                if (method.getDeclaringClass().isAnonymousClass()) {
                    String methodName =
                        method.getReturnType().getName() + " " +
                            method.getName() + "(" +
                            parameters[0].getSimpleName()+ " ," +
                            parameters[1].getSimpleName()+ ");";
                    LOG.warn("Trying to make anonymous {} {} accessible.", method.getDeclaringClass(), methodName);
                    method.setAccessible(true);
                }

                for (String fieldName : field.value()) {
                    List<Method> methods = fieldSetters
                        .computeIfAbsent(fieldName, k -> new ArrayList<>());
                    methods.add(method);
                }
            } else {
                throw new InvalidParserConfigurationException(
                    "In class [" + method.getDeclaringClass() + "] the method [" + method.getName() + "] " +
                    "has been annotated with YauaaField but it has the wrong method signature. " +
                    "It must look like [ public void " + method.getName() + "(" + classOfT.getSimpleName() + " record, String value) ]");
            }
        }
    }

    if (fieldSetters.isEmpty()) {
        throw new InvalidParserConfigurationException("You MUST specify at least 1 field to extract.");
    }

    userAgentAnalyzer = UserAgentAnalyzer
        .newBuilder()
        .hideMatcherLoadStats()
        .withCache(cacheSize)
        .withFields(fieldSetters.keySet())
        .dropTests()
        .immediateInitialization()
        .build();
}
 
Example 9
Source File: HibernateCrudRepository.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public HibernateCrudRepository() {

    Class<?>[] classes = GenericTypeResolver.resolveTypeArguments(this.getClass(), Repository.class);
    domainClass = (classes != null && classes.length == 2) ? (Class<T>) classes[0] : null;
}
 
Example 10
Source File: HibernateCrudRepository.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public HibernateCrudRepository() {

    Class<?>[] classes = GenericTypeResolver.resolveTypeArguments(this.getClass(), Repository.class);
    domainClass = (classes != null && classes.length == 2) ? (Class<T>) classes[0] : null;
}
 
Example 11
Source File: LayerFactoryServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
public <T extends RasterLayer> void registerRasterLayerFactory(RasterLayerFactory<T> factory) {
	Class<?>[] args = GenericTypeResolver.resolveTypeArguments(factory.getClass(), LayerFactory.class);
	factories.put(args[0], factory);
}
 
Example 12
Source File: LayerFactoryServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
public <T extends VectorLayer> void registerVectorLayerFactory(VectorLayerFactory<T> factory) {
	Class<?>[] args = GenericTypeResolver.resolveTypeArguments(factory.getClass(), LayerFactory.class);
	factories.put(args[0], factory);
}