List of usage examples for java.lang Class getDeclaredMethod
@CallerSensitive public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:au.com.dw.testdatacapturej.reflection.util.ReflectionUtil.java
/** * Check if a class has a default setter method for a field, using the specific class of the field only. * i.e. no superclasses or interfaces.//w ww .ja v a2 s . c o m * * Since we may want to invoke the setter method, abstract methods are not included (i.e. will return * false). * * @param clazz * @param parameterClass * @param setterMethodName * @return */ private static boolean hasSetterMethodForParameterClass(Class<?> clazz, String setterMethodName, Class<?> parameterClass) { boolean hasSetterMethodForParameterClass = false; try { Method method = clazz.getDeclaredMethod(setterMethodName, parameterClass); if (method != null) { int mod = method.getModifiers(); // ignore abstract methods if (!Modifier.isAbstract(mod)) { hasSetterMethodForParameterClass = true; } } } catch (NoSuchMethodException nsme) { // no need to do anything, since this is what we are checking for } catch (Exception e) { e.printStackTrace(); } return hasSetterMethodForParameterClass; }
From source file:ml.shifu.shifu.util.ClassUtils.java
public static Method getDeclaredMethod(String methodName, Class<?> clazz) throws NoSuchMethodException { String key = clazz.getName() + "#" + methodName; Method cacheMethod = METHOD_CACHE.get(key); if (cacheMethod != null) { return cacheMethod; }/*w w w . ja v a 2 s . co m*/ return clazz.getDeclaredMethod(methodName, ClassUtils.EMPTY_CLASS_ARRAY); }
From source file:org.zywx.wbpalmstar.plugin.uexiconlist.utils.IconListUtils.java
/** * ???/* w w w .ja va 2 s . co m*/ * * @param className * ?? * @param methodName * ?? * @param parmTypeList * ? * @return */ public static Method getReflectionMethod(String className, String methodName, Class<?>[] parmTypeList) { Method mMethod = null; try { Class<?> mClass = Class.forName(className); try { /** ?methodName, parmList?public?public */ mMethod = mClass.getDeclaredMethod(methodName, parmTypeList); mMethod.setAccessible(true); } catch (NoSuchMethodException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return mMethod; }
From source file:com.frostwire.android.gui.UniversalScanner.java
private static Uri nativeScanFile(Context context, String path) { try {// w ww .ja v a 2 s. co m File f = new File(path); Class<?> clazz = Class.forName("android.media.MediaScanner"); Constructor<?> mediaScannerC = clazz.getDeclaredConstructor(Context.class); Object scanner = mediaScannerC.newInstance(context); try { Method setLocaleM = clazz.getDeclaredMethod("setLocale", String.class); setLocaleM.invoke(scanner, Locale.US.toString()); } catch (Throwable e) { e.printStackTrace(); } Field mClientF = clazz.getDeclaredField("mClient"); mClientF.setAccessible(true); Object mClient = mClientF.get(scanner); Method scanSingleFileM = clazz.getDeclaredMethod("scanSingleFile", String.class, String.class, String.class); Uri fileUri = (Uri) scanSingleFileM.invoke(scanner, f.getAbsolutePath(), "external", "data/raw"); int n = context.getContentResolver().delete(fileUri, null, null); if (n > 0) { LOG.debug("Deleted from Files provider: " + fileUri); } Field mNoMediaF = mClient.getClass().getDeclaredField("mNoMedia"); mNoMediaF.setAccessible(true); mNoMediaF.setBoolean(mClient, false); // This is only for HTC (tested only on HTC One M8) try { Field mFileCacheF = clazz.getDeclaredField("mFileCache"); mFileCacheF.setAccessible(true); mFileCacheF.set(scanner, new HashMap<String, Object>()); } catch (Throwable e) { // no an HTC, I need some time to refactor this hack } try { Field mFileCacheF = clazz.getDeclaredField("mNoMediaPaths"); mFileCacheF.setAccessible(true); mFileCacheF.set(scanner, new HashMap<String, String>()); } catch (Throwable e) { e.printStackTrace(); } Method doScanFileM = mClient.getClass().getDeclaredMethod("doScanFile", String.class, String.class, long.class, long.class, boolean.class, boolean.class, boolean.class); Uri mediaUri = (Uri) doScanFileM.invoke(mClient, f.getAbsolutePath(), null, f.lastModified(), f.length(), false, true, false); Method releaseM = clazz.getDeclaredMethod("release"); releaseM.invoke(scanner); return mediaUri; } catch (Throwable e) { e.printStackTrace(); return null; } }
From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java
/** * Retrieve the <em>default value</em> of a named Annotation attribute, given the {@link Class annotation type}. * @param annotationType the <em>annotation type</em> for which the default value should be retrieved * @param attributeName the name of the attribute value to retrieve. * @return the default value of the named attribute, or {@code null} if not found * @see #getDefaultValue(java.lang.annotation.Annotation, String) *//*from ww w . j a v a 2 s. co m*/ public static Object getDefaultValue(Class<? extends Annotation> annotationType, String attributeName) { try { Method method = annotationType.getDeclaredMethod(attributeName, new Class[0]); return method.getDefaultValue(); } catch (Exception ex) { return null; } }
From source file:Main.java
private static Method getColumnSetMethod(Class<?> entityType, Field field, Class<?> typeClass, String suffix) { String fieldName = field.getName(); Method setMethod = null;//from w w w.j a v a 2 s . co m if (field.getType() == boolean.class) { setMethod = getBooleanColumnSetMethod(entityType, field, typeClass, suffix); } if (setMethod == null) { String methodName = "set" + fieldName.substring(0, 1).toUpperCase(Locale.getDefault()) + fieldName.substring(1) + suffix; try { setMethod = entityType.getDeclaredMethod(methodName, typeClass); } catch (NoSuchMethodException e) { Log.d("T", methodName + " not exist"); } } return setMethod; }
From source file:Main.java
private static Method getBooleanColumnSetMethod(Class<?> entityType, Field field, Class<?> typeClass, String suffix) {/* w w w . j a va 2 s . c o m*/ String fieldName = field.getName(); String methodName = null; if (isStartWithIs(field.getName())) { methodName = "set" + fieldName.substring(2, 3).toUpperCase(Locale.getDefault()) + fieldName.substring(3) + suffix; } else { methodName = "set" + fieldName.substring(0, 1).toUpperCase(Locale.getDefault()) + fieldName.substring(1) + suffix; } try { return entityType.getDeclaredMethod(methodName, typeClass); } catch (NoSuchMethodException e) { Log.d("L", methodName + " not exist"); } return null; }
From source file:Main.java
static public Object invokeMethod(Object owner, String methodName, Object... args) throws Exception { Class<?> ownerClass = owner.getClass(); Class<?>[] argsClass = null; if (args != null && args.length > 0) { argsClass = new Class<?>[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); if (argsClass[i] == Integer.class) { argsClass[i] = int.class; } else if (argsClass[i] == Boolean.class) { argsClass[i] = boolean.class; }//from w ww . j a v a2 s . co m } } Method method = ownerClass.getDeclaredMethod(methodName, argsClass); method.setAccessible(true); return method.invoke(owner, args); }
From source file:org.apache.cassandra.db.marshal.TypeParser.java
private static AbstractType<?> getRawAbstractType(Class<? extends AbstractType<?>> typeClass, TypeParser parser) throws ConfigurationException { try {// w w w. j a va2s . c o m Method method = typeClass.getDeclaredMethod("getInstance", TypeParser.class); return (AbstractType<?>) method.invoke(null, parser); } catch (NoSuchMethodException e) { throw new ConfigurationException("Invalid comparator class " + typeClass.getName() + ": must define a public static instance field or a public static method getInstance(TypeParser)."); } catch (IllegalAccessException e) { throw new ConfigurationException("Invalid comparator class " + typeClass.getName() + ": must define a public static instance field or a public static method getInstance(TypeParser)."); } catch (InvocationTargetException e) { ConfigurationException ex = new ConfigurationException( "Invalid definition for comparator " + typeClass.getName() + "."); ex.initCause(e.getTargetException()); throw ex; } }
From source file:apple.dts.samplecode.osxadapter.OSXAdapter.java
public static void setHandler(OSXAdapter adapter) { try {/* w w w. j a v a2 s . c om*/ Class<?> applicationClass = Class.forName("com.apple.eawt.Application"); if (macOSXApplication == null) { macOSXApplication = applicationClass.getConstructor((Class<?>[]) null).newInstance((Object[]) null); } Class<?> applicationListenerClass = Class.forName("com.apple.eawt.ApplicationListener"); Method addListenerMethod = applicationClass.getDeclaredMethod("addApplicationListener", new Class<?>[] { applicationListenerClass }); // Create a proxy object around this handler that can be reflectively added as an Apple ApplicationListener Object osxAdapterProxy = Proxy.newProxyInstance(OSXAdapter.class.getClassLoader(), new Class<?>[] { applicationListenerClass }, adapter); addListenerMethod.invoke(macOSXApplication, new Object[] { osxAdapterProxy }); } catch (ClassNotFoundException cnfe) { System.err.println( "This version of Mac OS X does not support the Apple EAWT. ApplicationEvent handling has been disabled (" + cnfe + ")"); } catch (Exception ex) { // Likely a NoSuchMethodException or an IllegalAccessException loading/invoking eawt.Application methods log.error("Mac OS X Adapter could not talk to EAWT:", ex); } }