List of usage examples for java.lang Class isInterface
@HotSpotIntrinsicCandidate public native boolean isInterface();
From source file:ipc.RPC.java
private static synchronized RpcEngine getProtocolEngine(Class protocol, Configuration conf) { RpcEngine engine = PROTOCOL_ENGINES.get(protocol); if (engine == null) { Class<?> impl = conf.getClass(ENGINE_PROP + "." + protocol.getName(), WritableRpcEngine.class); engine = (RpcEngine) ReflectionUtils.newInstance(impl, conf); if (protocol.isInterface()) PROXY_ENGINES.put(Proxy.getProxyClass(protocol.getClassLoader(), protocol), engine); PROTOCOL_ENGINES.put(protocol, engine); }//from ww w . j a va 2 s . co m return engine; }
From source file:edu.usu.sdl.openstorefront.doc.EntityProcessor.java
private static EntityDocModel createEntityModel(Class entity) { EntityDocModel docModel = new EntityDocModel(); docModel.setName(entity.getSimpleName()); APIDescription description = (APIDescription) entity.getAnnotation(APIDescription.class); if (description != null) { docModel.setDescription(description.value()); }/*from w w w. java2 s .c om*/ if (!entity.isInterface()) { addFields(entity, docModel); } return docModel; }
From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java
private static boolean assignPropertyValue(PropertyDescriptor sourceProp, Object sourceValue, PropertyDescriptor destProp, Object destInstance) { if (sourceValue == null) { System.out.println("Null value found, assignment skipped"); return true; }/*from w w w . j a v a2 s .c o m*/ boolean result = false; Class<?> sourceType = sourceProp.getPropertyType(); Method getter = sourceProp.getReadMethod(); Class<?> destType = destProp.getPropertyType(); Method setter = destProp.getWriteMethod(); try { if (destType.isInterface() || destType.isArray() || destType.isEnum()) { if (Collection.class.isAssignableFrom(sourceType) && Collection.class.isAssignableFrom(destType)) assignCollectionValue(getter, sourceValue, destType, setter, destInstance); else if (Map.class.isAssignableFrom(sourceType) && Map.class.isAssignableFrom(destType)) assignMapValue(getter, sourceValue, setter, destInstance); else assignMixedTypesValue(sourceType, getter, sourceValue, destType, setter, destInstance); } else if (destType.isInstance(sourceValue) || destType.isPrimitive()) setter.invoke(destInstance, sourceValue); else if (destType.isMemberClass()) setter.invoke(destInstance, sourceValue); else if (destType.isAssignableFrom(sourceType)) setter.invoke(destInstance, destType.cast(sourceValue)); else { //if (ClassUtils.isInnerClass(destType)) { Object member = transposeModel(sourceType, destType, sourceValue); member = destType.cast(member); setter.invoke(destInstance, member); } result = true; } catch (IllegalArgumentException ex) { System.out.println("Looks like type mismatch, source type: " + sourceType + ", dest type: " + destType); Logger.getLogger(ServiceModelTranslator.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException | IllegalAccessException ex) { Logger.getLogger(ServiceModelTranslator.class.getName()).log(Level.SEVERE, null, ex); } return result; }
From source file:com.weibo.api.motan.core.extension.ExtensionLoader.java
/** * check clz//from w w w . ja v a2 s . c om * * <pre> * 1. is interface * 2. is contains @Spi annotation * </pre> * * * @param <T> * @param clz */ private static <T> void checkInterfaceType(Class<T> clz) { if (clz == null) { failThrows(clz, "Error extension type is null"); } if (!clz.isInterface()) { failThrows(clz, "Error extension type is not interface"); } if (!isSpiType(clz)) { failThrows(clz, "Error extension type without @Spi annotation"); } }
From source file:org.red5.server.util.ScopeUtils.java
public static Object getScopeService(IScope scope, Class<?> intf, Class<?> defaultClass, boolean checkHandler) { if (scope == null || intf == null) { return null; }//from w ww. j a v a 2s.c o m // We expect an interface assert intf.isInterface(); String attr = IPersistable.TRANSIENT_PREFIX + SERVICE_CACHE_PREFIX + intf.getCanonicalName(); if (scope.hasAttribute(attr)) { // return cached service return scope.getAttribute(attr); } Object handler = null; if (checkHandler) { IScope current = scope; while (current != null) { IScopeHandler scopeHandler = current.getHandler(); if (intf.isInstance(scopeHandler)) { handler = scopeHandler; break; } if (!current.hasParent()) { break; } current = current.getParent(); } } if (handler == null && IScopeService.class.isAssignableFrom(intf)) { // we've got an IScopeService, try to lookup bean Field key = null; Object serviceName = null; try { key = intf.getField("BEAN_NAME"); serviceName = key.get(null); if (serviceName instanceof String) { handler = getScopeService(scope, (String) serviceName, defaultClass); } } catch (Exception e) { log.debug("No string field 'BEAN_NAME' in that interface"); } } if (handler == null && defaultClass != null) { try { handler = defaultClass.newInstance(); } catch (Exception e) { log.error("", e); } } // cache service scope.setAttribute(attr, handler); return handler; }
From source file:org.jdbcluster.JDBClusterUtil.java
/** * @param superClass//from w ww. j a v a 2s. c om * Class or Interface to look for * @param clazz * the class instance * @return -1 for not found. Counter for supercasses above parameter * superClass */ static private int countSuper(Class<?> superClass, Class<?> clazz) { if (superClass.isInterface()) return countSuperInterface(0, superClass, clazz.getInterfaces()); else return countSuperClass(0, superClass, clazz); }
From source file:antre.TypeResolver.java
/** * Resolves the generic Type for the {@code targetType} by walking the type hierarchy upwards from * the {@code initialType}./*from w w w.java 2s .c o m*/ */ public static Type resolveGenericType(Type initialType, Class<?> targetType) { Class<?> rawType; if (initialType instanceof ParameterizedType) rawType = (Class<?>) ((ParameterizedType) initialType).getRawType(); else rawType = (Class<?>) initialType; if (targetType.equals(rawType)) return initialType; Type result; if (targetType.isInterface()) { for (Type superInterface : rawType.getGenericInterfaces()) if (superInterface != null && !superInterface.equals(Object.class)) if ((result = resolveGenericType(superInterface, targetType)) != null) return result; } Type superType = rawType.getGenericSuperclass(); if (superType != null && !superType.equals(Object.class)) if ((result = resolveGenericType(superType, targetType)) != null) return result; return null; }
From source file:org.apache.crunch.impl.mem.collect.MemCollection.java
/** * The method creates a {@link TaskInputOutputContext} that will just provide * {@linkplain Configuration}. The method has been implemented with javaassist * as there are API changes in versions of Hadoop. In hadoop 1.0.3 the * {@linkplain TaskInputOutputContext} is abstract class while in version 2 * the same is an interface./*from www. jav a 2s . c o m*/ * <p> * Note: The intention of this is to provide the bare essentials that are * required to make the {@linkplain MemPipeline} work. It lacks even the basic * things that can proved some support for unit testing pipeline. */ private static TaskInputOutputContext<?, ?, ?, ?> getInMemoryContext(final Configuration conf) { ProxyFactory factory = new ProxyFactory(); Class<TaskInputOutputContext> superType = TaskInputOutputContext.class; Class[] types = new Class[0]; Object[] args = new Object[0]; final TaskAttemptID taskAttemptId = new TaskAttemptID(); if (superType.isInterface()) { factory.setInterfaces(new Class[] { superType }); } else { types = new Class[] { Configuration.class, TaskAttemptID.class, RecordWriter.class, OutputCommitter.class, StatusReporter.class }; args = new Object[] { conf, taskAttemptId, null, null, null }; factory.setSuperclass(superType); } final Set<String> handledMethods = ImmutableSet.of("getConfiguration", "getCounter", "progress", "getNumReduceTasks", "getTaskAttemptID"); factory.setFilter(new MethodFilter() { @Override public boolean isHandled(Method m) { return handledMethods.contains(m.getName()); } }); MethodHandler handler = new MethodHandler() { @Override public Object invoke(Object arg0, Method m, Method arg2, Object[] args) throws Throwable { String name = m.getName(); if ("getConfiguration".equals(name)) { return conf; } else if ("progress".equals(name)) { // no-op return null; } else if ("getTaskAttemptID".equals(name)) { return taskAttemptId; } else if ("getNumReduceTasks".equals(name)) { return 1; } else if ("getCounter".equals(name)) { // getCounter if (args.length == 1) { return MemPipeline.getCounters().findCounter((Enum<?>) args[0]); } else { return MemPipeline.getCounters().findCounter((String) args[0], (String) args[1]); } } else { throw new IllegalStateException("Unhandled method " + name); } } }; try { Object newInstance = factory.create(types, args, handler); return (TaskInputOutputContext<?, ?, ?, ?>) newInstance; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
From source file:com.github.dozermapper.core.util.MappingUtils.java
@SuppressWarnings("unchecked") private static Collection<?> prepareIndexedCollectionType(Class<?> collectionType, Object existingCollection, Object collectionEntry, int index) { Collection result = null;//from w w w .j ava2s . co m //Instantiation of the new Collection: can be interface or implementation class if (collectionType.isInterface()) { if (collectionType.equals(Set.class)) { result = new HashSet(); } else if (collectionType.equals(List.class)) { result = new ArrayList(); } else { throwMappingException( "Only interface types java.util.Set and java.util.List are supported for java.util.Collection type indexed properties."); } } else { //It is an implementation class of Collection try { result = (Collection) collectionType.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } //Fill in old values in new Collection if (existingCollection != null) { result.addAll((Collection) existingCollection); } //Add the new value: //For an ordered Collection if (result instanceof List) { while (result.size() < index + 1) { result.add(null); } ((List) result).set(index, collectionEntry); } else { //for an unordered Collection (index has no use here) result.add(collectionEntry); } return result; }
From source file:org.dozer.util.MappingUtils.java
@SuppressWarnings("unchecked") private static Collection<?> prepareIndexedCollectionType(Class<?> collectionType, Object existingCollection, Object collectionEntry, int index) { Collection result = null;// w w w . j a v a2s . co m //Instantiation of the new Collection: can be interface or implementation class if (collectionType.isInterface()) { if (collectionType.equals(Set.class)) { result = new HashSet(); } else if (collectionType.equals(List.class)) { result = new ArrayList(); } else { throwMappingException( "Only interface types java.util.Set and java.util.List are supported for java.util.Collection type indexed properties."); } } else { //It is an implementation class of Collection try { result = (Collection) collectionType.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } //Fill in old values in new Collection if (existingCollection != null) { result.addAll((Collection) existingCollection); } //Add the new value: //For an ordered Collection if (result instanceof List) { while (result.size() < index + 1) { result.add(null); } ((List) result).set(index, collectionEntry); } //for an unordered Collection (index has no use here) else { result.add(collectionEntry); } return result; }