List of usage examples for java.lang.reflect Modifier isPublic
public static boolean isPublic(int mod)
From source file:Main.java
public static Method getGetter(Object bean, String property) { Map<String, Method> cache = GETTER_CACHE.get(bean.getClass()); if (cache == null) { cache = new ConcurrentHashMap<String, Method>(); for (Method method : bean.getClass().getMethods()) { if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()) && !void.class.equals(method.getReturnType()) && method.getParameterTypes().length == 0) { String name = method.getName(); if (name.length() > 3 && name.startsWith("get")) { cache.put(name.substring(3, 4).toLowerCase() + name.substring(4), method); } else if (name.length() > 2 && name.startsWith("is")) { cache.put(name.substring(2, 3).toLowerCase() + name.substring(3), method); }/*from w ww . j a v a2s. co m*/ } } Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache); if (old != null) { cache = old; } } return cache.get(property); }
From source file:com.hihframework.core.utils.BeanUtils.java
/** * ?????/* w ww . ja v a 2 s .c o m*/ * @param bean * @param name * @return * @author * @since */ public static String getProperty(Object bean, String name) { try { Method m = bean.getClass().getMethod("get" + StringHelpers.upperFirst(name)); if (m != null && Modifier.isPublic(m.getModifiers())) { Object val = m.invoke(bean); return val == null ? null : val.toString(); } } catch (Exception e) { } try { Method[] methods = bean.getClass().getDeclaredMethods(); for (Method method : methods) { String n = method.getName(); if (!n.startsWith("get")) continue; n = n.substring(3); if (!name.equalsIgnoreCase(n)) continue; if (!Modifier.isPublic(method.getModifiers())) continue; Object val = method.invoke(bean); return val == null ? null : val.toString(); } } catch (Exception e) { } return null; }
From source file:Main.java
/** * set method is accessible// w w w . ja va 2s.c om * * @param method {@link java.lang.reflect.Method} */ public static void makeAccessible(final Method method) { if (!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) { method.setAccessible(true); } }
From source file:com.curl.orb.security.RemoteServiceAnnotationChecker.java
/** * Check the PublicService annotation. Throw AccessException if false. * //from w w w . j av a 2 s .c om * @param cls the class * @throws AccessException */ public static void check(Class<?> cls, Environment environment) throws AccessException { // ignore security if (environment == null) return; RemoteService remoteServiceAnnotation = (RemoteService) cls.getAnnotation(RemoteService.class); if (!Modifier.isPublic(cls.getModifiers()) || remoteServiceAnnotation == null || !environment.contain(remoteServiceAnnotation.value())) { Log log = LogFactory.getLog(RemoteServiceAnnotationChecker.class); log.debug("Cannot allow to access the class [" + cls.getName() + "]"); throw new AccessException("Cannot allow to access the class [" + cls.getName() + "]"); } // TODO: Cache the class(cls). Which is faster, cache or annotation? }
From source file:Main.java
/** * find the setter method and set the value. *//*from w ww .j a v a 2 s. c o m*/ public static void setProperties(Object bean, Map<String, Object> properties) { for (Method method : bean.getClass().getMethods()) { String name = method.getName(); if (name.length() > 3 && name.startsWith("set") && Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 1 && method.getDeclaringClass() != Object.class) { String key = name.substring(3, 4).toLowerCase() + name.substring(4); try { Object value = properties.get(key); if (value != null) { method.invoke(bean, new Object[] { convertCompatibleType(value, method.getParameterTypes()[0]) }); } } catch (Exception e) { } } } }
From source file:ReflectionUtils.java
/** * Determine whether the given field is a "public static final" constant. * @param field the field to check/*www .j a v a 2 s.c o m*/ */ public static boolean isPublicStaticFinal(Field field) { int modifiers = field.getModifiers(); return (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)); }
From source file:Main.java
/** * search the method and return the defined method. * it will {@link Class#getMethod(String, Class[])}, if exception occurs, * it will search for all methods, and find the most fit method. *///w ww. j ava 2 s .co m public static Method searchMethod(Class<?> currentClass, String name, Class<?>[] parameterTypes, boolean boxed) throws NoSuchMethodException { if (currentClass == null) { throw new NoSuchMethodException("class == null"); } try { return currentClass.getMethod(name, parameterTypes); } catch (NoSuchMethodException e) { Method likeMethod = null; for (Method method : currentClass.getMethods()) { if (method.getName().equals(name) && parameterTypes.length == method.getParameterTypes().length && Modifier.isPublic(method.getModifiers())) { if (parameterTypes.length > 0) { Class<?>[] types = method.getParameterTypes(); boolean eq = true; boolean like = true; for (int i = 0; i < parameterTypes.length; i++) { Class<?> type = types[i]; Class<?> parameterType = parameterTypes[i]; if (type != null && parameterType != null && !type.equals(parameterType)) { eq = false; if (boxed) { type = getBoxedClass(type); parameterType = getBoxedClass(parameterType); } if (!type.isAssignableFrom(parameterType)) { eq = false; like = false; break; } } } if (!eq) { if (like && (likeMethod == null || likeMethod.getParameterTypes()[0] .isAssignableFrom(method.getParameterTypes()[0]))) { likeMethod = method; } continue; } } return method; } } if (likeMethod != null) { return likeMethod; } throw e; } }
From source file:Main.java
/** * get the method start with 'get' or 'is'. *///from w w w. jav a 2 s . c om public static Method getGetter(Object bean, String property) { Map<String, Method> cache = GETTER_CACHE.get(bean.getClass()); if (cache == null) { cache = new ConcurrentHashMap<>(); for (Method method : bean.getClass().getMethods()) { if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()) && !void.class.equals(method.getReturnType()) && method.getParameterTypes().length == 0) { String name = method.getName(); if (name.length() > 3 && name.startsWith("get")) { cache.put(name.substring(3, 4).toLowerCase() + name.substring(4), method); } else if (name.length() > 2 && name.startsWith("is")) { cache.put(name.substring(2, 3).toLowerCase() + name.substring(3), method); } } } Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache); if (old != null) { cache = old; } } return cache.get(property); }
From source file:api.Status.java
public static Result getStatus() { ObjectNode metrics = Json.newObject(); OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean(); TreeMap<String, Object> values = new TreeMap<String, Object>(); for (Method method : os.getClass().getDeclaredMethods()) { method.setAccessible(true);// ww w. ja va 2 s .c o m if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) { Object value; try { value = method.invoke(os); values.put(method.getName(), value); } catch (Exception e) { Logger.warn("Error when invoking " + os.getClass().getName() + " (OperatingSystemMXBean) method " + method.getName() + ": " + e); } // try } // if } // for metrics.put("jvmLoad", (Double) values.get("getProcessCpuLoad")); metrics.put("cpuLoad", (Double) values.get("getSystemCpuLoad")); metrics.put("openFD", (Long) values.get("getOpenFileDescriptorCount")); metrics.put("maxFD", (Long) values.get("getMaxFileDescriptorCount")); metrics.put("freeMemory", (Long) values.get("getFreePhysicalMemorySize")); metrics.put("totalMemory", (Long) values.get("getTotalPhysicalMemorySize")); return ok(metrics.toString()); }
From source file:ReflectionUtils.java
/** * Make the given field accessible, explicitly setting it accessible if necessary. * The <code>setAccessible(true)</code> method is only called when actually necessary, * to avoid unnecessary conflicts with a JVM SecurityManager (if active). * @param field the field to make accessible * @see java.lang.reflect.Field#setAccessible *//* w w w . j a v a 2 s .c o m*/ public static void makeAccessible(Field field) { if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) { field.setAccessible(true); } }