List of usage examples for java.lang Class getMethod
@CallerSensitive public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:com.revolsys.util.JavaBeanUtil.java
/** * Clone the value if it has a clone method. * * @param value The value to clone.// w w w . j a v a 2 s . co m * @return The cloned value. */ @SuppressWarnings("unchecked") public static <V> V clone(final V value) { if (value instanceof Map) { final Map<Object, Object> sourceMap = (Map<Object, Object>) value; final Map<Object, Object> map = new LinkedHashMap<>(sourceMap); for (final Entry<Object, Object> entry : sourceMap.entrySet()) { final Object key = entry.getKey(); final Object mapValue = entry.getValue(); final Object clonedMapValue = clone(mapValue); map.put(key, clonedMapValue); } return (V) map; } else if (value instanceof List) { final List<?> list = (List<?>) value; final List<Object> cloneList = new ArrayList<>(); for (final Object object : list) { final Object clonedObject = clone(object); cloneList.add(clonedObject); } return (V) cloneList; } else if (value instanceof Cloneable) { try { final Class<? extends Object> valueClass = value.getClass(); final Method method = valueClass.getMethod("clone", ARRAY_CLASS_0); if (method != null) { return (V) method.invoke(value, ARRAY_OBJECT_0); } } catch (final Throwable e) { return Exceptions.throwUncheckedException(e); } } return value; }
From source file:com.revolsys.util.JavaBeanUtil.java
public static Method getMethod(final Class<?> clazz, final String name, final Class<?>... parameterTypes) { try {/* ww w.j a v a 2 s . c om*/ final Method method = clazz.getMethod(name, parameterTypes); return method; } catch (final NoSuchMethodException e) { throw new IllegalArgumentException(e); } }
From source file:com.antsdb.saltedfish.util.UberUtil.java
@SuppressWarnings("unchecked") public static <T> T clone(final T obj) throws CloneNotSupportedException { if (obj == null) { return null; }/*from w w w. j a v a 2 s .co m*/ if (obj instanceof Cloneable) { Class<?> clazz = obj.getClass(); Method m; try { m = clazz.getMethod("clone", (Class[]) null); } catch (NoSuchMethodException ex) { throw new NoSuchMethodError(ex.getMessage()); } try { return (T) m.invoke(obj, (Object[]) null); } catch (InvocationTargetException ex) { Throwable cause = ex.getCause(); if (cause instanceof CloneNotSupportedException) { throw ((CloneNotSupportedException) cause); } else { throw new Error("Unexpected exception", cause); } } catch (IllegalAccessException ex) { throw new IllegalAccessError(ex.getMessage()); } } else { throw new CloneNotSupportedException(); } }
From source file:fit.Fixture.java
public static boolean hasParseMethod(Class<?> type) { try {// ww w.j a v a 2 s. c o m type.getMethod("parse", String.class); return true; } catch (NoSuchMethodException e) { return false; } }
From source file:com.haulmont.bali.util.ReflectionHelper.java
/** * Searches for a method by its name and arguments. * @param c class// ww w .jav a 2 s.c o m * @param name method name * @param params method arguments * @return method reference or null if a suitable method not found */ @Nullable public static Method findMethod(Class c, String name, Object... params) { Class[] paramTypes = getParamTypes(params); Method method = null; try { method = c.getDeclaredMethod(name, paramTypes); } catch (NoSuchMethodException e) { try { method = c.getMethod(name, paramTypes); } catch (NoSuchMethodException e1) { // } } if (method != null) method.setAccessible(true); return method; }
From source file:Main.java
private static boolean setProxyPreICS(WebView webView, String host, final int port) { HttpHost proxyServer;// w w w . ja v a 2 s. c o m if (null == host) { proxyServer = null; } else { proxyServer = new HttpHost(host, port); } Class networkClass = null; Object network = null; try { networkClass = Class.forName("android.webkit.Network"); if (networkClass == null) { Log.e(LOG_TAG, "failed to get class for android.webkit.Network"); return false; } Method getInstanceMethod = networkClass.getMethod("getInstance", Context.class); if (getInstanceMethod == null) { Log.e(LOG_TAG, "failed to get getInstance method"); } network = getInstanceMethod.invoke(networkClass, new Object[] { webView.getContext() }); } catch (Exception ex) { Log.e(LOG_TAG, "error getting network: " + ex); return false; } if (network == null) { Log.e(LOG_TAG, "error getting network: network is null"); return false; } Object requestQueue = null; try { Field requestQueueField = networkClass.getDeclaredField("mRequestQueue"); requestQueue = getFieldValueSafely(requestQueueField, network); } catch (Exception ex) { Log.e(LOG_TAG, "error getting field value"); return false; } if (requestQueue == null) { Log.e(LOG_TAG, "Request queue is null"); return false; } Field proxyHostField = null; try { Class requestQueueClass = Class.forName("android.net.http.RequestQueue"); proxyHostField = requestQueueClass.getDeclaredField("mProxyHost"); } catch (Exception ex) { Log.e(LOG_TAG, "error getting proxy host field"); return false; } boolean temp = proxyHostField.isAccessible(); try { proxyHostField.setAccessible(true); proxyHostField.set(requestQueue, proxyServer); } catch (Exception ex) { Log.e(LOG_TAG, "error setting proxy host"); } finally { proxyHostField.setAccessible(temp); } Log.d(LOG_TAG, "Setting proxy with <= 3.2 API successful!"); return true; }
From source file:com.haulmont.cuba.core.config.type.TypeFactory.java
@Nullable private static TypeFactory getInferred(Class<?> returnType) { // special case due to Character.valueOf(char) does not accept String if (returnType == Character.class) { return new CharTypeFactory(); }/*from w w w .j a va 2s . c o m*/ for (String methodName : FACTORY_METHOD_NAMES) { try { Method factoryMethod = returnType.getMethod(methodName, String.class); if (isAcceptableMethod(returnType, factoryMethod)) { return new StaticTypeFactory(factoryMethod); } } catch (NoSuchMethodException ex) { // Ignore failure. } } try { Constructor ctor = returnType.getConstructor(String.class); if (Modifier.isPublic(ctor.getModifiers())) { return new ConstructorTypeFactory(ctor); } } catch (NoSuchMethodException e) { return null; } return null; }
From source file:AWTUtilities.java
/** * Sets the state of the specified frame, as specified by * <code>Frame.setExtendedState</code> if running in JDK 1.4 or later, * otherwise does nothing. The call to <code>Frame.setExtendedState()</code> * is done via reflection to avoid runtime errors. */// w ww . java 2 s . co m public static void setExtendedFrameState(Frame frame, int state) { if (PlatformUtils.isJavaBetterThan("1.4")) { try { Class frameClass = Class.forName("java.awt.Frame"); Method setExtendedStateMethod = frameClass.getMethod("setExtendedState", new Class[] { int.class }); setExtendedStateMethod.invoke(frame, new Object[] { new Integer(state) }); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
From source file:edu.cornell.mannlib.vedit.util.OperationUtils.java
public static void beanSetAndValidate(Object newObj, String field, String value, EditProcessObject epo) { Class<?> cls = (epo.getBeanClass() != null) ? epo.getBeanClass() : newObj.getClass(); Class<?>[] paramList = new Class[1]; paramList[0] = String.class; boolean isInt = false; boolean isBoolean = false; Method setterMethod = null;/*from w w w . j a v a2s . c om*/ try { setterMethod = cls.getMethod("set" + field, paramList); } catch (NoSuchMethodException e) { // let's try int paramList[0] = int.class; try { setterMethod = cls.getMethod("set" + field, paramList); isInt = true; } catch (NoSuchMethodException f) { // let's try boolean paramList[0] = boolean.class; try { setterMethod = cls.getMethod("set" + field, paramList); isBoolean = true; log.debug("found boolean field " + field); } catch (NoSuchMethodException g) { log.error("beanSet could not find an appropriate String, int, or boolean setter method for " + field); } } } Object[] arglist = new Object[1]; if (isInt) arglist[0] = Integer.decode(value); else if (isBoolean) arglist[0] = (value.equalsIgnoreCase("TRUE")); else arglist[0] = value; try { setterMethod.invoke(newObj, arglist); } catch (Exception e) { log.error("Couldn't invoke method"); log.error(e.getMessage()); log.error(field + " " + arglist[0]); } }
From source file:AWTUtilities.java
/** * Returns the state of the specified frame, as specified by * <code>Frame.getExtendedState()</code> if running under JDK 1.4 or later, * otherwise returns 0. The call to <code>Frame.getExtendedState()</code> is * done via reflection to avoid runtime errors. *///w w w . ja v a2 s. c o m public static int getExtendedFrameState(Frame frame) { if (PlatformUtils.isJavaBetterThan("1.4")) { try { Class frameClass = Class.forName("java.awt.Frame"); Method getExtendedStateMethod = frameClass.getMethod("getExtendedState", new Class[0]); Integer state = (Integer) getExtendedStateMethod.invoke(frame, new Object[0]); return state.intValue(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } return 0; }