List of usage examples for java.lang.reflect Method getModifiers
@Override public int getModifiers()
From source file:Main.java
public static Map<String, Object> getProperties(Object bean) { Map<String, Object> map = new HashMap<String, Object>(); for (Method method : bean.getClass().getMethods()) { String name = method.getName(); if ((name.length() > 3 && name.startsWith("get") || name.length() > 2 && name.startsWith("is")) && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0 && method.getDeclaringClass() != Object.class) { int i = name.startsWith("get") ? 3 : 2; String key = name.substring(i, i + 1).toLowerCase() + name.substring(i + 1); try { map.put(key, method.invoke(bean, new Object[0])); } catch (Exception e) { }/*from w ww.ja va 2 s.c o m*/ } } return map; }
From source file:com.hihframework.core.utils.BeanUtils.java
public static void bean2Bean(Object src, Object dest, String... excludes) { if (src == null || dest == null || src == dest) return;/*from w w w. j ava 2 s . c o m*/ Method[] methods = src.getClass().getDeclaredMethods(); for (Method m : methods) { String name = m.getName(); if (!Modifier.isPublic(m.getModifiers())) continue; if (!name.startsWith("get") && !name.startsWith("is")) continue; if (Collection.class.isAssignableFrom(m.getReturnType())) continue; boolean exc = false; for (String exclude : excludes) { if (name.equalsIgnoreCase(exclude)) { exc = true; break; } } if (exc) continue; int position = name.startsWith("get") ? 3 : 2; String method = name.substring(position); try { Object val = m.invoke(src); if (val == null) continue; Method targetFun = dest.getClass().getMethod("set" + method, m.getReturnType()); targetFun.invoke(dest, val); } catch (Exception e) { log.error(e); } } }
From source file:org.braiden.fpm2.util.PropertyUtils.java
private static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) { Method result = null; try {// www . j av a 2s . c o m result = clazz.getMethod(methodName, params); if (result != null && !Modifier.isPublic(result.getModifiers())) { result = null; } } catch (NoSuchMethodException e) { } return result; }
From source file:org.apache.brooklyn.util.javalang.MethodAccessibleReflections.java
private static Maybe<Method> tryFindAccessibleMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) { if (!isAccessible(clazz)) { return Maybe.absent(); }/* w w w. j a v a 2 s . com*/ try { Method altMethod = clazz.getMethod(methodName, parameterTypes); if (isAccessible(altMethod) && !Modifier.isStatic(altMethod.getModifiers())) { return Maybe.of(altMethod); } } catch (NoSuchMethodException | SecurityException e) { // Not found; swallow, and return absent } return Maybe.absent(); }
From source file:blue.lapis.pore.impl.event.PoreEventImplTest.java
private static boolean isDefault(Method method) { // Default methods are public non-abstract instance methods declared in an interface. return ((method.getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) == Modifier.PUBLIC) && method.getDeclaringClass().isInterface(); }
From source file:lite.flow.util.ActivityInspector.java
/** * Component can have multiple Entry methods. * @param clazz//from w w w.j av a 2s . c om * @return */ static public ArrayList<Method> getEntryMethods(Class<?> clazz) { ArrayList<Method> methods = new ArrayList<>(); for (Method method : clazz.getDeclaredMethods()) { if (Modifier.isPublic(method.getModifiers())) { methods.add(method); } } return methods; }
From source file:com.qrmedia.commons.persistence.hibernate.clone.HibernateEntityBeanCloner.java
private static Collection<String> getPropertyNames(Collection<Method> methods) { Collection<String> methodNames = new ArrayList<String>(); /*/* w ww . j a va 2 s . c o m*/ * If a method is an instance method, does not return void, takes no parameters * and is named "get..." (it's assumed to be public), add the corresponding field name. */ for (Method method : methods) { assert Modifier.isPublic(method.getModifiers()) : method; String methodName = method.getName(); Matcher getterNameMatcher = GETTER_PREFIX.matcher(methodName); if (!Modifier.isStatic(method.getModifiers()) && (method.getReturnType() != Void.class) && (method.getParameterTypes().length == 0) && getterNameMatcher.matches()) { // the first group is the (uppercase) first letter of the field name methodNames.add(getterNameMatcher.replaceFirst(getterNameMatcher.group(1).toLowerCase() + "$2")); } } return methodNames; }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType.java
@SuppressWarnings("unchecked") public static boolean supports(@SuppressWarnings("rawtypes") Class enumClass) { if (!isEnabled()) return false; if (enumClass.isEnum()) { try {/* www. jav a2 s . co m*/ Method idAccessor = enumClass.getMethod(ENUM_ID_ACCESSOR); int mods = idAccessor.getModifiers(); if (Modifier.isPublic(mods) && !Modifier.isStatic(mods)) { Class<?> returnType = idAccessor.getReturnType(); return returnType != null && typeResolver.basic(returnType.getName()) instanceof AbstractStandardBasicType; } } catch (NoSuchMethodException e) { // ignore } } return false; }
From source file:com.xhsoft.framework.common.utils.ReflectUtil.java
/** * <p>Description:setFieldValue</p> * @param target/*from ww w .ja va2 s . co m*/ * @param fname * @param ftype * @param fvalue * @return void */ @SuppressWarnings("unchecked") public static void setFieldValue(Object target, String fname, Class ftype, Object fvalue) { if (target == null || fname == null || "".equals(fname) || (fvalue != null && !ftype.isAssignableFrom(fvalue.getClass()))) { return; } Class clazz = target.getClass(); try { Method method = clazz .getDeclaredMethod("set" + Character.toUpperCase(fname.charAt(0)) + fname.substring(1), ftype); if (!Modifier.isPublic(method.getModifiers())) { method.setAccessible(true); } method.invoke(target, fvalue); } catch (Exception me) { try { Field field = clazz.getDeclaredField(fname); if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } field.set(target, fvalue); } catch (Exception fe) { if (logger.isDebugEnabled()) { logger.debug(fe); } } } }
From source file:org.eclipse.wb.android.internal.model.util.AndroidListenerProperties.java
/** * @return the {@link List} of events which are supported by given widget. *//*from w w w.j a va 2 s .c o m*/ private static List<ListenerInfo> getWidgetEvents(XmlObjectInfo widget) { Class<?> componentClass = widget.getDescription().getComponentClass(); List<ListenerInfo> events = m_widgetEvents.get(componentClass); if (events == null) { GenericTypeResolver typeResolver = new GenericTypeResolver(null); events = Lists.newArrayList(); m_widgetEvents.put(componentClass, events); { // events in Android has 'OnXXXListener' inner class as listener interface with // appropriate 'setOnXXXListener' method in main class Pattern pattern = Pattern.compile("On.*Listener"); Class<?>[] classes = componentClass.getClasses(); if (!ArrayUtils.isEmpty(classes)) { for (Class<?> innerClass : classes) { String shortClassName = CodeUtils.getShortClass(innerClass.getName()); Matcher matcher = pattern.matcher(shortClassName); if (matcher.matches()) { // additionally check for method, it should be public (RefUtils returns with any visibility) Method method = ReflectionUtils.getMethod(componentClass, "set" + shortClassName, innerClass); if (method != null && (method.getModifiers() & Modifier.PUBLIC) != 0) { ListenerInfo listener = new ListenerInfo(method, componentClass, typeResolver); events.add(listener); } } } } } // sort Collections.sort(events, new Comparator<ListenerInfo>() { public int compare(ListenerInfo o1, ListenerInfo o2) { return o1.getSimpleName().compareTo(o2.getSimpleName()); } }); } return events; }