List of usage examples for java.lang.reflect Method invoke
@CallerSensitive @ForceInline @HotSpotIntrinsicCandidate public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
From source file:Main.java
private static Object invokeMethod(Object handler, String callback, boolean fallback, Class<?>[] cls, Class<?>[] cls2, Object... params) throws Exception { if (handler == null || callback == null) return null; Method method = null; try {//from w w w. j a v a 2 s . co m if (cls == null) cls = new Class[0]; method = handler.getClass().getMethod(callback, cls); return method.invoke(handler, params); } catch (NoSuchMethodException e) { //AQUtility.debug(e.getMessage()); } try { if (fallback) { if (cls2 == null) { method = handler.getClass().getMethod(callback); return method.invoke(handler); } else { method = handler.getClass().getMethod(callback, cls2); return method.invoke(handler, params); } } } catch (NoSuchMethodException e) { } return null; }
From source file:Main.java
/** * Computes the maximum bounds of the current screen device. If this method is * called on JDK 1.4, Xinerama-aware results are returned. (See Sun-Bug-ID * 4463949 for details).// w w w. j a v a 2 s . co m * * @return the maximum bounds of the current screen. */ public static Rectangle getMaximumWindowBounds() { final GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { final Method method = GraphicsEnvironment.class.getMethod("getMaximumWindowBounds", (Class[]) null); return (Rectangle) method.invoke(localGraphicsEnvironment, (Object[]) null); } catch (Exception e) { // ignore ... will fail if this is not a JDK 1.4 .. } final Dimension s = Toolkit.getDefaultToolkit().getScreenSize(); return new Rectangle(0, 0, s.width, s.height); }
From source file:Main.java
/** * invoke the leftParameter and get the name property. * it will try the Array.length() and Map.get(), * then it will try get'Name' and is'Name', * last, it will to find the name by invoke field. *///w w w . ja v a2s . c om public static Object searchProperty(Object leftParameter, String name) throws Exception { Class<?> leftClass = leftParameter.getClass(); Object result; if (leftParameter.getClass().isArray() && "length".equals(name)) { result = Array.getLength(leftParameter); } else if (leftParameter instanceof Map) { result = ((Map<Object, Object>) leftParameter).get(name); } else { try { String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); Method method = leftClass.getMethod(getter, new Class<?>[0]); if (!method.isAccessible()) { method.setAccessible(true); } result = method.invoke(leftParameter, new Object[0]); } catch (NoSuchMethodException e2) { try { String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1); Method method = leftClass.getMethod(getter, new Class<?>[0]); if (!method.isAccessible()) { method.setAccessible(true); } result = method.invoke(leftParameter, new Object[0]); } catch (NoSuchMethodException e3) { Field field = leftClass.getField(name); result = field.get(leftParameter); } } } return result; }
From source file:Main.java
public static void putTextToViewById(Activity a, int id, int text) { View view = a.findViewById(id); Method m = null; try {/* w w w.j a v a2s . co m*/ m = view.getClass().getMethod("setText", new Class[] { Integer.TYPE }); } catch (NoSuchMethodException e) { } if (m != null) { try { m.invoke(view, text); } catch (Exception e) { e.printStackTrace(); } } }
From source file:Main.java
public static boolean hasSystemFeature(final Context pContext, final String pFeature) { try {//from ww w . j a va2s . com final Method PackageManager_hasSystemFeatures = PackageManager.class.getMethod("hasSystemFeature", new Class[] { String.class }); return (PackageManager_hasSystemFeatures == null) ? false : (Boolean) PackageManager_hasSystemFeatures.invoke(pContext.getPackageManager(), pFeature); } catch (final Throwable t) { return false; } }
From source file:com.armon.test.quartz.QuartzConfiguration.java
/** * Get the password from the Configuration instance using the * getPassword method if it exists. If not, then fall back to the * general get method for configuration elements. * @param conf configuration instance for accessing the passwords * @param alias the name of the password element * @param defPass the default password//from w ww . j a v a2 s .c o m * @return String password or default password * @throws IOException */ public static String getPassword(Configuration conf, String alias, String defPass) throws IOException { String passwd = null; try { Method m = Configuration.class.getMethod("getPassword", String.class); char[] p = (char[]) m.invoke(conf, alias); if (p != null) { LOG.debug(String.format( "Config option \"%s\" was found through" + " the Configuration getPassword method.", alias)); passwd = new String(p); } else { LOG.debug(String.format("Config option \"%s\" was not found. Using provided default value", alias)); passwd = defPass; } } catch (NoSuchMethodException e) { // this is a version of Hadoop where the credential //provider API doesn't exist yet LOG.debug(String .format("Credential.getPassword method is not available." + " Falling back to configuration.")); passwd = conf.get(alias, defPass); } catch (SecurityException e) { throw new IOException(e.getMessage(), e); } catch (IllegalAccessException e) { throw new IOException(e.getMessage(), e); } catch (IllegalArgumentException e) { throw new IOException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new IOException(e.getMessage(), e); } return passwd; }
From source file:main.java.stroum.vkapp.VK.LongPoll.java
private static void processEvent(JSONArray array, String className) { for (int i = 0; i < array.length(); ++i) { try {// ww w. j a v a 2 s.co m JSONArray arrayItem = (JSONArray) array.get(i); int type = (Integer) arrayItem.get(0); int uid; /* 0,$message_id,0 -- ?? ? local_id 1,$message_id,$flags -- ?? (FLAGS:=$flags) 2,$message_id,$mask[,$user_id] -- ? ?? (FLAGS|=$mask) 3,$message_id,$mask[,$user_id] -- ?? ?? (FLAGS&=~$mask) 4,$message_id,$flags,$from_id,$timestamp,$subject,$text,$attachments -- ?? 8,-$user_id,0 -- $user_id ? 9,-$user_id,$flags -- $user_id ? ($flags 0, ? ? (, ) 1, ? (, ?? away)) 51,$chat_id,$self -- (??, ) ? $chat_id . $self - ? ? 61,$user_id,$flags -- $user_id ? . ? ~5 ? ?? ?. $flags = 1 62,$user_id,$chat_id -- $user_id ? ? $chat_id. 70,$user_id,$call_id -- $user_id ? $call_id, ?? voip.getCallInfo. */ switch (type) { case 8: // uid = Math.abs(Integer.parseInt(arrayItem.get(1).toString())); // System.out.println(VK.getUserName(uid) + " is online"); break; case 9: // uid = Math.abs(Integer.parseInt(arrayItem.get(1).toString())); //System.out.println(VK.getUserName(uid) + " is offline"); break; case 4: Long message_id = Long.parseLong(arrayItem.get(1).toString()); int flags = Integer.parseInt(arrayItem.get(2).toString()); int from_id = Integer.parseInt(arrayItem.get(3).toString()); Long ts = Long.parseLong(arrayItem.get(4).toString()); String subject = arrayItem.get(5).toString(); String text = arrayItem.get(6).toString(); int from_uid = 0; try { JSONObject object = new JSONObject(arrayItem.get(7).toString()); from_uid = Integer.parseInt(object.getString("from")); } catch (Exception ignored) { } try { Class c = Class.forName("main.java.stroum.vkapp.Handlers." + className); // Todo: Fix Class[] paramTypes = new Class[] { Message.class }; Object[] args = new Object[] { new Message(message_id, flags, from_id, ts, text, from_uid) }; Method m = c.getMethod("handleMessage", paramTypes); m.invoke(c, args); } catch (Exception e) { e.printStackTrace(); } break; } } catch (JSONException ignored) { } } }
From source file:biz.wolschon.finance.jgnucash.actions.FileBugInBrowserAction.java
/** * @return instance of "javax.jnlp.ServiceManager" on platforms * that support it.//from w w w. ja v a 2 s . com */ @SuppressWarnings("unchecked") private static Object getJNLPServiceManagerObject() { try { Class serviceManagerClass = Class.forName("javax.jnlp.ServiceManager"); Method lookupMethod = serviceManagerClass.getMethod("lookup", new Class[] { String.class }); return lookupMethod.invoke(null, new Object[] { "javax.jnlp.BasicService" }); } catch (Exception ex) { LOGGER.info( "Cannot instanciate javax.jnlp.ServiceManager " + "- this platform seems not to support it.", ex); return null; } }
From source file:Main.java
/** * Applies stirng to View by Reflection/*from w ww.ja v a 2 s .co m*/ * * @param context Context * @param view View to apply to. * @param attrs Attributes of Layout */ public static void applyResourceStringToView(Context context, View view, AttributeSet attrs) { if (attrs != null) { Resources res = context.getResources(); for (int i = 0; i < attrs.getAttributeCount(); i++) { int resId = attrs.getAttributeResourceValue(i, 0); if (resId > 0) { String resType = res.getResourceTypeName(resId); if (resType.equals("string")) { String attrName = attrs.getAttributeName(i); try { Method method = view.getClass().getMethod("set" + capitalize(attrName), CharSequence.class); method.setAccessible(true); method.invoke(view, res.getString(resId)); } catch (NoSuchMethodException e) { } catch (Exception e) { } } } } } }
From source file:jin.collection.util.PropertyUtil.java
static Object getMethodResult(final Object element, final String methodName, final Object param) throws Exception { Class[] classes = null;/* w w w.j a va 2s .co m*/ Object[] params = null; if (param != null) { classes = new Class[] { param.getClass() }; params = new Object[] { param }; } // TODO: sistemare final Method m = element.getClass().getMethod(methodName, classes); m.setAccessible(true); return m.invoke(element, params); }