List of usage examples for java.lang.reflect Modifier isStatic
public static boolean isStatic(int mod)
From source file:com.amazonaws.services.simpleworkflow.flow.common.WorkflowExecutionUtils.java
/** * Not really a generic method for printing random object graphs. But it * works for events and decisions./* ww w.ja v a2s.c om*/ */ private static String prettyPrintObject(Object object, String methodToSkip, boolean skipNullsAndEmptyCollections, String indentation, boolean skipLevel) { StringBuffer result = new StringBuffer(); if (object == null) { return "null"; } Class<? extends Object> clz = object.getClass(); if (Number.class.isAssignableFrom(clz)) { return String.valueOf(object); } if (Boolean.class.isAssignableFrom(clz)) { return String.valueOf(object); } if (clz.equals(String.class)) { return (String) object; } if (clz.equals(Date.class)) { return String.valueOf(object); } if (Map.class.isAssignableFrom(clz)) { return String.valueOf(object); } if (Collection.class.isAssignableFrom(clz)) { return String.valueOf(object); } if (!skipLevel) { result.append(" {"); } Method[] eventMethods = object.getClass().getMethods(); boolean first = true; for (Method method : eventMethods) { String name = method.getName(); if (!name.startsWith("get")) { continue; } if (name.equals(methodToSkip) || name.equals("getClass")) { continue; } if (Modifier.isStatic(method.getModifiers())) { continue; } Object value; try { value = method.invoke(object, (Object[]) null); if (value != null && value.getClass().equals(String.class) && name.equals("getDetails")) { value = printDetails((String) value); } } catch (InvocationTargetException e) { throw new RuntimeException(e.getTargetException()); } catch (RuntimeException e) { throw (RuntimeException) e; } catch (Exception e) { throw new RuntimeException(e); } if (skipNullsAndEmptyCollections) { if (value == null) { continue; } if (value instanceof Map && ((Map<?, ?>) value).isEmpty()) { continue; } if (value instanceof Collection && ((Collection<?>) value).isEmpty()) { continue; } } if (!skipLevel) { if (first) { first = false; } else { result.append(";"); } result.append("\n"); result.append(indentation); result.append(" "); result.append(name.substring(3)); result.append(" = "); result.append(prettyPrintObject(value, methodToSkip, skipNullsAndEmptyCollections, indentation + " ", false)); } else { result.append( prettyPrintObject(value, methodToSkip, skipNullsAndEmptyCollections, indentation, false)); } } if (!skipLevel) { result.append("\n"); result.append(indentation); result.append("}"); } return result.toString(); }
From source file:adalid.core.Project.java
/** * * @param clazz/*w w w. ja v a 2 s.c o m*/ */ public void attachAddAttributesMethods(Class<?> clazz) { logger.debug(signature("attachAddAttributesMethods", clazz)); String name; boolean addAttributesMethod; int modifiers; Class<?> returnType; Class<?>[] parameterTypes; Method[] methods = clazz.getDeclaredMethods(); List<Method> list = Arrays.asList(methods); Comparator<Method> comparator = new ByMethodSequence(); ColUtils.sort(list, comparator); for (Method method : list) { name = method.getName(); addAttributesMethod = method.isAnnotationPresent(AddAttributesMethod.class); modifiers = method.getModifiers(); returnType = method.getReturnType(); parameterTypes = method.getParameterTypes(); if (addAttributesMethod && Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && void.class.equals(returnType) && parameterTypes.length == 1 && Artifact.class.isAssignableFrom(parameterTypes[0])) { logger.debug(signature(clazz.getSimpleName() + "." + name, parameterTypes[0])); _addAttributesMethods.add(method); } } }
From source file:adalid.core.AbstractArtifact.java
private boolean isNotRestricted(Field field) { int modifiers = field.getModifiers(); return !(Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)); }
From source file:com.github.helenusdriver.driver.impl.FieldInfoImpl.java
/** * Finds the getter method from the declaring class suitable to get a * reference to the field./*w w w . j av a 2s .com*/ * * @author paouelle * * @param declaringClass the non-<code>null</code> class declaring the field * @param prefix the non-<code>null</code> getter prefix to use * @return the getter method for the field or <code>null</code> if none found * @throws IllegalArgumentException if unable to find a suitable getter */ private Method findGetterMethod(Class<?> declaringClass, String prefix) { final String mname = prefix + WordUtils.capitalize(name, '_', '-'); try { final Method m = declaringClass.getDeclaredMethod(mname); final int mods = m.getModifiers(); if (Modifier.isAbstract(mods) || Modifier.isStatic(mods)) { return null; } final Class<?> wtype = ClassUtils.primitiveToWrapper(type); final Class<?> wrtype = ClassUtils.primitiveToWrapper( DataTypeImpl.unwrapOptionalIfPresent(m.getReturnType(), m.getGenericReturnType())); org.apache.commons.lang3.Validate.isTrue(wtype.isAssignableFrom(wrtype), "expecting getter for field '%s' with return type: %s", field, type.getName()); m.setAccessible(true); return m; } catch (NoSuchMethodException e) { return null; } }
From source file:grails.util.GrailsClassUtils.java
/** * Check whether the specified method is a property getter * * @param method The method/*from ww w .j ava 2s .com*/ * @return true if the method is a property getter */ public static boolean isPropertyGetter(Method method) { return !Modifier.isStatic(method.getModifiers()) && Modifier.isPublic(method.getModifiers()) && GrailsNameUtils.isGetter(method.getName(), method.getReturnType(), method.getParameterTypes()); }
From source file:com.p5solutions.core.utils.ReflectionUtility.java
/** * Checks if is static./*from w w w . j av a 2 s . com*/ * * @param field * the field * @return true, if is static */ public static boolean isStatic(Field field) { int modifiers = field.getModifiers(); return Modifier.isStatic(modifiers); }
From source file:org.soyatec.windowsazure.table.internal.CloudTableRest.java
/** * Deserial the xml to object accord to the give model class. * * @param partitionKey/*from w ww . j a va2s . c o m*/ * @param rowKey * @param eTag * @param timestamp * @param values * @return Instance of the given model class. */ private ITableServiceEntity createObjectByModelClass(String partitionKey, String rowKey, String eTag, String timestamp, List<ICloudTableColumn> values) { ITableServiceEntity newInstance = instanceModel(partitionKey, rowKey, eTag, timestamp, values); if (newInstance == null) { return null; } // Copy Field if (values != null && !values.isEmpty()) { for (ICloudTableColumn column : values) { Field field = null; try { field = newInstance.getClass().getDeclaredField(column.getName()); } catch (NoSuchFieldException e) { continue; } if (field == null) { continue; } int modifier = field.getModifiers(); if (Modifier.isPrivate(modifier) && Modifier.isFinal(modifier) && Modifier.isStatic(modifier)) { if (getModelClass() != null) { Logger.debug(MessageFormat.format( "{0} class {1} is a static final field. Can not set data to it.", getModelClass().getClass(), field.getName())); } continue; } boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } ETableColumnType type = column.getType(); String value = column.getValue(); if (value == null) { continue; } else { try { if (type == null) { setStringOrObjectField(newInstance, column, field, value); } else if (type.equals(ETableColumnType.TYPE_BINARY)) { field.set(newInstance, Base64.decode(value)); } else if (type.equals(ETableColumnType.TYPE_BOOL)) { field.setBoolean(newInstance, Boolean.parseBoolean(value)); } else if (type.equals(ETableColumnType.TYPE_DATE_TIME)) { field.set(newInstance, Utilities.tryGetDateTimeFromTableEntry(value)); } else if (type.equals(ETableColumnType.TYPE_DOUBLE)) { field.setDouble(newInstance, Double.parseDouble(value)); } else if (type.equals(ETableColumnType.TYPE_GUID)) { Guid guid = new Guid(); try { Field valueField = guid.getClass().getDeclaredField("value"); boolean accessiable = valueField.isAccessible(); if (!accessible) { valueField.setAccessible(true); } valueField.set(guid, value); valueField.setAccessible(accessiable); field.set(newInstance, guid); } catch (NoSuchFieldException e) { Logger.error(e.getMessage(), e); } } else if (type.equals(ETableColumnType.TYPE_INT)) { try { field.setInt(newInstance, Integer.parseInt(value)); } catch (Exception e) { field.setByte(newInstance, Byte.parseByte(value)); } } else if (type.equals(ETableColumnType.TYPE_LONG)) { field.setLong(newInstance, Long.parseLong(value)); } else if (type.equals(ETableColumnType.TYPE_STRING)) { setStringOrObjectField(newInstance, column, field, value); } } catch (Exception e) { Logger.error( MessageFormat.format("{0} class filed {1} set failed.", getModelClass(), value), e); } } // revert aaccessible field.setAccessible(accessible); } } return newInstance; }
From source file:cat.ereza.customactivityoncrash.CustomActivityOnCrash.java
/** * Sets an event listener to be called when events occur, so they can be reported * by the app as, for example, Google Analytics events. * If not set or set to null, no events will be reported. * * @param eventListener The event listener. * @throws IllegalArgumentException if the eventListener is an inner or anonymous class *///ww w . j a v a2 s. c o m public static void setEventListener(EventListener eventListener) { if (eventListener != null && eventListener.getClass().getEnclosingClass() != null && !Modifier.isStatic(eventListener.getClass().getModifiers())) { throw new IllegalArgumentException( "The event listener cannot be an inner or anonymous class, because it will need to be serialized. Change it to a class of its own, or make it a static inner class."); } else { CustomActivityOnCrash.eventListener = eventListener; } }
From source file:com.github.helenusdriver.driver.impl.FieldInfoImpl.java
/** * Finds the setter method from the declaring class suitable to set a * value for the field./* w ww . j a v a 2s . c o m*/ * * @author paouelle * * @param declaringClass the non-<code>null</code> class declaring the field * @return the setter method for the field or <code>null</code> if none found * @throws IllegalArgumentException if unable to find a suitable setter */ private Method findSetterMethod(Class<?> declaringClass) { final String mname = "set" + WordUtils.capitalize(name, '_', '-'); try { final Method m = declaringClass.getDeclaredMethod(mname, type); final int mods = m.getModifiers(); if (Modifier.isAbstract(mods) || Modifier.isStatic(mods)) { return null; } org.apache.commons.lang3.Validate.isTrue(m.getParameterCount() == 1, "expecting setter for field '%s' with one parameter", field); final Class<?> wtype = ClassUtils.primitiveToWrapper(type); final Class<?> wptype = ClassUtils.primitiveToWrapper(DataTypeImpl.unwrapOptionalIfPresent( m.getParameterTypes()[0], m.getParameters()[0].getParameterizedType())); org.apache.commons.lang3.Validate.isTrue(wtype.isAssignableFrom(wptype), "expecting setter for field '%s' with parameter type: %s", field, type.getName()); m.setAccessible(true); return m; } catch (NoSuchMethodException e) { return null; } }
From source file:edu.cmu.tetrad.util.TetradSerializableUtils.java
/** * @return a reference to the public static serializableInstance() method of * clazz, if there is one; otherwise, returns null. *///from ww w .j a va 2 s . c o m private Method serializableInstanceMethod(Class clazz) { Method[] methods = clazz.getMethods(); for (Method method : methods) { if ("serializableInstance".equals(method.getName())) { Class[] parameterTypes = method.getParameterTypes(); if (!(parameterTypes.length == 0)) { continue; } if (!(Modifier.isStatic(method.getModifiers()))) { continue; } if (Modifier.isAbstract(method.getModifiers())) { continue; } return method; } } return null; }