List of usage examples for java.lang.reflect Modifier isAbstract
public static boolean isAbstract(int mod)
From source file:org.apache.ddlutils.io.RoundtripTestBase.java
/** * Creates the test suite for the given test class which must be a sub class of * {@link RoundtripTestBase}. If the platform supports it, it will be tested * with both delimited and undelimited identifiers. * // www .ja v a 2 s .c o m * @param testedClass The tested class * @return The tests */ protected static TestSuite getTests(Class testedClass) { if (!RoundtripTestBase.class.isAssignableFrom(testedClass) || Modifier.isAbstract(testedClass.getModifiers())) { throw new DdlUtilsException("Cannot create parameterized tests for class " + testedClass.getName()); } TestSuite suite = new TestSuite(); try { Method[] methods = testedClass.getMethods(); PlatformInfo info = null; RoundtripTestBase newTest; for (int idx = 0; (methods != null) && (idx < methods.length); idx++) { if (methods[idx].getName().startsWith("test") && ((methods[idx].getParameterTypes() == null) || (methods[idx].getParameterTypes().length == 0))) { newTest = (RoundtripTestBase) testedClass.newInstance(); newTest.setName(methods[idx].getName()); newTest.setUseDelimitedIdentifiers(false); suite.addTest(newTest); if (info == null) { info = PlatformFactory.createNewPlatformInstance(newTest.getDatabaseName()) .getPlatformInfo(); } if (info.isDelimitedIdentifiersSupported()) { newTest = (RoundtripTestBase) testedClass.newInstance(); newTest.setName(methods[idx].getName()); newTest.setUseDelimitedIdentifiers(true); suite.addTest(newTest); } } } } catch (Exception ex) { throw new DdlUtilsException(ex); } return suite; }
From source file:edu.northwestern.bioinformatics.studycalendar.xml.writers.ChangeXmlSerializerFactoryTest.java
@SuppressWarnings({ "RawUseOfParameterizedType" }) public void testASerializerCanBeBuiltForEveryChange() throws Exception { List<String> errors = new ArrayList<String>(); for (Class<? extends Change> aClass : domainReflections.getSubTypesOf(Change.class)) { if (Modifier.isPublic(aClass.getModifiers()) && !Modifier.isAbstract(aClass.getModifiers()) && !aClass.isAnonymousClass()) { Change c = aClass.newInstance(); try { factory.createXmlSerializer(c); } catch (StudyCalendarError sce) { errors.add(String.format("Failure with %s: %s", c, sce.getMessage())); }// w ww . j av a 2 s .c o m } } if (!errors.isEmpty()) { fail(StringUtils.join(errors.iterator(), '\n')); } }
From source file:edu.northwestern.bioinformatics.studycalendar.xml.writers.ChangeableXmlSerializerFactoryTest.java
@SuppressWarnings({ "RawUseOfParameterizedType" }) public void testASerializerCanBeBuiltForEveryChangeable() throws Exception { List<String> errors = new ArrayList<String>(); for (Class<? extends Changeable> aClass : domainReflections.getSubTypesOf(Changeable.class)) { if (!Modifier.isAbstract(aClass.getModifiers()) && !aClass.isAnonymousClass()) { Changeable c = aClass.newInstance(); try { factory.createXmlSerializer(c); } catch (StudyCalendarError sce) { errors.add(String.format("Failure with %s: %s", c, sce.getMessage())); }// w w w. ja v a 2s. com } } if (!errors.isEmpty()) { fail(StringUtils.join(errors.iterator(), '\n')); } }
From source file:org.spongepowered.common.util.ReflectionUtil.java
public static <T> T createInstance(final Class<T> objectClass, Object... args) { checkArgument(!Modifier.isAbstract(objectClass.getModifiers()), "Cannot construct an instance of an abstract class!"); checkArgument(!Modifier.isInterface(objectClass.getModifiers()), "Cannot construct an instance of an interface!"); if (args == null) { args = new Object[] { null }; }/*from w w w .jav a 2s .c o m*/ final Constructor<T> ctor = findConstructor(objectClass, args); try { return ctor.newInstance(args); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { SpongeImpl.getLogger().error("Couldn't find an appropriate constructor for " + objectClass.getCanonicalName() + "with the args: " + Arrays.toString(args), e); } throw new IllegalArgumentException("Couldn't find an appropriate constructor for " + objectClass.getCanonicalName() + "the args: " + Arrays.toString(args)); }
From source file:nl.strohalm.cyclos.utils.ClassHelper.java
/** * If the class is concrete, return it. Else, try to find a well known subclass * @param <T> The specified class type * @param <C> The resulting class type * @param clazz The specified class//from w w w . j a va 2s. c om * @return The class if it is concrete, or a well-known subclass. * @throws IllegalArgumentException The specified class is abstract (or interface) and not a well-known one */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T, C extends T> Class<C> concreteClass(final Class<T> clazz) { if (clazz == null) { throw new NullPointerException("null.clazz"); } Class<C> ret = null; final int modifiers = clazz.getModifiers(); if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers) || Collection.class.isAssignableFrom(clazz)) { if (Calendar.class.isAssignableFrom(clazz)) { ret = (Class) GregorianCalendar.class; } else if (SortedSet.class.isAssignableFrom(clazz)) { ret = (Class) TreeSet.class; } else if (Set.class.isAssignableFrom(clazz)) { ret = (Class) LinkedHashSet.class; } else if (Collection.class.isAssignableFrom(clazz)) { ret = (Class) ArrayList.class; } else if (SortedMap.class.isAssignableFrom(clazz)) { ret = (Class) TreeMap.class; } else if (Map.class.isAssignableFrom(clazz)) { ret = (Class) LinkedHashMap.class; } else { throw new IllegalArgumentException("Unknown concrete class for " + clazz.getName()); } } else { ret = (Class) clazz; } return ret; }
From source file:com.cuubez.visualizer.resource.ResourceMetaDataScanner.java
public static boolean isResource(Class<?> clazz) { if (Modifier.isInterface(clazz.getModifiers()) || Modifier.isAbstract(clazz.getModifiers())) { return false; }//ww w. java 2 s. c o m if (clazz.getAnnotation(Path.class) != null && clazz.getAnnotation(Group.class) != null) { return true; } Class<?> declaringClass = clazz; while (!declaringClass.equals(Object.class)) { // try a superclass Class<?> superclass = declaringClass.getSuperclass(); if (superclass.getAnnotation(Path.class) != null && clazz.getAnnotation(Group.class) != null) { return true; } // try interfaces Class<?>[] interfaces = declaringClass.getInterfaces(); for (Class<?> interfaceClass : interfaces) { if (interfaceClass.getAnnotation(Path.class) != null && clazz.getAnnotation(Group.class) != null) { return true; } } declaringClass = declaringClass.getSuperclass(); } return false; }
From source file:blue.lapis.pore.impl.event.PoreEventTest.java
@Test public void findUnimplementedEvents() throws IOException { Set<Class<?>> events = Sets.newLinkedHashSet(); for (ClassPath.ClassInfo info : PoreTests.getClassPath().getTopLevelClassesRecursive(BUKKIT_PACKAGE)) { try {/*w ww . jav a 2 s . c o m*/ Class<?> event = info.load(); if (Event.class.isAssignableFrom(event) && !Modifier.isAbstract(event.getModifiers())) { events.add(event); } } catch (Throwable e) { PoreTests.getLogger().warn("Failed to load {}", info, e); } } for (ClassPath.ClassInfo info : PoreTests.getClassPath().getTopLevelClassesRecursive(PORE_PACKAGE)) { Class<?> type; try { type = info.load(); if (!Event.class.isAssignableFrom(type)) { continue; } } catch (Throwable e) { PoreTests.getLogger().warn("Failed to load {}", info, e); continue; } events.remove(type.getSuperclass()); } if (!events.isEmpty()) { for (Class<?> event : events) { String bukkitPackage = StringUtils.removeStart(event.getPackage().getName(), BUKKIT_PACKAGE + '.'); PoreTests.getLogger().warn("{}: Pore{} is missing", bukkitPackage, event.getSimpleName()); } } }
From source file:org.lanternpowered.server.util.ReflectionHelper.java
public static <T> T createInstance(final Class<T> objectClass, @Nullable Object... args) { checkArgument(!Modifier.isAbstract(objectClass.getModifiers()), "Cannot construct an instance of an abstract class!"); checkArgument(!Modifier.isInterface(objectClass.getModifiers()), "Cannot construct an instance of an interface!"); if (args == null) { args = new Object[] { null }; }//from w w w . ja v a2 s . co m final Constructor<T> ctor = findConstructor(objectClass, args); try { return ctor.newInstance(args); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { Lantern.getLogger().error("Couldn't find an appropriate constructor for " + objectClass.getCanonicalName() + "with the args: " + Arrays.toString(args), e); } throw new IllegalArgumentException("Couldn't find an appropriate constructor for " + objectClass.getCanonicalName() + "the args: " + Arrays.toString(args)); }
From source file:org.xenei.jena.entities.impl.ResourceEntityProxy.java
/** * Invokes an method on the proxy.//w w w . j a va2s. co m */ @Override public Object intercept(final Object obj, final Method m, final Object[] args, final MethodProxy proxy) throws Throwable { // handle the cases where the method is not abstract if (!Modifier.isAbstract(m.getModifiers())) { return interceptNonAbstract(obj, m, args, proxy); } else { return interceptAnnotated(obj, m, args, proxy); } }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static Map<?, ?> instantiateMap(Class<?> mapClass) { if (!Map.class.isAssignableFrom(mapClass)) { throw new IllegalArgumentException(); }/* w w w .j a va 2 s . co m*/ if (!Modifier.isAbstract(mapClass.getModifiers())) { try { //noinspection unchecked return (Map<?, ?>) mapClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new IllegalStateException(e); } } return new HashMap<>(); }