List of usage examples for java.lang Class isInterface
@HotSpotIntrinsicCandidate public native boolean isInterface();
From source file:com.alta189.bukkit.script.event.EventScanner.java
public static void scanPlugin(Plugin plugin) { if (pluginEvents.containsKey(plugin)) { return;/* w ww . j a v a 2s . co m*/ } BScript.getInstance().debug("Scanning plugin " + plugin.getName()); if (plugin instanceof JavaPlugin) { ClassLoader loader = ReflectionUtil.getFieldValue(JavaPlugin.class, plugin, "classLoader"); Reflections reflections = new Reflections(new ConfigurationBuilder() .filterInputsBy(new FilterBuilder().exclude(FilterBuilder.prefix("org.bukkit"))) .setUrls(ClasspathHelper.forClassLoader(loader))); Set<Class<? extends Event>> classes = reflections.getSubTypesOf(Event.class); BScript.getInstance().info("Found " + classes.size() + " classes extending " + Event.class.getCanonicalName() + " in " + plugin.getName()); for (Class<? extends Event> clazz : classes) { if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { continue; } BScript.getInstance().debug(clazz.getCanonicalName()); String className = clazz.getCanonicalName(); if (className == null) { className = clazz.getName(); } BScript.getInstance().debug(className); events.put(className, clazz); String simpleName = clazz.getSimpleName(); if (simpleNameEvents.get(simpleName) != null) { simpleNameEvents.remove(simpleName); } else { simpleNameEvents.put(simpleName, clazz); } } if (classes.size() > 0) { pluginEvents.put(plugin, classes); } } else { BScript.getInstance().debug("Plugin is not JavaPlugin " + plugin.getName()); } }
From source file:com.comcast.cereal.convert.MapCerealizer.java
private static boolean isInstantiable(Class<?> clazz) { boolean abs = Modifier.isAbstract(clazz.getModifiers()); boolean hasConstuctor = ConstructorUtils.getAccessibleConstructor(clazz, new Class[0]) != null; return !clazz.isInterface() && !abs && hasConstuctor; }
From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java
private static int findCIDistance(Class<?> lower, Class<?> upper) { if (!upper.isInterface() || lower.isInterface()) { throw new IllegalArgumentException( String.format("Invalid input class : upper=%s lower=%s", upper, lower)); }/*ww w . jav a 2 s . co m*/ if (lower == upper) { return 0; } if (!upper.isAssignableFrom(lower)) { return -1; } int distance = -1; int offset = 0; do { int tmp = findCIOneLevelDistance(lower, upper); if (tmp >= 0) { tmp += offset + 1; distance = distance < 0 ? tmp : Math.min(distance, tmp); } if (distance == 0) { break; } lower = lower.getSuperclass(); offset++; } while (lower != null); return distance; }
From source file:com.hurence.logisland.classloading.PluginProxy.java
/** * Return all interfaces that the given class implements as a Set, * including ones implemented by superclasses. * <p>If the class itself is an interface, it gets returned as sole interface. * * @param clazz the class to analyze for interfaces * @param classLoader the ClassLoader that the interfaces need to be visible in * (may be {@code null} when accepting all declared interfaces) * @return all interfaces that the given object implements as a Set */// w w w. j av a 2s. c om public static Set<Class<?>> getAllInterfacesForClassAsSet(Class<?> clazz, ClassLoader classLoader) { if (clazz.isInterface() && isVisible(clazz, classLoader)) { return Collections.<Class<?>>singleton(clazz); } Set<Class<?>> interfaces = new LinkedHashSet<Class<?>>(); Class<?> current = clazz; while (current != null) { Class<?>[] ifcs = current.getInterfaces(); for (Class<?> ifc : ifcs) { interfaces.addAll(getAllInterfacesForClassAsSet(ifc, classLoader)); } current = current.getSuperclass(); } return interfaces; }
From source file:com.hurence.logisland.classloading.PluginProxy.java
private static Object createProxy(Object object, Class superClass, Class[] interfaces, ClassLoader cl) { CglibProxyHandler handler = new CglibProxyHandler(object); Enhancer enhancer = new Enhancer(); if (superClass != null && !AbstractConfigurableComponent.class.isAssignableFrom(superClass)) { enhancer.setSuperclass(superClass); }/* w w w . ja v a 2 s.c o m*/ enhancer.setCallback(handler); Set<Class<?>> il = new LinkedHashSet<>(); il.add(SerializationMagik.class); il.add(DelegateAware.class); if (interfaces != null) { for (Class<?> i : interfaces) { if (i.isInterface()) { il.add(i); } } enhancer.setInterfaces(il.toArray(new Class[il.size()])); } enhancer.setClassLoader(cl == null ? Thread.currentThread().getContextClassLoader() : cl); return enhancer.create(); }
From source file:com.jaspersoft.jasperserver.war.helper.GenericParametersHelper.java
private static ParameterizedType findParametrizedType(Class<?> classToParse, Class<?> genericClassToFind, Map<String, Class<?>> inputParameterValues) { ParameterizedType type = null; if (genericClassToFind.isInterface()) { final Type[] genericInterfaces = classToParse.getGenericInterfaces(); if (genericInterfaces != null && genericInterfaces.length > 0) { for (Type genericInterface : genericInterfaces) { if (genericInterface == genericClassToFind) { throw new IllegalArgumentException(classToParse.getName() + " is raw implementation of " + genericClassToFind.getName()); }/*from w ww. j a v a 2 s .c o m*/ if (genericInterface instanceof ParameterizedType) { ParameterizedType currentParametrizedType = (ParameterizedType) genericInterface; Map<String, Class<?>> currentParameterValues = new HashMap<String, Class<?>>( inputParameterValues); if (currentParametrizedType.getRawType() == genericClassToFind) { type = (ParameterizedType) genericInterface; } else { currentParameterValues = getCurrentParameterValues( ((Class<?>) currentParametrizedType.getRawType()).getTypeParameters(), currentParametrizedType.getActualTypeArguments(), new HashMap<String, Class<?>>(inputParameterValues)); type = findParametrizedType((Class<?>) currentParametrizedType.getRawType(), genericClassToFind, currentParameterValues); } if (type != null) { inputParameterValues.clear(); inputParameterValues.putAll(currentParameterValues); break; } } } } } else { final Type genericSuperclass = classToParse.getGenericSuperclass(); if (genericSuperclass == genericClassToFind) { log.debug(classToParse.getName() + " is raw subclass of " + genericClassToFind.getName()); } else if (genericSuperclass instanceof ParameterizedType && ((ParameterizedType) genericSuperclass).getRawType() == genericClassToFind) { type = (ParameterizedType) genericSuperclass; } } return type; }
From source file:Main.java
/** * Converts the given array to a set with the given type. Works like * {@link Arrays#asList(Object...)}.//from ww w .j a va 2s. co m * * @param <T> * The type of the set values. * @param setType * The set type to return. * @param arr * The array to convert into a set. * @return An object of the given set or, if, and only if, an error * occurred, <code>null</code>. */ @SuppressWarnings("unchecked") public static <T> Set<T> toSet(@SuppressWarnings("rawtypes") final Class<? extends Set> setType, final T... arr) { assert setType != null : "SetType cannot be null!"; assert !Modifier.isAbstract(setType.getModifiers()) : "SetType cannot be abstract!"; assert !setType.isInterface() : "SetType cannot be an interface!"; assert arr != null : "Arr cannot be null!"; Set<T> result = null; try { result = setType.newInstance(); for (final T t : arr) { result.add(t); } } catch (final InstantiationException ex) { ex.printStackTrace(); } catch (final IllegalAccessException ex) { ex.printStackTrace(); } return result; }
From source file:com.github.robozonky.app.events.SessionEvents.java
@SuppressWarnings("unchecked") static <T extends Event> Class<T> getImplementingEvent(final Class<T> original) { final Stream<Class<?>> provided = ClassUtils.getAllInterfaces(original).stream(); final Stream<Class<?>> interfaces = original.isInterface() ? // interface could be extending it directly Stream.concat(Stream.of(original), provided) : provided; final String apiPackage = "com.github.robozonky.api.notifications"; return (Class<T>) interfaces.filter(i -> Objects.equals(i.getPackage().getName(), apiPackage)) .filter(i -> i.getSimpleName().endsWith("Event")) .filter(i -> !Objects.equals(i.getSimpleName(), "Event")).findFirst() .orElseThrow(() -> new IllegalStateException("Not an event:" + original)); }
From source file:net.servicefixture.util.ReflectionUtils.java
public static boolean isAbstractType(Class type) { return (type.isInterface() || Modifier.isAbstract(type.getModifiers())) && !type.isPrimitive(); }
From source file:com.norconex.commons.lang.ClassFinder.java
private static String resolveName(ClassLoader loader, String rawName, Class<?> superClass) { String className = rawName;// www . j av a 2 s.co m if (!rawName.endsWith(".class") || className.contains("$")) { return null; } className = className.replaceAll("[\\\\/]", "."); className = StringUtils.removeStart(className, "."); className = StringUtils.removeEnd(className, ".class"); try { Class<?> clazz = loader.loadClass(className); // load only concrete implementations if (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()) && superClass.isAssignableFrom(clazz)) { return clazz.getName(); } } catch (ClassNotFoundException e) { LOG.error("Invalid class: " + className, e); } catch (NoClassDefFoundError e) { LOG.debug("Invalid class: " + className, e); } return null; }