void addIfTestMethod(Method m) { if (m.getAnnotation(Test.class) == null) return; if (!(m.getReturnType().equals(boolean.class) || m.getReturnType().equals(void.class))) throw new RuntimeException("@Test method" + " must return boolean or void"); m.setAccessible(true); // In case it's private, etc. add(m); }
// we set an observer to detect VirtualMachineImpl.dispose call // and on dispose we add corresponding VirtualMachineImpl.class to // free VirtualMachimeImpl Class list. protected static void setVMDisposeObserver(final Object vm) { try { Method setDisposeObserverMethod = vm.getClass() .getDeclaredMethod("setDisposeObserver", new Class[] {java.util.Observer.class}); setDisposeObserverMethod.setAccessible(true); setDisposeObserverMethod.invoke( vm, new Object[] { new Observer() { public void update(Observable o, Object data) { if (DEBUG) { System.out.println("got VM.dispose notification"); } addFreeVMImplClass(vm.getClass()); } } }); } catch (Exception exp) { if (DEBUG) { System.out.println("setVMDisposeObserver() got an exception:"); exp.printStackTrace(); } } }
public static RestMethodWrapper get(final Object pOwner, final Method pMethod) { // Make it work with private methods and pMethod.setAccessible(true); if (HasMethodHandleHelper.HASHANDLES && (!"1.7".equals("java.specification.version"))) { return new Java8RestMethodWrapper(pOwner, pMethod); } else { return new Java6RestMethodWrapper(pOwner, pMethod); } }
private static Method checkForCreatorMethod(Method m) { if (m.getAnnotation(TestObjectCreate.class) == null) return null; if (!m.getReturnType().equals(testClass)) throw new RuntimeException( "@TestObjectCreate " + "must return instance of Class to be tested"); if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) < 1) throw new RuntimeException("@TestObjectCreate " + "must be static."); m.setAccessible(true); return m; }
private static Method checkForCleanupMethod(Method m) { if (m.getAnnotation(TestObjectCleanup.class) == null) return null; if (!m.getReturnType().equals(void.class)) throw new RuntimeException("@TestObjectCleanup " + "must return void"); if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) < 1) throw new RuntimeException("@TestObjectCleanup " + "must be static."); if (m.getParameterTypes().length == 0 || m.getParameterTypes()[0] != testClass) throw new RuntimeException( "@TestObjectCleanup " + "must take an argument of the tested type."); m.setAccessible(true); return m; }
/** * Gets the features of a specific <tt>DiscoverInfo</tt> in the form of a read-only * <tt>Feature</tt> <tt>Iterator<tt/> by calling the internal method {@link * DiscoverInfo#getFeatures()}. * * @param discoverInfo the <tt>DiscoverInfo</tt> the features of which are to be retrieved * @return a read-only <tt>Feature</tt> <tt>Iterator</tt> which lists the features of the * specified <tt>discoverInfo</tt> */ @SuppressWarnings("unchecked") private static Iterator<DiscoverInfo.Feature> getDiscoverInfoFeatures(DiscoverInfo discoverInfo) { Method getFeaturesMethod; try { getFeaturesMethod = DiscoverInfo.class.getDeclaredMethod("getFeatures"); } catch (NoSuchMethodException nsmex) { throw new UndeclaredThrowableException(nsmex); } getFeaturesMethod.setAccessible(true); try { return (Iterator<DiscoverInfo.Feature>) getFeaturesMethod.invoke(discoverInfo); } catch (IllegalAccessException iaex) { throw new UndeclaredThrowableException(iaex); } catch (InvocationTargetException itex) { throw new UndeclaredThrowableException(itex); } }
public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.readLine().trim()); Object o; // Solution starts here if (num < 1 || num > Math.pow(2, 30)) throw new Exception(); Solution ob = new Solution(); Class<?> c = Class.forName("Solution$Private"); Constructor<?> constructor = c.getDeclaredConstructor(Solution.class); constructor.setAccessible(true); o = constructor.newInstance(ob); Method m = c.getDeclaredMethod("powerof2", new Class[] {int.class}); m.setAccessible(true); String ans = (String) m.invoke(o, num); System.out.println(num + " is " + ans); // ends here System.out.println( "An instance of class: " + o.getClass().getSimpleName() + " has been created"); } // end of main