List of usage examples for java.lang Class getMethod
@CallerSensitive public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:Main.java
public static Method getMethod(final Class<?> targetClass, final String name, final Class<?>... parameterTypes) { if (targetClass == null || TextUtils.isEmpty(name)) { return null; }//from w ww. ja va2 s .c o m try { return targetClass.getMethod(name, parameterTypes); } catch (final SecurityException | NoSuchMethodException e) { // ignore } return null; }
From source file:biz.wolschon.finance.jgnucash.actions.FileBugInBrowserAction.java
/** * @return instance of "javax.jnlp.ServiceManager" on platforms * that support it./* ww w . j av a2 s.c o m*/ */ @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:com.wxxr.nirvana.ContainerAccess.java
/** * Removes an attribute from a context.//from w w w . j av a 2 s.c o m * * @param context * The context object to use. * @param name * The name of the attribute to remove. * @throws NirvanaException * If something goes wrong during removal. */ private static void removeAttribute(Object context, String name) throws NirvanaException { try { Class<?> contextClass = context.getClass(); Method attrMethod = contextClass.getMethod("removeAttribute", String.class); attrMethod.invoke(context, name); } catch (Exception e) { throw new NirvanaException("Unable to remove attribute for specified context: '" + context + "'"); } }
From source file:gr.demokritos.iit.textforms.TextForm.java
/** * Get an instance of the correct subclass - the subclass that can parse the * JSON. If an error is encountered it throws an exception like specified * below.// ww w .jav a2s. c om * * @param data, the data representing the text object * @param classname, the class - subclass of TextForm to use to parse * the format. Will cal fromFormat of that class. * @return An instance of the class that parses the JSON * @throws gr.demokritos.iit.textforms.TextForm.InvalidFormatException */ public static TextForm fromFormat(String data, Class classname) throws InvalidFormatException { try { Class<? extends TextForm> textform = classname.asSubclass(TextForm.class); Method fromFormat = textform.getMethod("fromFormat", String.class); return textform.cast(fromFormat.invoke(null, data)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ex) { if (!API.mapping.containsValue(classname)) { Logger.getLogger(TextForm.class.getName()).log(Level.SEVERE, "fromFormat generics expects one of the " + "following values {0}", API.mapping.values().toString()); } Logger.getLogger(TextForm.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:com.wxxr.nirvana.ContainerAccess.java
/** * Returns an attribute from a context./*w w w . j a v a2 s .c o m*/ * * @param context * The context object to use. * @param attributeName * The name of the attribute to search for. * @return The object, that is the value of the specified attribute. */ private static Object getAttribute(Object context, String attributeName) { try { Class<?> contextClass = context.getClass(); Method attrMethod = contextClass.getMethod("getAttribute", String.class); return attrMethod.invoke(context, attributeName); } catch (Exception e) { LOG.warn("Unable to retrieve container from specified context: '" + context + "'", e); return null; } }
From source file:com.abiquo.model.util.ModelTransformer.java
private static Method setter(final String fieldName, final Class clazz, final Class type) throws Exception { String name = "set" + StringUtils.capitalize(fieldName); Method method = clazz.getMethod(name, new Class[] { type }); if (method != null) { method.setAccessible(true);//from w w w . j a va 2 s .c o m } return method; }
From source file:net.daboross.bukkitdev.skywars.util.CrossVersion.java
/** * Supports Bukkit earlier than 1.6. TODO: removable? *//*from ww w .j a va2 s .c om*/ public static void setHealth(Damageable d, double health) { Validate.notNull(d, "Damageable cannot be null"); try { d.setHealth(health); } catch (NoSuchMethodError ignored) { Class<? extends Damageable> dClass = d.getClass(); try { Method healthMethod = dClass.getMethod("setHealth", Integer.TYPE); healthMethod.invoke(d, (int) health); } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { SkyStatic.getLogger().log(Level.WARNING, "Couldn't find / use .setHealth method of LivingEntity!", ex); } } }
From source file:de.micromata.genome.gwiki.utils.ClassUtils.java
@SuppressWarnings("unchecked") public static <T> T invokeDefaultStaticMethod(Class<T> expectedClass, String className, String methodName) { Class<?> cls = classForName(className); try {/*from ww w . ja v a2 s . c om*/ Method m = cls.getMethod(methodName, new Class[] {}); T t = (T) m.invoke(null, new Object[] {}); if (t == null) { return t; } if (expectedClass.isAssignableFrom(t.getClass()) == false) { throw new RuntimeException("method: " + methodName + " from class: " + className + " does not return correct class type: " + expectedClass + " but: " + t.getClass()); } return t; } catch (NoSuchMethodException ex) { throw new RuntimeException("Cannot find method: " + methodName + " in class: " + className); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } catch (InvocationTargetException ex) { Throwable nex = ex.getCause(); if (nex instanceof RuntimeException) { throw (RuntimeException) nex; } throw new RuntimeException(ex); } }
From source file:jin.collection.util.PropertyUtil.java
public static boolean hasGetter(final String property, final Class type) { final String methodName = "get" + capitalize(property); try {/*w w w. ja v a 2 s . c o m*/ final Method m = type.getMethod(methodName, null); } catch (final NoSuchMethodException e) { return false; } return true; }
From source file:com.microsoft.tfs.client.common.ui.helpers.WorkingSetHelper.java
public static void addToWorkingSet(final IProject project, final IWorkingSet workingSet) { try {//from ww w .j a v a 2 s . c om boolean aggregateWorkingSetAvailable = false; try { aggregateWorkingSetAvailable = (Class.forName("org.eclipse.ui.IAggregateWorkingSet") != null); //$NON-NLS-1$ } catch (final Exception e) { /* Suppress */ } if (aggregateWorkingSetAvailable && isAggregateWorkingSet(workingSet)) { try { final Class aggregateClass = Class.forName("org.eclipse.ui.IAggregateWorkingSet"); //$NON-NLS-1$ final Method getComponentsMethod = aggregateClass.getMethod("getComponents", (Class[]) null); //$NON-NLS-1$ final IWorkingSet[] components = (IWorkingSet[]) getComponentsMethod.invoke(workingSet, (Object[]) null); for (int i = 0; i < components.length; i++) { addToWorkingSet(project, components[i]); } return; } catch (final Throwable t) { final String messageFormat = "Could not add {0} to working set {1}"; //$NON-NLS-1$ final String message = MessageFormat.format(messageFormat, project.getName(), WorkingSetHelper.getLabel(workingSet)); log.error(message, t); } } final IAdaptable[] setMembers = workingSet.getElements(); final IAdaptable[] newMembers = new IAdaptable[setMembers.length + 1]; System.arraycopy(setMembers, 0, newMembers, 0, setMembers.length); newMembers[setMembers.length] = project; workingSet.setElements(newMembers); } catch (final Exception e) { final String messageFormat = "Could not add {0} to working set {1}"; //$NON-NLS-1$ final String message = MessageFormat.format(messageFormat, project.getName(), getLabel(workingSet)); log.error(message, e); } }