List of usage examples for java.lang Class getMethod
@CallerSensitive public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:ReflectUtils.java
private static synchronized Method findMethod(Object obj, String property, Object value) { Method m = null;/*from w ww. jav a2s. c o m*/ Class<?> theClass = obj.getClass(); String setter = String.format("set%C%s", property.charAt(0), property.substring(1)); Class paramType = value.getClass(); while (paramType != null) { try { m = theClass.getMethod(setter, paramType); return m; } catch (NoSuchMethodException ex) { // try on the interfaces of this class for (Class iface : paramType.getInterfaces()) { try { m = theClass.getMethod(setter, iface); return m; } catch (NoSuchMethodException ex1) { } } paramType = paramType.getSuperclass(); } } return m; }
From source file:com.github.riking.dropcontrol.ItemStringInterpreter.java
public static BasicItemMatcher valueOf(String itemString) throws IllegalArgumentException { itemString = itemString.toUpperCase(); if (itemString.equals("ANY")) { return new BasicItemMatcher(null, null, null); }/*from ww w. j a va 2 s . com*/ String[] split = itemString.split(":"); Validate.isTrue(split.length <= 2, "Unable to parse item string - too many colons (maximum 1). Please correct the format and reload the config. Input: " + itemString); Material mat = getMaterial(split[0]); Validate.notNull(mat, "Unable to parse item string - unrecognized material. Please correct the format and reload the config. Input: " + itemString); if (split.length == 1) { return new BasicItemMatcher(mat, null, null); } String dataString = split[1]; short data; try { data = Short.parseShort(dataString); return new BasicItemMatcher(mat, data, null); // the datastring is not passed if it was just a number } catch (NumberFormatException ignored) { } if (materialData.containsKey(mat.getData())) { Class<? extends MaterialData> matClass = mat.getData(); Class<? extends Enum> enumClass = materialData.get(mat.getData()); Enum enumValue; try { enumValue = (Enum) enumClass.getMethod("valueOf", String.class).invoke(null, dataString); } catch (InvocationTargetException e) { throw new IllegalArgumentException("Unable to parse item string - " + dataString + " is not a valid member of " + enumClass.getSimpleName(), e.getCause()); } catch (Exception rethrow) { throw new RuntimeException("Unexpected exception when parsing item string", rethrow); } MaterialData matData; try { matData = matClass.getConstructor(enumClass).newInstance(enumValue); } catch (Exception rethrow) { throw new RuntimeException("Unexpected exception when parsing item string", rethrow); } data = (short) matData.getData(); return new BasicItemMatcher(mat, data, dataString); } if (workarounds.containsKey(mat)) { StringInterpreter interp = workarounds.get(mat); data = interp.interpret(dataString); return new BasicItemMatcher(mat, data, dataString); } throw new IllegalArgumentException( "Unable to parse item string - I don't know how to parse a word-data for " + mat); }
From source file:com.palantir.ptoss.util.Reflections.java
/** * Looks up an {@link Enum} value by its {@link String} name. * @param enumType {@link Enum} class to query. * @param value {@link String} name for the {@link Enum} value. * @return the actual {@link Enum} value specified by the passed name. * @see Enum#valueOf(Class, String)// w ww.j a v a2 s.c o m */ public static Object evalEnum(Class<?> enumType, String value) { try { Method method = enumType.getMethod("valueOf", String.class); method.setAccessible(true); return method.invoke(null, value); } catch (Exception ew) { throw new IllegalArgumentException("could not find enum value: " + value); } }
From source file:com.baasbox.configuration.PropertiesConfigurationHelper.java
public static void override(String completeKey, Object value) throws ConfigurationException { Object enumValue = findByKey(completeKey); try {/* w ww . j a va 2 s. c o m*/ String[] splittedKeys = completeKey.split("\\."); String section = splittedKeys[0]; Class en = PropertiesConfigurationHelper.CONFIGURATION_SECTIONS.get(section); en.getMethod("override", Object.class).invoke(enumValue, value); } catch (Exception e) { throw new ConfigurationException("Invalid key -" + completeKey + "- or value -" + value + "-", e); } }
From source file:hm.binkley.util.XPropsConverter.java
@SuppressWarnings("unchecked") private static <T, E extends Exception> Conversion<T, E> invokeValueOf(final Class<T> token) throws NoSuchMethodError { try {//from w w w .j ava 2 s . com final Method method = token.getMethod("valueOf", String.class); return value -> { try { return (T) method.invoke(null, value); } catch (final IllegalAccessException e) { final IllegalAccessError x = new IllegalAccessError(e.getMessage()); x.setStackTrace(e.getStackTrace()); throw x; } catch (final InvocationTargetException e) { final Throwable root = getRootCause(e); final RuntimeException x = new RuntimeException(root); x.setStackTrace(root.getStackTrace()); throw x; } }; } catch (final NoSuchMethodException ignored) { return null; } }
From source file:hm.binkley.util.XPropsConverter.java
@SuppressWarnings("unchecked") private static <T, E extends Exception> Conversion<T, E> invokeOf(final Class<T> token) throws NoSuchMethodError { try {/*w ww . j a va2 s. co m*/ final Method method = token.getMethod("of", String.class); return value -> { try { return (T) method.invoke(null, value); } catch (final IllegalAccessException e) { final IllegalAccessError x = new IllegalAccessError(e.getMessage()); x.setStackTrace(e.getStackTrace()); throw x; } catch (final InvocationTargetException e) { final Throwable root = getRootCause(e); final RuntimeException x = new RuntimeException(root); x.setStackTrace(root.getStackTrace()); throw x; } }; } catch (final NoSuchMethodException ignored) { return null; } }
From source file:com.baasbox.configuration.PropertiesConfigurationHelper.java
public static void setVisible(String completeKey, Boolean value) throws ConfigurationException { Object enumValue = findByKey(completeKey); try {/*from www . j av a 2 s . c o m*/ String[] splittedKeys = completeKey.split("\\."); String section = splittedKeys[0]; Class en = PropertiesConfigurationHelper.CONFIGURATION_SECTIONS.get(section); en.getMethod("setVisible", boolean.class).invoke(enumValue, value); } catch (Exception e) { BaasBoxLogger.error("Invalid key -" + completeKey + "- or value -" + value + "-", e); throw new ConfigurationException("Invalid key -" + completeKey + "- or value -" + value + "-", e); } }
From source file:com.baasbox.configuration.PropertiesConfigurationHelper.java
public static void setEditable(String completeKey, Boolean value) throws ConfigurationException { Object enumValue = findByKey(completeKey); try {/* w ww .j a v a2s .c o m*/ String[] splittedKeys = completeKey.split("\\."); String section = splittedKeys[0]; Class en = PropertiesConfigurationHelper.CONFIGURATION_SECTIONS.get(section); en.getMethod("setEditable", boolean.class).invoke(enumValue, value); } catch (Exception e) { BaasBoxLogger.error("Invalid key -" + completeKey + "- or value -" + value + "-", e); throw new ConfigurationException("Invalid key -" + completeKey + "- or value -" + value + "-", e); } }
From source file:Main.java
/** * Returns attribute's getter method. If the method not found then * NoSuchMethodException will be thrown. * // w ww . j a v a 2s . co m * @param cls * the class the attribute belongs too * @param attr * the attribute's name * @return attribute's getter method * @throws NoSuchMethodException * if the getter was not found */ public final static Method getAttributeGetter(Class cls, String attr) throws NoSuchMethodException { StringBuffer buf = new StringBuffer(attr.length() + 3); buf.append("get"); if (Character.isLowerCase(attr.charAt(0))) { buf.append(Character.toUpperCase(attr.charAt(0))).append(attr.substring(1)); } else { buf.append(attr); } try { return cls.getMethod(buf.toString(), (Class[]) null); } catch (NoSuchMethodException e) { buf.replace(0, 3, "is"); return cls.getMethod(buf.toString(), (Class[]) null); } }
From source file:io.openmessaging.rocketmq.utils.BeanUtils.java
public static void setProperties(Class<?> clazz, Object obj, String methodName, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Class<?> parameterClass = getMethodClass(clazz, methodName); Method setterMethod = clazz.getMethod(methodName, parameterClass); if (parameterClass == Boolean.TYPE) { setterMethod.invoke(obj, Boolean.valueOf(value.toString())); } else if (parameterClass == Integer.TYPE) { setterMethod.invoke(obj, Integer.valueOf(value.toString())); } else if (parameterClass == Double.TYPE) { setterMethod.invoke(obj, Double.valueOf(value.toString())); } else if (parameterClass == Float.TYPE) { setterMethod.invoke(obj, Float.valueOf(value.toString())); } else if (parameterClass == Long.TYPE) { setterMethod.invoke(obj, Long.valueOf(value.toString())); } else//from ww w .ja va 2 s . c o m setterMethod.invoke(obj, value); }