Example usage for java.lang Class getGenericInterfaces

List of usage examples for java.lang Class getGenericInterfaces

Introduction

In this page you can find the example usage for java.lang Class getGenericInterfaces.

Prototype

public Type[] getGenericInterfaces() 

Source Link

Document

Returns the Type s representing the interfaces directly implemented by the class or interface represented by this object.

Usage

From source file:com.alfresco.orm.repository.AlfrescoRespositoryProxyFactoryBean.java

public Class getTypeClass(Class clasz) {
    Class retClass = null;//from  w w  w .  j a v a  2s  .  com
    for (Type type : clasz.getGenericInterfaces()) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            // System.out.print("Return type is " + pt.getRawType() +
            // " with the following type arguments: ");
            for (Type t : pt.getActualTypeArguments()) {
                // System.out.println(t + " ");
                retClass = (Class) t;
            }
        } else {
            // System.out.println("Return type is " + type);
        }
    }
    return retClass;
}

From source file:com.glaf.core.util.ReflectUtils.java

public static Class<?> getGenericClass(Class<?> cls, int i) {
    try {//from  ww w.jav a 2s  . co m
        ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
        Object genericClass = parameterizedType.getActualTypeArguments()[i];
        if (genericClass instanceof ParameterizedType) { // ?
            return (Class<?>) ((ParameterizedType) genericClass).getRawType();
        } else if (genericClass instanceof GenericArrayType) { // ?
            return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
        } else {
            return (Class<?>) genericClass;
        }
    } catch (Throwable e) {
        throw new IllegalArgumentException(cls.getName() + " generic type undefined!", e);
    }
}

From source file:com.mingo.convert.ConverterService.java

private <T> Set<Class<? extends Converter>> getConverters(final Class<T> aClass) {
    Set<Class<? extends Converter>> suitableConverters = new HashSet<>();
    for (Class<? extends Converter> converterClass : convertersClasses) {
        if (converterClass.getGenericInterfaces().length > 0
                && converterClass.getGenericInterfaces()[0] instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) converterClass.getGenericInterfaces()[0];
            if (pt.getActualTypeArguments().length > 0 && pt.getActualTypeArguments()[0] instanceof Class) {
                Class<?> at = (Class<?>) pt.getActualTypeArguments()[0];
                if (aClass.equals(at)) {
                    suitableConverters.add(converterClass);
                }/* w  ww.  j  a v a2  s . c o  m*/
            }
        }
    }
    return suitableConverters;
}

From source file:com.nesscomputing.jersey.exceptions.GuiceProvisionExceptionMapper.java

private Class<?> getType(Class<?> c) {
    Class<?> _c = c;
    while (_c != Object.class) {
        Type[] ts = _c.getGenericInterfaces();
        for (Type t : ts) {
            if (t instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) t;
                if (pt.getRawType() == ExceptionMapper.class) {
                    return getResolvedType(pt.getActualTypeArguments()[0], c, _c);
                }//from   ww w  .jav a2  s  .  c  o  m
            }
        }

        _c = _c.getSuperclass();
    }

    // This statement will never be reached
    return null;
}

From source file:net.nelz.simplesm.aop.CacheBase.java

protected boolean verifyTypeIsList(final Class clazz) {
    if (List.class.equals(clazz)) {
        return true;
    }//from w ww . j  av  a 2s  . c o  m
    final Type[] types = clazz.getGenericInterfaces();
    if (types != null) {
        for (final Type type : types) {
            if (type != null) {
                if (type instanceof ParameterizedType) {
                    final ParameterizedType ptype = (ParameterizedType) type;
                    if (List.class.equals(ptype.getRawType())) {
                        return true;
                    }
                } else {
                    if (List.class.equals(type)) {
                        return true;
                    }
                }
            }
        }
    }

    return false;
}

From source file:pl.bristleback.server.bristle.conf.resolver.action.ActionExtractorsResolver.java

private Class getParameterClass(Class extractorClass, Class parametrizedInterface) {
    for (int i = 0; i < extractorClass.getInterfaces().length; i++) {
        Class interfaceOfClass = extractorClass.getInterfaces()[i];
        if (parametrizedInterface.equals(interfaceOfClass)) {
            Type genericInterface = extractorClass.getGenericInterfaces()[i];
            if (genericInterface instanceof ParameterizedType) {
                return (Class) ((ParameterizedType) (genericInterface)).getActualTypeArguments()[0];
            }// www . ja  va2s  .c  o  m
            return Object.class;
        }
    }
    throw new BristleRuntimeException("Cannot find " + parametrizedInterface.getSimpleName()
            + " interface, this exception should never happen.");
}

From source file:br.gov.frameworkdemoiselle.internal.interceptor.AuditableInterceptor.java

