List of usage examples for java.lang.reflect Modifier isStatic
public static boolean isStatic(int mod)
From source file:com.baasbox.configuration.PropertiesConfigurationHelper.java
public static String dumpConfiguration(String section) { Class en = CONFIGURATION_SECTIONS.get(section); try {//w w w .j av a 2s .c o m StringBuilder sb = new StringBuilder(); String enumDescription = ""; Method getEnumDescription = en.getMethod("getEnumDescription"); if (getEnumDescription != null && getEnumDescription.getReturnType() == String.class && Modifier.isStatic(getEnumDescription.getModifiers())) enumDescription = (String) getEnumDescription.invoke(null); sb.append(enumDescription); sb.append("\n"); sb.append(section.toUpperCase()); sb.append("\n"); String lastSection = ""; EnumSet values = EnumSet.allOf(en); for (Object v : values) { String key = (String) ((Method) v.getClass().getMethod("getKey")).invoke(v); Object value = ((Method) en.getMethod("getValue")).invoke(v); String subsection = key.substring(0, key.indexOf('.')); if (!lastSection.equals(subsection)) { sb.append(" - "); sb.append(subsection.toUpperCase()); sb.append("\n"); lastSection = subsection; } sb.append(" + "); sb.append(key); sb.append(" = "); sb.append(value); sb.append("\n"); } return sb.toString(); } catch (Exception e) { BaasBoxLogger.error("Cannot generate a json for " + en.getSimpleName() + " Enum. Is it an Enum that implements the IProperties interface?", e); } return ""; }
From source file:com.github.jknack.handlebars.helper.DefaultHelperRegistry.java
/** * <p>//from w w w.j av a2 s. c o m * Register all the helper methods for the given helper source. * </p> * * @param source The helper source. * @param clazz The helper source class. */ private void registerDynamicHelper(final Object source, final Class<?> clazz) { int size = helpers.size(); int replaced = 0; if (clazz != Object.class) { Set<String> overloaded = new HashSet<String>(); // Keep backing up the inheritance hierarchy. Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { boolean isPublic = Modifier.isPublic(method.getModifiers()); String helperName = method.getName(); if (isPublic && CharSequence.class.isAssignableFrom(method.getReturnType())) { boolean isStatic = Modifier.isStatic(method.getModifiers()); if (source != null || isStatic) { if (helpers.containsKey(helperName)) { replaced++; } isTrue(overloaded.add(helperName), "name conflict found: " + helperName); registerHelper(helperName, new MethodHelper(method, source)); } } } } isTrue((size + replaced) != helpers.size(), "No helper method was found in: " + clazz.getName()); }
From source file:cz.cuni.mff.d3s.tools.perfdoc.server.measuring.codegen.CodeGenerator.java
private void makeAndCompileGeneratorCode(BenchmarkSetting setting) throws CompileException, IOException { MethodReflectionInfo mrInfo = (MethodReflectionInfo) setting.getWorkload(); Method generator = mrInfo.getMethod(); VelocityContext context = new VelocityContext(); context.put("gFunction", generator); context.put("gFunctionIsStatic", Modifier.isStatic(generator.getModifiers())); context.put("gClass", mrInfo.getContainingClass().getName()); //TODO if enum - need to prefix with full name + dot context.put("gParameterType", generator.getParameterTypes()); context.put("gArgument", setting.getWorkloadArguments().getValues()); writeCode(context, templateGeneratorName); String javaSourceName = javaDestinationDir + directoryName + "/" + templateGeneratorName + ".java"; String javaClassDirectory = compiledClassDestinationDir + directoryName; List<String> classPaths = getCompilationClassPaths(); classPaths.add(javaClassDirectory);/*from ww w . j a v a 2s.c o m*/ Compiler.compile(javaSourceName, classPaths); }
From source file:org.apache.dubbo.config.spring.beans.factory.annotation.CompatibleReferenceAnnotationBeanPostProcessor.java
/** * Finds {@link InjectionMetadata.InjectedElement} Metadata from annotated {@link Reference @Reference} methods * * @param beanClass The {@link Class} of Bean * @return non-null {@link List}//from w ww . j a v a 2 s . co m */ private List<ReferenceMethodElement> findMethodReferenceMetadata(final Class<?> beanClass) { final List<ReferenceMethodElement> elements = new LinkedList<ReferenceMethodElement>(); ReflectionUtils.doWithMethods(beanClass, new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Method bridgedMethod = findBridgedMethod(method); if (!isVisibilityBridgeMethodPair(method, bridgedMethod)) { return; } Reference reference = findAnnotation(bridgedMethod, Reference.class); if (reference != null && method.equals(ClassUtils.getMostSpecificMethod(method, beanClass))) { if (Modifier.isStatic(method.getModifiers())) { if (logger.isWarnEnabled()) { logger.warn("@Reference annotation is not supported on static methods: " + method); } return; } if (method.getParameterTypes().length == 0) { if (logger.isWarnEnabled()) { logger.warn("@Reference annotation should only be used on methods with parameters: " + method); } } PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, beanClass); elements.add(new ReferenceMethodElement(method, pd, reference)); } } }); return elements; }
From source file:com.eurelis.opencms.workflows.workflows.toolobjects.WorkflowPropertyContainerElement.java
/** * Generate a XML Representation of the given object. * /* w w w .j av a2 s .com*/ * @return the xml corresponding to this class */ public String convertInXML() { StringBuffer xmlString = new StringBuffer(1024); // get the class Class<WorkflowPropertyContainerElement> thisClass = (Class<WorkflowPropertyContainerElement>) this .getClass(); // get the fields Field[] fields = thisClass.getDeclaredFields(); // open tag with name of the class xmlString.append("<" + this.getClass().getSimpleName()); xmlString.append(" "); /* * Add all fields in a XML (this class has only simple tag, so use * only attribute) */ for (int i = 0; i < fields.length; i++) { // get modifier int modifierValue = fields[i].getModifiers(); // don't write static variable if (!Modifier.isStatic(modifierValue)) { // get field name String fieldName = fields[i].getName(); // don't write if name startWith "this" if (!fieldName.startsWith("this")) { // remove "_" before variable if required if (fieldName.startsWith("_")) { xmlString.append(fieldName.substring(1)); } else { xmlString.append(fieldName); } xmlString.append("=\""); try { xmlString.append(fields[i].get(this).toString()); } catch (IllegalArgumentException e) { LOGGER.info(ErrorFormatter.formatException(e)); } catch (IllegalAccessException e) { LOGGER.info(ErrorFormatter.formatException(e)); } xmlString.append("\" "); } } } // close tag xmlString.append("/>"); return xmlString.toString(); }
From source file:org.apache.hadoop.hbase.util.ClassSize.java
/** * The estimate of the size of a class instance depends on whether the JVM * uses 32 or 64 bit addresses, that is it depends on the size of an object * reference. It is a linear function of the size of a reference, e.g. * 24 + 5*r where r is the size of a reference (usually 4 or 8 bytes). * * This method returns the coefficients of the linear function, e.g. {24, 5} * in the above example.//from w ww.ja v a 2s .co m * * @param cl A class whose instance size is to be estimated * @param debug debug flag * @return an array of 3 integers. The first integer is the size of the * primitives, the second the number of arrays and the third the number of * references. */ @SuppressWarnings("unchecked") private static int[] getSizeCoefficients(Class cl, boolean debug) { int primitives = 0; int arrays = 0; //The number of references that a new object takes int references = nrOfRefsPerObj; int index = 0; for (; null != cl; cl = cl.getSuperclass()) { Field[] field = cl.getDeclaredFields(); if (null != field) { for (Field aField : field) { if (Modifier.isStatic(aField.getModifiers())) continue; Class fieldClass = aField.getType(); if (fieldClass.isArray()) { arrays++; references++; } else if (!fieldClass.isPrimitive()) { references++; } else {// Is simple primitive String name = fieldClass.getName(); if (name.equals("int") || name.equals("I")) primitives += Bytes.SIZEOF_INT; else if (name.equals("long") || name.equals("J")) primitives += Bytes.SIZEOF_LONG; else if (name.equals("boolean") || name.equals("Z")) primitives += Bytes.SIZEOF_BOOLEAN; else if (name.equals("short") || name.equals("S")) primitives += Bytes.SIZEOF_SHORT; else if (name.equals("byte") || name.equals("B")) primitives += Bytes.SIZEOF_BYTE; else if (name.equals("char") || name.equals("C")) primitives += Bytes.SIZEOF_CHAR; else if (name.equals("float") || name.equals("F")) primitives += Bytes.SIZEOF_FLOAT; else if (name.equals("double") || name.equals("D")) primitives += Bytes.SIZEOF_DOUBLE; } if (debug) { if (LOG.isDebugEnabled()) { LOG.debug("" + index + " " + aField.getName() + " " + aField.getType()); } } index++; } } } return new int[] { primitives, arrays, references }; }
From source file:com.ery.ertc.estorm.util.ClassSize.java
/** * The estimate of the size of a class instance depends on whether the JVM uses 32 or 64 bit addresses, that is it depends on the size * of an object reference. It is a linear function of the size of a reference, e.g. 24 + 5*r where r is the size of a reference (usually * 4 or 8 bytes).//from w w w .j a va 2 s .c o m * * This method returns the coefficients of the linear function, e.g. {24, 5} in the above example. * * @param cl * A class whose instance size is to be estimated * @param debug * debug flag * @return an array of 3 integers. The first integer is the size of the primitives, the second the number of arrays and the third the * number of references. */ @SuppressWarnings("unchecked") private static int[] getSizeCoefficients(Class cl, boolean debug) { int primitives = 0; int arrays = 0; // The number of references that a new object takes int references = nrOfRefsPerObj; int index = 0; for (; null != cl; cl = cl.getSuperclass()) { Field[] field = cl.getDeclaredFields(); if (null != field) { for (Field aField : field) { if (Modifier.isStatic(aField.getModifiers())) continue; Class fieldClass = aField.getType(); if (fieldClass.isArray()) { arrays++; references++; } else if (!fieldClass.isPrimitive()) { references++; } else {// Is simple primitive String name = fieldClass.getName(); if (name.equals("int") || name.equals("I")) primitives += Bytes.SIZEOF_INT; else if (name.equals("long") || name.equals("J")) primitives += Bytes.SIZEOF_LONG; else if (name.equals("boolean") || name.equals("Z")) primitives += Bytes.SIZEOF_BOOLEAN; else if (name.equals("short") || name.equals("S")) primitives += Bytes.SIZEOF_SHORT; else if (name.equals("byte") || name.equals("B")) primitives += Bytes.SIZEOF_BYTE; else if (name.equals("char") || name.equals("C")) primitives += Bytes.SIZEOF_CHAR; else if (name.equals("float") || name.equals("F")) primitives += Bytes.SIZEOF_FLOAT; else if (name.equals("double") || name.equals("D")) primitives += Bytes.SIZEOF_DOUBLE; } if (debug) { if (LOG.isDebugEnabled()) { LOG.debug("" + index + " " + aField.getName() + " " + aField.getType()); } } index++; } } } return new int[] { primitives, arrays, references }; }
From source file:com.rosenvold.spring.SpringContextAnalyzer.java
private boolean isStatic(Field field) { return Modifier.isStatic(field.getModifiers()); }
From source file:com.feilong.core.lang.reflect.FieldUtil.java
/** * <code>klass</code> ? <code>excludeFieldNames</code> ?list. * * @param klass//from w w w.j a v a2 s .co m * the klass * @param excludeFieldNames * ?field names,?nullOrEmpty ? * @return <code>obj</code> null, {@link NullPointerException}<br> * {@link FieldUtils#getAllFieldsList(Class)} nullempty, {@link Collections#emptyList()}<br> * @see FieldUtils#getAllFieldsList(Class) * @since 1.7.1 */ //no static public static List<Field> getAllFieldList(final Class<?> klass, String... excludeFieldNames) { // {@link Field},parents, public/protect/private/inherited... List<Field> fieldList = FieldUtils.getAllFieldsList(klass); if (isNullOrEmpty(fieldList)) { return Collections.emptyList(); } //********************************************************************************************** Predicate<Field> excludeFieldPredicate = BeanPredicateUtil.containsPredicate("name", excludeFieldNames); Predicate<Field> staticPredicate = new Predicate<Field>() { @Override public boolean evaluate(Field field) { int modifiers = field.getModifiers(); // ??? log serialVersionUID boolean isStatic = Modifier.isStatic(modifiers); String pattern = "[{}.{}],modifiers:[{}]{}"; LOGGER.trace(pattern, klass.getSimpleName(), field.getName(), modifiers, isStatic ? " [isStatic]" : EMPTY); return isStatic; } }; return CollectionsUtil.selectRejected(fieldList, PredicateUtils.orPredicate(excludeFieldPredicate, staticPredicate)); }
From source file:com.netflix.bdp.inviso.history.TraceJobHistoryLoader.java
private void handleAttemptEvent(HistoryEvent event) throws IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { TaskAttempt attempt = new TaskAttempt(); for (Field f : event.getDatum().getClass().getFields()) { f.setAccessible(true);//from w ww .ja v a2s .co m if (Modifier.isStatic(f.getModifiers())) { continue; } String name = f.getName(); Object value = f.get(event.getDatum()); if (skipElements.contains(name)) { continue; } if (value instanceof CharSequence) { value = value.toString(); } if (value == null || value.toString().trim().isEmpty()) { continue; } if ("counters".equals(name)) { Method m = event.getClass().getDeclaredMethod("getCounters", new Class[0]); m.setAccessible(true); Counters counters = (Counters) m.invoke(event, new Object[0]); value = handleCounterEntries(counters); } attempt.put(name, value); } Task task = job.getTask("taskid"); task.getAttempt((String) attempt.get("attemptId")).merge(attempt); }