List of usage examples for java.lang Class getInterfaces
public Class<?>[] getInterfaces()
From source file:com.opensymphony.xwork2.conversion.impl.XWorkConverter.java
/** * Looks for converter mappings for the specified class, traversing up its class hierarchy and interfaces and adding * any additional mappings it may find. Mappings lower in the hierarchy have priority over those higher in the * hierarcy.//from w w w .ja v a2s. co m * * @param clazz the class to look for converter mappings for * @return the converter mappings */ protected Map<String, Object> buildConverterMapping(Class clazz) throws Exception { Map<String, Object> mapping = new HashMap<String, Object>(); // check for conversion mapping associated with super classes and any implemented interfaces Class curClazz = clazz; while (!curClazz.equals(Object.class)) { // add current class' mappings addConverterMapping(mapping, curClazz); // check interfaces' mappings Class[] interfaces = curClazz.getInterfaces(); for (Class anInterface : interfaces) { addConverterMapping(mapping, anInterface); } curClazz = curClazz.getSuperclass(); } if (mapping.size() > 0) { converterHolder.addMapping(clazz, mapping); } else { converterHolder.addNoMapping(clazz); } return mapping; }
From source file:jef.tools.reflect.ClassEx.java
/** * ?????/*from w ww.j av a 2 s.co m*/ * * @return */ public Class<?>[] getAllInterfaces() { LinkedHashSet<Class<?>> intf = new LinkedHashSet<Class<?>>(); Class<?> c = cls; while (c != Object.class) { for (Class<?> ic : c.getInterfaces()) { intf.add(ic); } c = c.getSuperclass(); } return intf.toArray(new Class<?>[intf.size()]); }
From source file:org.apache.jsp.happyaxis_jsp.java
/** * Check if class implements specified interface. * @param Class clazz//from ww w . java 2 s. com * @param String interface name * @return boolean */ private boolean implementsInterface(Class clazz, String interfaceName) { if (clazz == null) { return false; } Class[] interfaces = clazz.getInterfaces(); if (interfaces.length != 0) { for (int i = 0; i < interfaces.length; i++) { if (interfaces[i].getName().equals(interfaceName)) { return true; } } } return false; }
From source file:org.eclipse.emf.teneo.ERuntime.java
/** * Walks up the class hierarchy and adds the superclasses to the concrete-interface mapping * class sets/*from ww w . j ava2 s . c om*/ */ private void addAbstractSupers(Class<?> clazz) { // clazz is null or not an eobject if (clazz == null || !EObject.class.isAssignableFrom(clazz)) { return; } // if already been here then go on for the superclasses if (concreteToEClass.get(clazz) != null) { addAbstractSupers(clazz.getSuperclass()); return; } // new one, find all its interfaces final Class<?>[] interf = clazz.getInterfaces(); for (Class<?> element : interf) { if (EObject.class.isAssignableFrom(element)) { final EClass eclass = interfaceToEClass.get(element); concreteToEClass.put(clazz, eclass); eclassifierToConcrete.put(eclass, clazz); } } }
From source file:org.apache.camel.impl.converter.BaseTypeConverterRegistry.java
protected TypeConverter doLookup(Class<?> toType, Class<?> fromType, boolean isSuper) { if (fromType != null) { // lets try if there is a direct match TypeConverter converter = getTypeConverter(toType, fromType); if (converter != null) { return converter; }/*from ww w . ja va 2 s. c o m*/ // try the interfaces for (Class<?> type : fromType.getInterfaces()) { converter = getTypeConverter(toType, type); if (converter != null) { return converter; } } // try super then Class<?> fromSuperClass = fromType.getSuperclass(); if (fromSuperClass != null && !fromSuperClass.equals(Object.class)) { converter = doLookup(toType, fromSuperClass, true); if (converter != null) { return converter; } } } // only do these tests as fallback and only on the target type (eg not on its super) if (!isSuper) { if (fromType != null && !fromType.equals(Object.class)) { // lets try classes derived from this toType Set<Map.Entry<TypeMapping, TypeConverter>> entries = typeMappings.entrySet(); for (Map.Entry<TypeMapping, TypeConverter> entry : entries) { TypeMapping key = entry.getKey(); Class<?> aToType = key.getToType(); if (toType.isAssignableFrom(aToType)) { Class<?> aFromType = key.getFromType(); // skip Object based we do them last if (!aFromType.equals(Object.class) && aFromType.isAssignableFrom(fromType)) { return entry.getValue(); } } } // lets test for Object based converters as last resort TypeConverter converter = getTypeConverter(toType, Object.class); if (converter != null) { return converter; } } } // none found return null; }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
/** * @return the list of all supertypes: superclasses and interfaces. Class itself and its * interfaces first, then superclass. *///from ww w.j av a 2 s . c o m public static List<Class<?>> getSuperHierarchy(Class<?> clazz) throws Exception { List<Class<?>> types = Lists.newArrayList(); types.add(clazz); // check super Class Class<?> superclass = clazz.getSuperclass(); if (superclass != null) { // interfaces for (Class<?> interfaceClass : clazz.getInterfaces()) { types.addAll(getSuperHierarchy(interfaceClass)); } // super Class types.addAll(getSuperHierarchy(superclass)); } // done return types; }
From source file:com.nesscomputing.jersey.exceptions.GuiceProvisionExceptionMapper.java
@Inject @SuppressWarnings("unchecked") public GuiceProvisionExceptionMapper(final Injector injector) { for (Map.Entry<Key<?>, Binding<?>> binding : injector.getAllBindings().entrySet()) { final Key<?> key = binding.getKey(); final Class<?> clazz = key.getTypeLiteral().getRawType(); Class<? extends Throwable> c = null; if (clazz.equals(ExceptionMapper.class)) { final Type type = key.getTypeLiteral().getType(); if (type instanceof ParameterizedType) { final Type[] params = ((ParameterizedType) type).getActualTypeArguments(); Preconditions.checkState(params != null && params.length == 1, "Not a valid Exception mapper found: %s", type); c = Class.class.cast(params[0]); }/*from w w w . ja va 2s . c o m*/ } else { final Class<?>[] interfaces = clazz.getInterfaces(); if (ArrayUtils.contains(interfaces, ExceptionMapper.class)) { final Type type = key.getTypeLiteral().getType(); // Bind concrete classes, but skip the GuiceProvisionExceptionMapper (that would be a circular dep). if (type instanceof Class && !(this.getClass().equals(type))) { c = getExceptionType(Class.class.cast(type)); } } } if (c != null) { exceptionMappers.put(c, ExceptionMapper.class.cast(injector.getInstance(key))); } } }
From source file:com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.java
/** * <p>This method 'collects' all the validator configurations for a given * action invocation.</p>//from ww w. ja va2 s . c o m * <p/> * <p>It will traverse up the class hierarchy looking for validators for every super class * and directly implemented interface of the current action, as well as adding validators for * any alias of this invocation. Nifty!</p> * <p/> * <p>Given the following class structure: * <pre> * interface Thing; * interface Animal extends Thing; * interface Quadraped extends Animal; * class AnimalImpl implements Animal; * class QuadrapedImpl extends AnimalImpl implements Quadraped; * class Dog extends QuadrapedImpl; * </pre></p> * <p/> * <p>This method will look for the following config files for Dog: * <pre> * Animal * Animal-context * AnimalImpl * AnimalImpl-context * Quadraped * Quadraped-context * QuadrapedImpl * QuadrapedImpl-context * Dog * Dog-context * </pre></p> * <p/> * <p>Note that the validation rules for Thing is never looked for because no class in the * hierarchy directly implements Thing.</p> * * @param clazz the Class to look up validators for. * @param context the context to use when looking up validators. * @param checkFile true if the validation config file should be checked to see if it has been * updated. * @param checked the set of previously checked class-contexts, null if none have been checked * @return a list of validator configs for the given class and context. */ private List<ValidatorConfig> buildValidatorConfigs(Class clazz, String context, boolean checkFile, Set<String> checked) { List<ValidatorConfig> validatorConfigs = new ArrayList<ValidatorConfig>(); if (checked == null) { checked = new TreeSet<String>(); } else if (checked.contains(clazz.getName())) { return validatorConfigs; } if (clazz.isInterface()) { Class[] interfaces = clazz.getInterfaces(); for (Class anInterface : interfaces) { validatorConfigs.addAll(buildValidatorConfigs(anInterface, context, checkFile, checked)); } } else { if (!clazz.equals(Object.class)) { validatorConfigs.addAll(buildValidatorConfigs(clazz.getSuperclass(), context, checkFile, checked)); } } // look for validators for implemented interfaces Class[] interfaces = clazz.getInterfaces(); for (Class anInterface1 : interfaces) { if (checked.contains(anInterface1.getName())) { continue; } validatorConfigs.addAll(buildClassValidatorConfigs(anInterface1, checkFile)); if (context != null) { validatorConfigs.addAll(buildAliasValidatorConfigs(anInterface1, context, checkFile)); } checked.add(anInterface1.getName()); } validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile)); if (context != null) { validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile)); } checked.add(clazz.getName()); return validatorConfigs; }
From source file:com.civprod.writerstoolbox.apps.ToolSuite.java
private boolean addTool(Class<? extends Object> toolClass, Object inTool, boolean addOnly) { boolean added = false; if (toolClass != null) { if ((!addOnly) || (!toolMap.containsKey(toolClass))) { toolMap.put(toolClass, inTool); added |= toolMap.get(toolClass) == inTool; //stop recurshion when you reach Object(i.e. the root of the class tree) if (!Object.class.equals(toolClass)) { added |= addTool(toolClass.getSuperclass(), inTool, addOnly); for (Class<?> curClass : toolClass.getInterfaces()) { added |= addTool(curClass, inTool, addOnly); }/*from w w w .ja va 2 s . c o m*/ } } } return added; }
From source file:com.opensymphony.xwork2.validator.AnnotationActionValidatorManager1.java
/** * <p>//w w w . j a v a2 s . c om * This method 'collects' all the validator configurations for a given action invocation. * </p> * <p/> * <p> * It will traverse up the class hierarchy looking for validators for every super class and directly implemented interface of the current action, as well as adding validators for any alias of this invocation. Nifty! * </p> * <p/> * <p> * Given the following class structure: * * <pre> * interface Thing; * interface Animal extends Thing; * interface Quadraped extends Animal; * class AnimalImpl implements Animal; * class QuadrapedImpl extends AnimalImpl implements Quadraped; * class Dog extends QuadrapedImpl; * </pre> * * </p> * <p/> * <p> * This method will look for the following config files for Dog: * * <pre> * Animal * Animal-context * AnimalImpl * AnimalImpl-context * Quadraped * Quadraped-context * QuadrapedImpl * QuadrapedImpl-context * Dog * Dog-context * </pre> * * </p> * <p/> * <p> * Note that the validation rules for Thing is never looked for because no class in the hierarchy directly implements Thing. * </p> * * @param clazz * the Class to look up validators for. * @param context * the context to use when looking up validators. * @param checkFile * true if the validation config file should be checked to see if it has been updated. * @param checked * the set of previously checked class-contexts, null if none have been checked * @return a list of validator configs for the given class and context. */ private List<ValidatorConfig> buildValidatorConfigs(Class clazz, String context, boolean checkFile, Set<String> checked) { List<ValidatorConfig> validatorConfigs = new ArrayList<ValidatorConfig>(); if (checked == null) { checked = new TreeSet<String>(); } else if (checked.contains(clazz.getName())) { return validatorConfigs; } if (clazz.isInterface()) { Class[] interfaces = clazz.getInterfaces(); for (Class anInterface : interfaces) { validatorConfigs.addAll(buildValidatorConfigs(anInterface, context, checkFile, checked)); } } else { if (!clazz.equals(Object.class)) { validatorConfigs.addAll(buildValidatorConfigs(clazz.getSuperclass(), context, checkFile, checked)); } } // look for validators for implemented interfaces Class[] interfaces = clazz.getInterfaces(); for (Class anInterface1 : interfaces) { if (checked.contains(anInterface1.getName())) { continue; } validatorConfigs.addAll(buildClassValidatorConfigs(anInterface1, checkFile)); if (context != null) { validatorConfigs.addAll(buildAliasValidatorConfigs(anInterface1, context, checkFile)); } checked.add(anInterface1.getName()); } validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile)); if (context != null) { validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile)); validatorConfigs.addAll(buildAliasValidatorConfigsJSON(clazz, context, checkFile, fileManager, validatorFileCache, validatorFactory, LOG)); } checked.add(clazz.getName()); return validatorConfigs; }