List of usage examples for java.lang Class isInterface
@HotSpotIntrinsicCandidate public native boolean isInterface();
From source file:com.edmunds.autotest.ClassUtil.java
public static Object instanceClass(Class cls, String msg) { if (cls.isInterface()) { return Proxy.newProxyInstance(cls.getClassLoader(), new Class[] { cls }, NOOP_HANDLER); } else if (isStandardClass(cls) && hasDefaultConstructor(cls)) { try {/*from ww w. j ava 2 s . com*/ Constructor constructor = getDefaultConstructor(cls); constructor.setAccessible(true); return constructor.newInstance(); } catch (InvocationTargetException e) { log.error(msg, e); throw new RuntimeException(msg, e); } catch (InstantiationException e) { log.error(msg, e); throw new RuntimeException(msg, e); } catch (IllegalAccessException e) { log.error(msg, e); throw new RuntimeException(msg, e); } } return null; }
From source file:edu.usc.goffish.gofs.namenode.NameNodeProvider.java
@SuppressWarnings("unchecked") public static Class<? extends IInternalNameNode> loadNameNodeType(String typeName) throws ClassNotFoundException, ReflectiveOperationException { Class<?> givenType = Class.forName(typeName); if (!IInternalNameNode.class.isAssignableFrom(givenType) || givenType.isInterface() || Modifier.isAbstract(givenType.getModifiers())) { throw new IllegalArgumentException( typeName + " is not a concrete class deriving from IInternalNameNode"); }//from w w w.ja v a 2 s. co m return (Class<? extends IInternalNameNode>) givenType; }
From source file:ReflectionUtils.java
/** * Attempt to find a {@link Method} on the supplied class with the supplied name * and parameter types. Searches all superclasses up to <code>Object</code>. * <p>Returns <code>null</code> if no {@link Method} can be found. * @param clazz the class to introspect//from w w w. j a v a2 s . co m * @param name the name of the method * @param paramTypes the parameter types of the method * @return the Method object, or <code>null</code> if none found */ public static Method findMethod(Class clazz, String name, Class[] paramTypes) { Class searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods()); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (name.equals(method.getName()) && Arrays.equals(paramTypes, method.getParameterTypes())) { return method; } } searchType = searchType.getSuperclass(); } return null; }
From source file:MyClass.java
public static String getClassDescription(Class c) { StringBuilder classDesc = new StringBuilder(); int modifierBits = 0; String keyword = ""; if (c.isInterface()) { modifierBits = c.getModifiers() & Modifier.interfaceModifiers(); if (c.isAnnotation()) { keyword = "@interface"; } else {/*www . j a va 2 s. c o m*/ keyword = "interface"; } } else if (c.isEnum()) { modifierBits = c.getModifiers() & Modifier.classModifiers(); keyword = "enum"; } modifierBits = c.getModifiers() & Modifier.classModifiers(); keyword = "class"; String modifiers = Modifier.toString(modifierBits); classDesc.append(modifiers); classDesc.append(" " + keyword); String simpleName = c.getSimpleName(); classDesc.append(" " + simpleName); String genericParms = getGenericTypeParams(c); classDesc.append(genericParms); Class superClass = c.getSuperclass(); if (superClass != null) { String superClassSimpleName = superClass.getSimpleName(); classDesc.append(" extends " + superClassSimpleName); } String interfaces = Main.getClassInterfaces(c); if (interfaces != null) { classDesc.append(" implements " + interfaces); } return classDesc.toString(); }
From source file:org.jruby.rack.mock.MockAsyncContext.java
private static <T> T instantiate(Class<T> clazz) throws IllegalArgumentException { if (clazz.isInterface()) { throw new IllegalArgumentException(clazz + " is an interface"); }//w ww. j a va 2 s . c om try { return clazz.newInstance(); } catch (InstantiationException ex) { throw new IllegalArgumentException(clazz + " is it an abstract class?", ex); } catch (IllegalAccessException ex) { throw new IllegalArgumentException(clazz + " constructor accessible?", ex); } }
From source file:ReflectApp.java
static void describeClassOrInterface(Class className, String name) { displayModifiers(className.getModifiers()); displayFields(className.getDeclaredFields()); displayMethods(className.getDeclaredMethods()); if (className.isInterface()) { System.out.println("Interface: " + name); } else {/* w ww . j a va 2 s . c o m*/ System.out.println("Class: " + name); displayInterfaces(className.getInterfaces()); displayConstructors(className.getDeclaredConstructors()); } }
From source file:ShowClass.java
/** * Display the modifiers, name, superclass and interfaces of a class or * interface. Then go and list all constructors, fields, and methods. *//*from w ww . j a va 2 s.c o m*/ public static void print_class(Class c) { // Print modifiers, type (class or interface), name and superclass. if (c.isInterface()) { // The modifiers will include the "interface" keyword here... System.out.print(Modifier.toString(c.getModifiers()) + " " + typename(c)); } else if (c.getSuperclass() != null) { System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c) + " extends " + typename(c.getSuperclass())); } else { System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c)); } // Print interfaces or super-interfaces of the class or interface. Class[] interfaces = c.getInterfaces(); if ((interfaces != null) && (interfaces.length > 0)) { if (c.isInterface()) System.out.print(" extends "); else System.out.print(" implements "); for (int i = 0; i < interfaces.length; i++) { if (i > 0) System.out.print(", "); System.out.print(typename(interfaces[i])); } } System.out.println(" {"); // Begin class member listing. // Now look up and display the members of the class. System.out.println(" // Constructors"); Constructor[] constructors = c.getDeclaredConstructors(); for (int i = 0; i < constructors.length; i++) // Display constructors. print_method_or_constructor(constructors[i]); System.out.println(" // Fields"); Field[] fields = c.getDeclaredFields(); // Look up fields. for (int i = 0; i < fields.length; i++) // Display them. print_field(fields[i]); System.out.println(" // Methods"); Method[] methods = c.getDeclaredMethods(); // Look up methods. for (int i = 0; i < methods.length; i++) // Display them. print_method_or_constructor(methods[i]); System.out.println("}"); // End class member listing. }
From source file:com.edmunds.autotest.ClassUtil.java
public static boolean isStandardClass(Class cls) { return !(((cls.getModifiers() & Modifier.ABSTRACT) > 0) || cls.isInterface() || cls.isMemberClass() || cls.isLocalClass() || cls.isSynthetic()); }
From source file:cz.lbenda.common.ClassLoaderHelper.java
@SuppressWarnings("unchecked") public static List<String> instancesOfClass(Class clazz, List<String> libs, boolean abstractClass, boolean interf) { List<String> classNames = new ArrayList<>(); ClassLoader clr = createClassLoader(libs, false); List<String> result = new ArrayList<>(); libs.forEach(lib -> {/* ww w . ja va2 s .com*/ try (ZipFile file = new ZipFile(lib)) { file.stream().forEach(entry -> { if (entry.getName().equals("META-INF/services/" + clazz.getName())) { try { String string = IOUtils.toString(file.getInputStream(entry)); String[] lines = string.split("\n"); for (String line : lines) { result.add(line.trim()); } } catch (IOException e) { throw new RuntimeException(e); } } else if (entry.getName().endsWith(".class")) { String className = entry.getName().substring(0, entry.getName().length() - 6).replace("/", "."); classNames.add(className); } }); } catch (IOException e) { throw new RuntimeException(e); } }); classNames.forEach(cc -> { try { Class cla = clr.loadClass(cc); if ((interf || !cla.isInterface()) && (abstractClass || !Modifier.isAbstract(cla.getModifiers())) && clazz.isAssignableFrom(cla)) { if (!result.contains(cc)) { result.add(cc); } } } catch (ClassNotFoundException | NoClassDefFoundError e) { /* It's common to try to create class which haven't class which need*/ } }); return result; }
From source file:com.wavemaker.tools.apidocs.tools.parser.util.MethodUtils.java
private static Set<Method> getMethods(Class<?> type, Predicate<? super Method> predicate) { final Method[] declaredMethods = type.isInterface() ? type.getMethods() : type.getDeclaredMethods(); Set<Method> filtered = new LinkedHashSet<>(); if (declaredMethods != null && declaredMethods.length > 0) { for (final Method method : declaredMethods) { if (predicate.apply(method)) { filtered.add(method);// ww w . java 2 s . co m } } } return filtered; }