List of usage examples for java.lang Class getDeclaredMethods
@CallerSensitive public Method[] getDeclaredMethods() throws SecurityException
From source file:Main.java
public static void setProperties(Object object, Map<String, ? extends Object> properties, boolean includeSuperClasses) { if (object == null || properties == null) { return;//from w w w . j av a 2 s . co m } Class<?> objectClass = object.getClass(); for (Map.Entry<String, ? extends Object> entry : properties.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (key != null && key.length() > 0) { String setterName = "set" + Character.toUpperCase(key.charAt(0)) + key.substring(1); Method setter = null; // Try to use the exact setter if (value != null) { try { if (includeSuperClasses) { setter = objectClass.getMethod(setterName, value.getClass()); } else { setter = objectClass.getDeclaredMethod(setterName, value.getClass()); } } catch (Exception ex) { } } // Find a more generic setter if (setter == null) { Method[] methods = includeSuperClasses ? objectClass.getMethods() : objectClass.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equals(setterName)) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 1 && isAssignableFrom(parameterTypes[0], value)) { setter = method; break; } } } } // Invoke if (setter != null) { try { setter.invoke(object, value); } catch (Exception e) { } } } } }
From source file:com.hubspot.utils.circuitbreaker.CircuitBreakerWrapper.java
/** * Wraps the supplied object toWrap in a CircuitBreaker conforming to the supplied CircuitBreakerPolicy. *///www. j av a 2 s .c om public <T, W extends T> T wrap(W toWrap, Class<T> interfaceToProxy, CircuitBreakerPolicy policy) throws CircuitBreakerWrappingException { sanityCheck(toWrap, interfaceToProxy, policy); // walk the chain of interfaces implemented by T and check for their blacklisted methods Stack<Class<?>> implementedInterfaces = new Stack<Class<?>>(); implementedInterfaces.addAll(Arrays.asList(interfaceToProxy.getInterfaces())); implementedInterfaces.add(interfaceToProxy); Map<Method, Class[]> blacklist = new HashMap(); while (!implementedInterfaces.isEmpty()) { Class<?> implementedInterface = implementedInterfaces.pop(); for (Method m : implementedInterface.getDeclaredMethods()) { // check that the blacklisted method throws CircuitBreakerException if (m.isAnnotationPresent(CircuitBreakerExceptionBlacklist.class)) { if (!ArrayUtils.contains(m.getExceptionTypes(), CircuitBreakerException.class)) { throw new CircuitBreakerWrappingException( "Wrapped methods must throw CircuitBreakerException"); } CircuitBreakerExceptionBlacklist a = (CircuitBreakerExceptionBlacklist) m .getAnnotation(CircuitBreakerExceptionBlacklist.class); blacklist.put(m, a.blacklist()); } } implementedInterfaces.addAll(Arrays.asList(implementedInterface.getInterfaces())); } Class<?>[] interfaces = new Class<?>[] { interfaceToProxy }; InvocationHandler handler = new CircuitBreakerInvocationHandler(toWrap, blacklist, policy); T newProxyInstance = (T) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, handler); return newProxyInstance; }
From source file:com.medsphere.fileman.FMRecord.java
static private void accumulateJavaDomainFields(Map<String, AnnotatedElement> domainFields, Class<? extends FMRecord> domainClass) { if (domainClass != null && domainClass.getSuperclass() != null) { Class<? extends FMRecord> superClass = (Class<? extends FMRecord>) domainClass.getSuperclass(); accumulateJavaDomainFields(domainFields, superClass); }//from w w w.jav a 2 s. com for (AnnotatedElement e : domainClass.getDeclaredFields()) { FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class); if (annote != null) { domainFields.put(annote.number(), e); } } for (AnnotatedElement e : domainClass.getDeclaredMethods()) { FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class); if (annote != null) { domainFields.put(annote.number(), e); } } }
From source file:com.glaf.core.util.ReflectUtils.java
private static Method findMethod(Class<? extends Object> clazz, String methodName, Object[] args) { for (Method method : clazz.getDeclaredMethods()) { if (method.getName().equals(methodName) && matches(method.getParameterTypes(), args)) { return method; }/*from w w w . j a va 2 s . c o m*/ } Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { return findMethod(superClass, methodName, args); } return null; }
From source file:org.apache.hadoop.metrics2.lib.MutableRates.java
/** * Initialize the registry with all the methods in a protocol * so they all show up in the first snapshot. * Convenient for JMX implementations.//w w w.j a v a 2s . c o m * @param protocol the protocol class */ public void init(Class<?> protocol) { if (protocolCache.contains(protocol)) return; protocolCache.add(protocol); for (Method method : protocol.getDeclaredMethods()) { String name = method.getName(); LOG.debug(name); try { registry.newRate(name, name, false, true); } catch (Exception e) { LOG.error("Error creating rate metrics for " + method.getName(), e); } } }
From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java
/** * Gets the declared members of the given type from the specified class. * * @author paouelle//from w w w . jav a 2 s .c o m * * @param <T> the type of member * * @param type the type of members to retrieve * @param clazz the class from which to retrieve the members * @return the non-<code>null</code> members of the given type from the * specified class * @throws NullPointerException if <code>type</code> or * <code>clazz</code> is <code>null</code> * @throws IllegalArgumentException if <code>type</code> is not * {@link Field}, {@link Method}, or {@link Constructor} */ @SuppressWarnings("unchecked") public static <T extends Member> T[] getDeclaredMembers(Class<T> type, Class<?> clazz) { org.apache.commons.lang3.Validate.notNull(type, "invalid null member type"); org.apache.commons.lang3.Validate.notNull(clazz, "invalid null class"); if (type == Field.class) { return (T[]) clazz.getDeclaredFields(); } else if (type == Method.class) { return (T[]) clazz.getDeclaredMethods(); } else if (type == Constructor.class) { return (T[]) clazz.getDeclaredConstructors(); } else { throw new IllegalArgumentException("invalid member class: " + type.getName()); } }
From source file:com.medsphere.fileman.FMRecord.java
private static void accumulateNumericMapping(Map<String, String> numberMap, Class<? extends FMRecord> domainClass) { if (domainClass != null && domainClass.getSuperclass() != null) { Class<? extends FMRecord> superClass = (Class<? extends FMRecord>) domainClass.getSuperclass(); accumulateNumericMapping(numberMap, superClass); }/* w ww. ja va 2s.c o m*/ for (AnnotatedElement e : domainClass.getDeclaredFields()) { FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class); if (annote != null) { numberMap.put(annote.name(), annote.number()); } } for (AnnotatedElement e : domainClass.getDeclaredMethods()) { FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class); if (annote != null) { numberMap.put(annote.name(), annote.number()); } } }
From source file:org.apache.hadoop.hbase.thrift.ThriftMetrics.java
private void createMetricsForMethods(Class<?> iface) { LOG.debug("Creating metrics for interface " + iface.toString()); for (Method m : iface.getDeclaredMethods()) { if (getMethodTimeMetrics(m.getName()) == null) LOG.debug("Creating metrics for method:" + m.getName()); createMethodTimeMetrics(m.getName()); }/*from w w w. ja v a 2s. c o m*/ }
From source file:org.apache.aries.blueprint.plugin.model.Bean.java
public Bean(Class<?> clazz) { this.clazz = clazz; this.id = getBeanName(clazz); for (Method method : clazz.getDeclaredMethods()) { PostConstruct postConstruct = method.getAnnotation(PostConstruct.class); if (postConstruct != null) { this.initMethod = method.getName(); }//from w ww . ja v a2 s. co m PreDestroy preDestroy = method.getAnnotation(PreDestroy.class); if (preDestroy != null) { this.destroyMethod = method.getName(); } } this.persistenceUnitField = getPersistenceUnit(); this.transactionDef = new JavaxTransactionFactory().create(clazz); if (this.transactionDef == null) { this.transactionDef = new SpringTransactionFactory().create(clazz); } properties = new TreeSet<Property>(); }
From source file:net.sf.beanlib.provider.BeanChecker.java
/** * Returns true if the fromBean is empty; or false otherwise. * It is considered empty if every publicly declared getter methods of the specified class * either return null or blank string.//ww w . jav a2 s .c o m * * @param fromBean java bean to check if empty or not. * @param k specified class name from which the getter methods are derived. */ public boolean empty(Object fromBean, Class<?> k) { Class<?> fromClass = fromBean.getClass(); Method[] ma = k.getDeclaredMethods(); try { // invoking all declaring getter methods of fromBean for (int i = 0; i < ma.length; i++) { Method m = ma[i]; String name = m.getName(); try { if (name.startsWith("get") && Modifier.isPublic(m.getModifiers())) { Method getter = fromClass.getMethod(name); Object attrValue = getter.invoke(fromBean); if (attrValue == null) { continue; } if (attrValue instanceof String) { String s = (String) attrValue; s = s.trim(); if (s.length() == 0) { continue; } } return false; } } catch (NoSuchMethodException ignore) { // ignore and proceed to the next method. } } return true; } catch (IllegalAccessException e) { log.error("", e); throw new BeanlibException(e); } catch (SecurityException e) { log.error("", e); throw new BeanlibException(e); } catch (InvocationTargetException e) { log.error("", e.getTargetException()); throw new BeanlibException(e.getTargetException()); } }