List of usage examples for java.lang.reflect Modifier TRANSIENT
int TRANSIENT
To view the source code for java.lang.reflect Modifier TRANSIENT.
Click Source Link
From source file:org.kuali.rice.krad.uif.util.CloneUtils.java
public static Field[] getFields(Class<?> c, boolean includeStatic, boolean includeTransient) { String cacheKey = c.getName() + ":" + includeStatic; Field[] array = fieldCache.get(cacheKey); if (array == null) { ArrayList<Field> fields = new ArrayList<Field>(); List<Class<?>> classes = getClassHierarchy(c, false); // Reverse order so we make sure we maintain consistent order Collections.reverse(classes); for (Class<?> clazz : classes) { Field[] allFields = clazz.getDeclaredFields(); for (Field f : allFields) { if ((!includeTransient) && ((f.getModifiers() & Modifier.TRANSIENT) == Modifier.TRANSIENT)) { continue; } else if (f.isSynthetic()) { // Synthetic fields are bad!!! continue; }//from w w w .j a v a2 s. com boolean isStatic = (f.getModifiers() & Modifier.STATIC) == Modifier.STATIC; if ((isStatic) && (!includeStatic)) { continue; } if (f.getName().equalsIgnoreCase("serialVersionUID")) { continue; } f.setAccessible(true); fields.add(f); } } array = fields.toArray(new Field[fields.size()]); fieldCache.put(cacheKey, array); } return array; }
From source file:org.openmrs.web.controller.report.ReportObjectFormController.java
@SuppressWarnings("unchecked") @Override/*from ww w .j av a 2 s . c o m*/ protected Map referenceData(HttpServletRequest request, Object obj, Errors err) throws Exception { Map addedData = new HashMap(); ReportObjectService rs = (ReportObjectService) Context.getService(ReportObjectService.class); List<String> availableTypes = rs.getReportObjectTypes(); addedData.put("availableTypes", availableTypes.iterator()); String selectedType = ServletRequestUtils.getStringParameter(request, "type", ""); if (selectedType.length() > 0) { List<String> availableSubTypes = rs.getReportObjectSubTypes(selectedType); addedData.put("availableSubTypes", availableSubTypes.iterator()); } Map extendedObjectInfo = new HashMap(); Map transientObjects = new HashMap(); for (Field field : obj.getClass().getDeclaredFields()) { String fieldName = field.getName(); int modifiers = field.getModifiers(); if ((modifiers & (0x0 | Modifier.TRANSIENT)) == (0x0 | Modifier.TRANSIENT)) { log.debug("OBJECT IS TRANSIENT, SO NOT TRYING TO EDIT"); transientObjects.put(fieldName, "true"); } else { Method m = obj.getClass().getMethod( "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1), (Class[]) null); Object fieldObj = m.invoke(obj, (Object[]) null); extendedObjectInfo.put(fieldName, fieldObj); } } addedData.put("extendedObjectInfo", extendedObjectInfo); addedData.put("transientObjects", transientObjects); return addedData; }
From source file:org.gvnix.service.roo.addon.addon.ws.export.WSExportValidationServiceImpl.java
/** * Add @GvNIXXmlElement annotation with attributes to java type. * <ul>/*from w ww .j a va 2 s . c o m*/ * <li>name attribute value from java type simple name</li> * <li>namespace attribute value from java type package</li> * <li>elementList attribute from all not transient fields (Java and AJs)</li> * <li>exported attribute is always false</li> * <li>xmlTypeName from java simple type, if not empty</li> * </ul> * * @param javaType To get attributes for gvNIX annotation * @param typeName Type name to add annotation */ protected void addGvNixXmlElementAnnotation(JavaType javaType, JavaType typeName) { List<AnnotationAttributeValue<?>> attrs = new ArrayList<AnnotationAttributeValue<?>>(); // name attribute value from java type simple name StringAttributeValue name = new StringAttributeValue(new JavaSymbolName("name"), StringUtils.uncapitalize(javaType.getSimpleTypeName())); attrs.add(name); // namespace attribute value from java type package StringAttributeValue namespace = new StringAttributeValue(new JavaSymbolName("namespace"), wSConfigService.convertPackageToTargetNamespace(javaType.getPackage().toString())); attrs.add(namespace); // Create attribute list with all (Java & AJs) no transient fields List<FieldMetadata> fields = javaParserService.getFieldsInAll(typeName); List<StringAttributeValue> values = new ArrayList<StringAttributeValue>(); for (FieldMetadata field : fields) { // Transient fields can't have JAXB annotations (b.e. entityManager) if (field.getModifier() != Modifier.TRANSIENT) { // Create an attribute list with fields StringAttributeValue value = new StringAttributeValue(new JavaSymbolName("ignored"), field.getFieldName().getSymbolName()); if (!values.contains(value)) { values.add(value); } } } ArrayAttributeValue<StringAttributeValue> elements = new ArrayAttributeValue<StringAttributeValue>( new JavaSymbolName("elementList"), values); attrs.add(elements); // xmlTypeName from java type simple type, if not empty if (elements != null && !elements.getValue().isEmpty()) { StringAttributeValue xmlTypeName = new StringAttributeValue(new JavaSymbolName("xmlTypeName"), javaType.getSimpleTypeName()); attrs.add(xmlTypeName); } else { StringAttributeValue xmlTypeName = new StringAttributeValue(new JavaSymbolName("xmlTypeName"), ""); attrs.add(xmlTypeName); } // exported attribute is always false (when code first) BooleanAttributeValue exported = new BooleanAttributeValue(new JavaSymbolName("exported"), false); attrs.add(exported); annotationsService.addJavaTypeAnnotation(typeName, GvNIXXmlElement.class.getName(), attrs, false); }
From source file:solidstack.reflect.Dumper.java
public void dumpTo(Object o, DumpWriter out) { try {//from ww w . j a v a2 s . com if (o == null) { out.append("<null>"); return; } Class<?> cls = o.getClass(); if (cls == String.class) { out.append("\"").append(((String) o).replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "\\r") .replace("\t", "\\t").replace("\"", "\\\"")).append("\""); return; } if (o instanceof CharSequence) { out.append("(").append(o.getClass().getName()).append(")"); dumpTo(o.toString(), out); return; } if (cls == char[].class) { out.append("(char[])"); dumpTo(String.valueOf((char[]) o), out); return; } if (cls == byte[].class) { out.append("byte[").append(Integer.toString(((byte[]) o).length)).append("]"); return; } if (cls == Class.class) { out.append(((Class<?>) o).getCanonicalName()).append(".class"); return; } if (cls == File.class) { out.append("File( \"").append(((File) o).getPath()).append("\" )"); return; } if (cls == AtomicInteger.class) { out.append("AtomicInteger( ").append(Integer.toString(((AtomicInteger) o).get())).append(" )"); return; } if (cls == AtomicLong.class) { out.append("AtomicLong( ").append(Long.toString(((AtomicLong) o).get())).append(" )"); return; } if (o instanceof ClassLoader) { out.append(o.getClass().getCanonicalName()); return; } if (cls == java.lang.Short.class || cls == java.lang.Long.class || cls == java.lang.Integer.class || cls == java.lang.Float.class || cls == java.lang.Byte.class || cls == java.lang.Character.class || cls == java.lang.Double.class || cls == java.lang.Boolean.class || cls == BigInteger.class || cls == BigDecimal.class) { out.append("(").append(cls.getSimpleName()).append(")").append(o.toString()); return; } String className = cls.getCanonicalName(); if (className == null) className = cls.getName(); out.append(className); if (this.skip.contains(className) || o instanceof java.lang.Thread) { out.append(" (skipped)"); return; } Integer id = this.visited.get(o); if (id == null) { id = ++this.id; this.visited.put(o, id); if (!this.hideIds) out.append(" <id=" + id + ">"); } else { out.append(" <refid=" + id + ">"); return; } if (cls.isArray()) { if (Array.getLength(o) == 0) out.append(" []"); else { out.newlineOrSpace().append("[").newlineOrSpace().indent().setFirst(); int rowCount = Array.getLength(o); for (int i = 0; i < rowCount; i++) { out.comma(); dumpTo(Array.get(o, i), out); } out.newlineOrSpace().unIndent().append("]"); } } else if (o instanceof Collection && !this.overriddenCollection.contains(className)) { Collection<?> list = (Collection<?>) o; if (list.isEmpty()) out.append(" []"); else { out.newlineOrSpace().append("[").newlineOrSpace().indent().setFirst(); for (Object value : list) { out.comma(); dumpTo(value, out); } out.newlineOrSpace().unIndent().append("]"); } } else if (o instanceof Properties && !this.overriddenCollection.contains(className)) // Properties is a Map, so it must come before the Map { Field def = cls.getDeclaredField("defaults"); if (!def.isAccessible()) def.setAccessible(true); Properties defaults = (Properties) def.get(o); Hashtable<?, ?> map = (Hashtable<?, ?>) o; out.newlineOrSpace().append("[").newlineOrSpace().indent().setFirst(); for (Map.Entry<?, ?> entry : map.entrySet()) { out.comma(); dumpTo(entry.getKey(), out); out.append(": "); dumpTo(entry.getValue(), out); } if (defaults != null && !defaults.isEmpty()) { out.comma().append("defaults: "); dumpTo(defaults, out); } out.newlineOrSpace().unIndent().append("]"); } else if (o instanceof Map && !this.overriddenCollection.contains(className)) { Map<?, ?> map = (Map<?, ?>) o; if (map.isEmpty()) out.append(" []"); else { out.newlineOrSpace().append("[").newlineOrSpace().indent().setFirst(); for (Map.Entry<?, ?> entry : map.entrySet()) { out.comma(); dumpTo(entry.getKey(), out); out.append(": "); dumpTo(entry.getValue(), out); } out.newlineOrSpace().unIndent().append("]"); } } else if (o instanceof Method) { out.newlineOrSpace().append("{").newlineOrSpace().indent().setFirst(); Field field = cls.getDeclaredField("clazz"); if (!field.isAccessible()) field.setAccessible(true); out.comma().append("clazz").append(": "); dumpTo(field.get(o), out); field = cls.getDeclaredField("name"); if (!field.isAccessible()) field.setAccessible(true); out.comma().append("name").append(": "); dumpTo(field.get(o), out); field = cls.getDeclaredField("parameterTypes"); if (!field.isAccessible()) field.setAccessible(true); out.comma().append("parameterTypes").append(": "); dumpTo(field.get(o), out); field = cls.getDeclaredField("returnType"); if (!field.isAccessible()) field.setAccessible(true); out.comma().append("returnType").append(": "); dumpTo(field.get(o), out); out.newlineOrSpace().unIndent().append("}"); } else { ArrayList<Field> fields = new ArrayList<Field>(); while (cls != Object.class) { Field[] fs = cls.getDeclaredFields(); for (Field field : fs) fields.add(field); cls = cls.getSuperclass(); } Collections.sort(fields, new Comparator<Field>() { public int compare(Field left, Field right) { return left.getName().compareTo(right.getName()); } }); if (fields.isEmpty()) out.append(" {}"); else { out.newlineOrSpace().append("{").newlineOrSpace().indent().setFirst(); for (Field field : fields) if ((field.getModifiers() & Modifier.STATIC) == 0) if (!this.hideTransients || (field.getModifiers() & Modifier.TRANSIENT) == 0) { out.comma().append(field.getName()).append(": "); if (!field.isAccessible()) field.setAccessible(true); if (field.getType().isPrimitive()) if (field.getType() == boolean.class) // TODO More? out.append(field.get(o).toString()); else out.append("(").append(field.getType().getName()).append(")") .append(field.get(o).toString()); else dumpTo(field.get(o), out); } out.newlineOrSpace().unIndent().append("}"); } } } catch (IOException e) { throw new FatalIOException(e); } catch (Exception e) { dumpTo(e.toString(), out); } }
From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteria.java
/** * Set the criterion that selects only {@link Member}s that exactly have the * specified modifiers. The access modifiers are set separately. Use * {@link #withAccess(AccessType...)} to set access modifiers. * * @param modifiers//from w w w . j a va 2 s . c o m * @since 1.0.0.0 */ public void withModifiers(int modifiers) { if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers) || Modifier.isPrivate(modifiers)) { throw new IllegalArgumentException( "access modifiers are not allowed as argument. Use withAccess() instead."); } int allowedModifiers = Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | Modifier.VOLATILE | Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT | Modifier.INTERFACE; if ((modifiers & allowedModifiers) == 0) { throw new IllegalArgumentException( "modifiers must be one of [" + Modifier.toString(allowedModifiers) + "]"); } this.modifiers = modifiers; }
From source file:net.redstoneore.legacyfactions.Factions.java
public GsonBuilder getGsonBuilder() { if (this.gsonBuilder == null) { Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() { }.getType();//from w ww. ja v a2s . com Type mapLocalityToStringSetType = new TypeToken<Map<Locality, Set<String>>>() { }.getType(); GsonBuilder builder = new GsonBuilder().setPrettyPrinting(); try { builder.setLenient(); } catch (NoSuchMethodError e) { // older minecraft plugins don't have this in their version of Gson } this.gsonBuilder = builder.disableHtmlEscaping() .excludeFieldsWithModifiers(Modifier.TRANSIENT, Modifier.VOLATILE) .registerTypeAdapter(LazyLocation.class, new LazyLocationAdapter()) .registerTypeAdapter(mapFLocToStringSetType, new MapFlocationSetAdapter()) .registerTypeAdapter(mapLocalityToStringSetType, new MapLocalitySetAdapter()) .registerTypeAdapter(CrossColour.class, new CrossColourAdapter()) .registerTypeAdapter(CrossEntityType.class, new CrossEntityTypeAdapter()) ; } return this.gsonBuilder; }
From source file:org.bitpipeline.lib.friendlyjson.JSONEntity.java
private JSONObject fillWithClass(JSONObject json, Class<?> clazz) throws JSONMappingException { for (Field field : clazz.getDeclaredFields()) { if ((field.getModifiers() & Modifier.TRANSIENT) != 0) { // don't care about transient fields. continue; }// w ww . j a v a2 s .c om String jsonName = field.getName(); if (jsonName.equals("this$0")) continue; boolean accessible = field.isAccessible(); if (!accessible) field.setAccessible(true); try { Object value = field.get(this); Object jsonField = toJson(value); json.put(jsonName, jsonField); } catch (Exception e) { throw new JSONMappingException(e); } if (!accessible) field.setAccessible(false); } return json; }
From source file:com.clavain.alerts.Methods.java
private static void sendCallback(Alert alert, String url) { Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create(); String json = gson.toJson(alert); sendPost(url, json);//from w w w . ja v a2 s .c o m }
From source file:com.clavain.alerts.Methods.java
private static void sendCallback(ReturnServiceCheck alert, String url) { Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create(); String json = gson.toJson(alert); sendPost(url, json);//from ww w. j a v a 2 s . co m }
From source file:com.ms.commons.summer.web.util.json.JsonBeanUtils.java
private static boolean isTransientField(String name, Class beanClass) { try {//from w w w .j av a 2 s. c o m Field field = beanClass.getDeclaredField(name); return (field.getModifiers() & Modifier.TRANSIENT) == Modifier.TRANSIENT; } catch (Exception e) { // swallow exception } return false; }