List of usage examples for java.lang.reflect Modifier isStatic
public static boolean isStatic(int mod)
From source file:com.datos.vfs.util.DelegatingFileSystemOptionsBuilder.java
/** * tries to convert the value and pass it to the given method *///from www. j ava 2s . co m private boolean convertValuesAndInvoke(final Method configSetter, final Context ctx) throws FileSystemException { final Class<?>[] parameters = configSetter.getParameterTypes(); if (parameters.length < 2) { return false; } if (!parameters[0].isAssignableFrom(FileSystemOptions.class)) { return false; } final Class<?> valueParameter = parameters[1]; Class<?> type; if (valueParameter.isArray()) { type = valueParameter.getComponentType(); } else { if (ctx.values.length > 1) { return false; } type = valueParameter; } if (type.isPrimitive()) { final Class<?> objectType = PRIMATIVE_TO_OBJECT.get(type.getName()); if (objectType == null) { log.warn(Messages.getString("vfs.provider/config-unexpected-primitive.error", type.getName())); return false; } type = objectType; } final Class<? extends Object> valueClass = ctx.values[0].getClass(); if (type.isAssignableFrom(valueClass)) { // can set value directly invokeSetter(valueParameter, ctx, configSetter, ctx.values); return true; } if (valueClass != String.class) { log.warn(Messages.getString("vfs.provider/config-unexpected-value-class.error", valueClass.getName(), ctx.scheme, ctx.name)); return false; } final Object convertedValues = Array.newInstance(type, ctx.values.length); Constructor<?> valueConstructor; try { valueConstructor = type.getConstructor(STRING_PARAM); } catch (final NoSuchMethodException e) { valueConstructor = null; } if (valueConstructor != null) { // can convert using constructor for (int iterValues = 0; iterValues < ctx.values.length; iterValues++) { try { Array.set(convertedValues, iterValues, valueConstructor.newInstance(new Object[] { ctx.values[iterValues] })); } catch (final InstantiationException e) { throw new FileSystemException(e); } catch (final IllegalAccessException e) { throw new FileSystemException(e); } catch (final InvocationTargetException e) { throw new FileSystemException(e); } } invokeSetter(valueParameter, ctx, configSetter, convertedValues); return true; } Method valueFactory; try { valueFactory = type.getMethod("valueOf", STRING_PARAM); if (!Modifier.isStatic(valueFactory.getModifiers())) { valueFactory = null; } } catch (final NoSuchMethodException e) { valueFactory = null; } if (valueFactory != null) { // can convert using factory method (valueOf) for (int iterValues = 0; iterValues < ctx.values.length; iterValues++) { try { Array.set(convertedValues, iterValues, valueFactory.invoke(null, new Object[] { ctx.values[iterValues] })); } catch (final IllegalAccessException e) { throw new FileSystemException(e); } catch (final InvocationTargetException e) { throw new FileSystemException(e); } } invokeSetter(valueParameter, ctx, configSetter, convertedValues); return true; } return false; }
From source file:Utilities.java
/** Initialization of the names and values * @return array of two hashmaps first maps * allowed key names to their values (String, Integer) * and second// ww w . ja va 2s. co m * hashtable for mapping of values to their names (Integer, String) */ private static synchronized HashMap[] initNameAndValues() { if (namesAndValues != null) { HashMap[] arr = (HashMap[]) namesAndValues.get(); if (arr != null) { return arr; } } Field[] fields; // JW - fix Issue #353-swingx: play nicer inside sandbox. try { fields = KeyEvent.class.getDeclaredFields(); // fields = KeyEvent.class.getFields(); } catch (SecurityException e) { // JW: need to do better? What are the use-cases where we don't have // any access to the fields? fields = new Field[0]; } HashMap<String, Integer> names = new HashMap<String, Integer>(((fields.length * 4) / 3) + 5, 0.75f); HashMap<Integer, String> values = new HashMap<Integer, String>(((fields.length * 4) / 3) + 5, 0.75f); for (int i = 0; i < fields.length; i++) { if (Modifier.isStatic(fields[i].getModifiers())) { String name = fields[i].getName(); if (name.startsWith("VK_")) { // NOI18N // exclude VK name = name.substring(3); try { int numb = fields[i].getInt(null); Integer value = new Integer(numb); names.put(name, value); values.put(value, name); } catch (IllegalArgumentException ex) { } catch (IllegalAccessException ex) { } } } } if (names.get("CONTEXT_MENU") == null) { // NOI18N Integer n = new Integer(0x20C); names.put("CONTEXT_MENU", n); // NOI18N values.put(n, "CONTEXT_MENU"); // NOI18N n = new Integer(0x20D); names.put("WINDOWS", n); // NOI18N values.put(n, "WINDOWS"); // NOI18N } HashMap[] arr = { names, values }; namesAndValues = new SoftReference<Object>(arr); return arr; }
From source file:com.opengamma.financial.analytics.curve.CurveNodeIdMapper.java
/** * Gets all fields used by the Fudge builder. * @return The fields/*from w w w. j a v a 2 s . com*/ */ protected static List<String> getCurveIdMapperNames() { final List<String> list = new ArrayList<>(); for (final Field field : CurveNodeIdMapperBuilder.class.getDeclaredFields()) { if (Modifier.isStatic(field.getModifiers()) && field.isSynthetic() == false) { field.setAccessible(true); try { list.add((String) field.get(null)); } catch (final Exception ex) { // Ignore } } } Collections.sort(list, String.CASE_INSENSITIVE_ORDER); return ImmutableList.copyOf(list); }
From source file:org.batoo.common.reflect.ReflectHelper.java
/** * Returns the property descriptors for the class. * /*from w w w . j av a 2 s.c o m*/ * @param clazz * the class * @return the property descriptors * * @since 2.0.1 */ public static PropertyDescriptor[] getProperties(Class<?> clazz) { final List<PropertyDescriptor> properties = Lists.newArrayList(); final Method[] methodList = clazz.getDeclaredMethods(); // check each method. for (final Method method : methodList) { if (method == null) { continue; } // skip static and private methods. final int mods = method.getModifiers(); if (Modifier.isStatic(mods) || !Modifier.isPublic(mods) || method.isBridge() || method.isSynthetic()) { continue; } final String name = method.getName(); if (method.getParameterTypes().length == 0) { if (name.startsWith(ReflectHelper.GET_PREFIX)) { properties.add(new PropertyDescriptor(clazz, name.substring(3), method)); } else if ((method.getReturnType() == boolean.class) && name.startsWith(ReflectHelper.IS_PREFIX)) { properties.add(new PropertyDescriptor(clazz, name.substring(2), method)); } } } return properties.toArray(new PropertyDescriptor[properties.size()]); }
From source file:com.dpbymqn.fsm.manager.FsmManager.java
private void onDecision(final Method m, final OnDecision a, Class<? extends StatefulObject> stObjectClz, Class<? extends StateListener> clzLst, StateListener sl, StatefulObject st) { if (!testDeclaredAndActualClassConformity(m, a.smClass(), st, sl, stObjectClz, clzLst)) { return;//from ww w. j a va2 s . c o m } final Class<? extends StatefulObject> stcls = StatefulObject.class.equals(a.smClass()) ? stObjectClz : a.smClass(); final String prev = StringUtils.isEmpty(a.prev()) ? null : a.prev(); final String next = StringUtils.isEmpty(a.next()) ? null : a.next(); final WeakReference<StateListener> slRef = new WeakReference<StateListener>(sl); if (String.class.equals(m.getReturnType())) { if (m.getParameterTypes() != null) { switch (m.getParameterTypes().length) { case 1: if (String.class.equals(m.getParameterTypes()[0])) { regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return null; } @Override public String query(StatefulObject sm, String fromState) { return (String) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), fromState); } }); } else if (StatefulObject.class.isAssignableFrom(m.getParameterTypes()[0])) { regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return null; } @Override public String query(StatefulObject sm, String fromState) { return (String) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), sm); } }); } break; case 2: regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return null; } @Override public String query(StatefulObject sm, String fromState) { return (String) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), sm, fromState); } }); break; } } else { regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return null; } @Override public String query(StatefulObject sm, String fromState) { return (String) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get()); } }); } } else if (Boolean.class.equals(m.getReturnType()) || boolean.class.equals(m.getReturnType())) { if (m.getParameterTypes() != null) { switch (m.getParameterTypes().length) { case 1: if (String.class.equals(m.getParameterTypes()[0])) { regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return (Boolean) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), toState); } @Override public String query(StatefulObject sm, String fromState) { return null; } }); } else if (StatefulObject.class.isAssignableFrom(m.getParameterTypes()[0])) { regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return (Boolean) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), sm); } @Override public String query(StatefulObject sm, String fromState) { return null; } }); } break; case 2: if (String.class.equals(m.getParameterTypes()[0])) { regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return (Boolean) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), fromState, toState); } @Override public String query(StatefulObject sm, String fromState) { return null; } }); } else if (StatefulObject.class.isAssignableFrom(m.getParameterTypes()[0])) { regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return (Boolean) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), sm, toState); } @Override public String query(StatefulObject sm, String fromState) { return null; } }); } break; case 3: regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return (Boolean) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get(), sm, fromState, toState); } @Override public String query(StatefulObject sm, String fromState) { return null; } }); break; default: regDecisionCbk(stcls, st, sl, prev, next, new DecisionCallback() { @Override public Boolean query(StatefulObject sm, String fromState, String toState) { return (Boolean) invoke(m, Modifier.isStatic(m.getModifiers()) ? null : slRef.get()); } @Override public String query(StatefulObject sm, String fromState) { return null; } }); } } } }
From source file:ca.uhn.fhir.jaxrs.server.AbstractJaxRsConformanceProvider.java
/** * This method will add a provider to the conformance. This method is almost an exact copy of {@link ca.uhn.fhir.rest.server.RestfulServer#findResourceMethods } * //from w w w . j a v a 2 s . co m * @param theProvider * an instance of the provider interface * @param theProviderInterface * the class describing the providers interface * @return the numbers of basemethodbindings added * @see ca.uhn.fhir.rest.server.RestfulServer#findResourceMethods */ public int addProvider(IResourceProvider theProvider, Class<? extends IResourceProvider> theProviderInterface) throws ConfigurationException { int count = 0; for (Method m : ReflectionUtil.getDeclaredMethods(theProviderInterface)) { BaseMethodBinding<?> foundMethodBinding = BaseMethodBinding.bindMethod(m, getFhirContext(), theProvider); if (foundMethodBinding == null) { continue; } count++; // if (foundMethodBinding instanceof ConformanceMethodBinding) { // myServerConformanceMethod = foundMethodBinding; // continue; // } if (!Modifier.isPublic(m.getModifiers())) { throw new ConfigurationException( "Method '" + m.getName() + "' is not public, FHIR RESTful methods must be public"); } else { if (Modifier.isStatic(m.getModifiers())) { throw new ConfigurationException( "Method '" + m.getName() + "' is static, FHIR RESTful methods must not be static"); } else { ourLog.debug("Scanning public method: {}#{}", theProvider.getClass(), m.getName()); String resourceName = foundMethodBinding.getResourceName(); ResourceBinding resourceBinding; if (resourceName == null) { resourceBinding = myServerBinding; } else { RuntimeResourceDefinition definition = getFhirContext().getResourceDefinition(resourceName); if (myResourceNameToBinding.containsKey(definition.getName())) { resourceBinding = myResourceNameToBinding.get(definition.getName()); } else { resourceBinding = new ResourceBinding(); resourceBinding.setResourceName(resourceName); myResourceNameToBinding.put(resourceName, resourceBinding); } } List<Class<?>> allowableParams = foundMethodBinding.getAllowableParamAnnotations(); if (allowableParams != null) { for (Annotation[] nextParamAnnotations : m.getParameterAnnotations()) { for (Annotation annotation : nextParamAnnotations) { Package pack = annotation.annotationType().getPackage(); if (pack.equals(IdParam.class.getPackage())) { if (!allowableParams.contains(annotation.annotationType())) { throw new ConfigurationException("Method[" + m.toString() + "] is not allowed to have a parameter annotated with " + annotation); } } } } } resourceBinding.addMethod(foundMethodBinding); ourLog.debug(" * Method: {}#{} is a handler", theProvider.getClass(), m.getName()); } } } return count; }
From source file:com.alibaba.rocketmq.common.MixAll.java
/** * ??Properties/* ww w . j a v a 2 s.co m*/ */ public static Properties object2Properties(final Object object) { Properties properties = new Properties(); Field[] fields = object.getClass().getDeclaredFields(); for (Field field : fields) { if (!Modifier.isStatic(field.getModifiers())) { String name = field.getName(); if (!name.startsWith("this")) { Object value = null; try { field.setAccessible(true); value = field.get(object); } catch (IllegalArgumentException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); } if (value != null) { properties.setProperty(name, value.toString()); } } } } return properties; }
From source file:gov.nih.nci.iso21090.hibernate.tuple.IsoConstantTuplizerHelper.java
private Boolean setNullFlavor(Object parent) { Class klass = parent.getClass(); Boolean allNullFlavors = true; if (Any.class.isAssignableFrom(klass)) { if (((Any) parent).getNullFlavor() == null) { try { while (klass != null && klass != Object.class) { for (Field field : klass.getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers())) { field.setAccessible(true); Object value = field.get(parent); if (value == null) { setNullFlavor(parent, field.getName()); } else { if (Any.class.isAssignableFrom(value.getClass())) { if (!setNullFlavor(value)) allNullFlavors = false; if (((Any) value).getNullFlavor() != null) allNullFlavors = false; } else { allNullFlavors = false; }// ww w . j a v a 2 s .com } } } klass = klass.getSuperclass(); } } catch (SecurityException e) { throw new HibernateException(e); } catch (IllegalAccessException e) { throw new HibernateException(e); } catch (IllegalArgumentException e) { throw new HibernateException(e); } if (allNullFlavors) ((Any) parent).setNullFlavor(NullFlavor.NI); } } return allNullFlavors; }
From source file:ml.shifu.shifu.container.meta.MetaFactory.java
/** * Iterate each property of Object, get the value and validate * /*w w w .j a va 2 s. c o m*/ * @param isGridSearch * - if grid search, ignore validation in train#params as they are set all as list * @param ptag * - the prefix of key to search @MetaItem * @param obj * - the object to validate * @return ValidateResult * If all items are OK, the ValidateResult.status will be true; * Or the ValidateResult.status will be false, ValidateResult.causes will contain the reasons * @throws Exception * any exception in validaiton */ public static ValidateResult iterateCheck(boolean isGridSearch, String ptag, Object obj) throws Exception { ValidateResult result = new ValidateResult(true); if (obj == null) { return result; } Class<?> cls = obj.getClass(); Field[] fields = cls.getDeclaredFields(); Class<?> parentCls = cls.getSuperclass(); if (!parentCls.equals(Object.class)) { Field[] pfs = parentCls.getDeclaredFields(); fields = (Field[]) ArrayUtils.addAll(fields, pfs); } for (Field field : fields) { if (!field.isSynthetic() && !Modifier.isStatic(field.getModifiers()) && !isJsonIngoreField(field)) { Method method = cls.getMethod("get" + getMethodName(field.getName())); Object value = method.invoke(obj); encapsulateResult(result, validate(isGridSearch, ptag + ITEM_KEY_SEPERATOR + field.getName(), value)); } } return result; }
From source file:com.searchbox.collection.oppfin.EENCollection.java
private static void getFieldValues(Object target, String path, FieldMap fields) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IntrospectionException { if (JAXBElement.class.isAssignableFrom(target.getClass())) { target = ((JAXBElement) target).getValue(); }//from ww w .ja v a2 s . com if (Date.class.isAssignableFrom(target.getClass())) { LOGGER.debug("put Date:" + target); fields.put(path, target); } else if (Calendar.class.isAssignableFrom(target.getClass())) { fields.put(path, ((Calendar) target).getTime()); } else if (XMLGregorianCalendar.class.isAssignableFrom(target.getClass())) { fields.put(path, ((XMLGregorianCalendar) target).toGregorianCalendar().getTime()); } else if (!target.getClass().isArray() && target.getClass().getName().startsWith("java.")) { if (!target.toString().isEmpty()) { fields.put(path, target.toString()); } } else { for (java.lang.reflect.Field field : target.getClass().getDeclaredFields()) { if (field.getName().startsWith("_") || Modifier.isStatic(field.getModifiers())) { continue; } field.setAccessible(true); Method reader = new PropertyDescriptor(field.getName(), target.getClass()).getReadMethod(); try { if (reader != null) { Object obj = reader.invoke(target); if (field.getType().isArray()) { for (Object object : (Object[]) obj) { getFieldValues(object, path + WordUtils.capitalize(field.getName().toLowerCase()), fields); } } else if (java.util.Collection.class.isAssignableFrom(obj.getClass())) { for (Object object : (java.util.Collection) obj) { getFieldValues(object, path + WordUtils.capitalize(field.getName().toLowerCase()), fields); } } else { getFieldValues(obj, path + WordUtils.capitalize(field.getName().toLowerCase()), fields); } } } catch (Exception e) { ; } } } }