public static <T> T newInstanceOrFallback(String jdk7Type, ClassLoader loader, Class<? extends T> fallbackType) { // Use java 7 APIs, if available Class<?> handlerClass = null; if (JavaVersion.current().isJava7Compatible()) { try { handlerClass = loader.loadClass(jdk7Type); } catch (ClassNotFoundException e) { // Ignore } } if (handlerClass == null) { handlerClass = fallbackType; } try { return Cast.uncheckedCast(handlerClass.newInstance()); } catch (Exception e) { throw UncheckedException.throwAsUncheckedException(e); } }
public static <T> T newInstanceOrFallback(String jdk7Type, ClassLoader loader, Class<? extends T> fallbackType) { // Use java 7 APIs, if available Class<?> handlerClass = null; if (JavaVersion.current().isJava7Compatible()) { try { handlerClass = loader.loadClass(jdk7Type); } catch (ClassNotFoundException e) { // Ignore } } if (handlerClass == null) { handlerClass = fallbackType; } try { return Cast.uncheckedCast(handlerClass.newInstance()); } catch (Exception e) { throw UncheckedException.throwAsUncheckedException(e); } }
private DirectoryWalker createInstance() { if (javaVersion.isJava8Compatible() || (javaVersion.isJava7Compatible() && defaultEncodingContainsPlatformEncoding())) { return new Jdk7DirectoryWalker(fileSystem); } else { return new DefaultDirectoryWalker(fileSystem); } }
@Test public void testWithSuppressed() throws Exception { // Java 7 only test. Assume.assumeTrue("This test is for Java7 only", JavaVersion.current().isJava7Compatible()); IOException wrapped = new IOException("testWithSuppressed-Message"); Method addSuppressed = ReflectionUtils.tryGetPublicMethod( wrapped.getClass(), "addSuppressed", Void.TYPE, Throwable.class); assertNotNull("Expected to have addSuppressed in Java 7", addSuppressed); Exception suppressed1 = new NotSerializableTestException("testWithSuppressed-suppressed1"); Exception suppressed2 = new IOException("testWithSuppressed-suppressed2"); addSuppressed.invoke(wrapped, suppressed1); addSuppressed.invoke(wrapped, suppressed2); TransferableExceptionWrapper wrapper = TransferableExceptionWrapper.wrap(wrapped); checkWrapper(wrapped, wrapper); Method getSuppressed = ReflectionUtils.tryGetPublicMethod( wrapper.getClass(), "getSuppressed", Throwable[].class); assertNotNull("Expected to have getSuppressed in Java 7", getSuppressed); Throwable[] wrappedSuppressed = (Throwable[])getSuppressed.invoke(wrapper); assertEquals("Must wrapp suppressed exceptions as well.", 2, wrappedSuppressed.length); checkWrapperOrSame(suppressed1, wrappedSuppressed[0]); checkWrapperOrSame(suppressed2, wrappedSuppressed[1]); }