private Class<?> getFirstInterfaceTypeParameter(Class<?> internalAuditorClass) throws Exception {
    Type genericInterface = internalAuditorClass.getGenericInterfaces()[0];
    if (genericInterface != null) {
        String gStr = genericInterface.toString();
        String strType = StringUtils.EMPTY;
        if (gStr.indexOf('<') != -1) {
            strType = gStr.substring(gStr.indexOf('<') + 1, gStr.indexOf('>'));
        }/*from   w  w w.jav a  2s . com*/
        if (StringUtils.isNotEmpty(strType)) {
            return Class.forName(strType);
        }
    }
    return null;
}

From source file:net.firejack.platform.core.utils.Factory.java

public Type getGenericInterface(Class sourceClass, Class genericInterface) {
    Type[] types = sourceClass.getGenericInterfaces();
    for (Type type : types) {
        if (type instanceof Class) {
            if (genericInterface.isAssignableFrom((Class) type)) {
                return type;
            }//from w w w. j  av  a 2 s  .c o m
        } else if (type instanceof ParameterizedType) {
            if (genericInterface.isAssignableFrom((Class) ((ParameterizedType) type).getRawType())) {
                return type;
            }
        }
    }
    return null;
}

From source file:fr.mcc.ginco.audit.tracking.GincoRevListener.java

@Override
public void entityChanged(Class entityClass, String entityName, Serializable entityId,
        RevisionType revisionType, Object revisionEntity) {
    GincoRevModifiedEntityType revEntity = new GincoRevModifiedEntityType();
    revEntity.setEntityClassName(entityClass.getName());
    revEntity.setRevision(((GincoRevEntity) revisionEntity).getId());
    ((GincoRevEntity) revisionEntity).addModifiedEntityType(revEntity);
    if (ArrayUtils.contains(entityClass.getGenericInterfaces(), IAuditableBean.class)) {
        if (!revisionType.equals(RevisionType.DEL)) {
            GenericHibernateDAO objectDAO = new GenericHibernateDAO(entityClass);
            objectDAO.setSessionFactory((SessionFactory) applicationContext.getBean("gincoSessionFactory"));
            String thesaurusId = ((IAuditableBean) objectDAO.getById(entityId)).getThesaurusId();
            ((GincoRevEntity) revisionEntity).setThesaurusId(thesaurusId);
        } else {//from   www  .j  ava 2 s  .com
            SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("gincoSessionFactory");
            AuditReader reader = AuditReaderFactory.get(sessionFactory.getCurrentSession());

            AuditQueryBuilder queryBuilder = new AuditQueryBuilder();
            AuditQuery query;
            try {
                query = queryBuilder.getEntityAddedQuery(reader, Class.forName(entityName), entityId);
                try {
                    List<Object[]> createdEvent = (List<Object[]>) query.getResultList();
                    if (createdEvent != null && createdEvent.size() > 0) {
                        ((GincoRevEntity) revisionEntity)
                                .setThesaurusId(((GincoRevEntity) createdEvent.get(0)[1]).getThesaurusId());
                    }
                } catch (AuditException ae) {
                    logger.warn("Unable to get the creation revision of the destroyed object", ae);
                }
            } catch (ClassNotFoundException e) {
                logger.error("Error storing audit data", e);
            }
        }
    } else {
        logger.warn("Trying to audit a bean not implementing IAuditableBean interface");
    }

}

From source file:nz.co.senanque.validationengine.metadata.AnnotationsMetadataFactory.java

private void scanPackageForValidators(String basePackage,
        Map<Class<? extends Annotation>, Class<? extends FieldValidator<Annotation>>> validators)
        throws ClassNotFoundException {

    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(
            true) {// w ww . j  a  v  a2 s. c om
        private String iface = FieldValidator.class.getCanonicalName();

        /**
         * Check if the class has the right annotation
         * @param metadataReader the ASM ClassReader for the class
         * @return whether the class qualifies as a candidate component
         */
        protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
            AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
            for (String n : metadata.getInterfaceNames()) {
                if (iface.equals(n)) {
                    return true;
                }
            }
            return false;
        }

        /**
         * Determine whether the given bean definition qualifies as candidate.
         * <p>The default implementation checks whether the class is concrete
         * (i.e. not abstract and not an interface). Can be overridden in subclasses.
         * @param beanDefinition the bean definition to check
         * @return whether the bean definition qualifies as a candidate component
         */
        protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
            return (beanDefinition.getMetadata().isConcrete() && beanDefinition.getMetadata().isIndependent());
        }

    };

    //      String basePackage = "nz/co/senanque/validationengine/fieldvalidators";//nz.co.senanque.validationengine.fieldvalidators
    Set<BeanDefinition> components = provider.findCandidateComponents(basePackage.replace('.', '/'));
    for (BeanDefinition component : components) {
        @SuppressWarnings("unchecked")
        Class<? extends FieldValidator<Annotation>> class_ = (Class<? extends FieldValidator<Annotation>>) Class
                .forName(component.getBeanClassName());
        Type[] types = class_.getGenericInterfaces();
        ParameterizedType t0 = (ParameterizedType) types[0];
        @SuppressWarnings("unchecked")
        Class<? extends Annotation> p = (Class<? extends Annotation>) t0.getActualTypeArguments()[0];
        validators.put(p, class_);
    }
}