public void testImmutable() { Class<Fastq> cls = Fastq.class; assertTrue(Modifier.isPublic(cls.getModifiers())); assertTrue(Modifier.isFinal(cls.getModifiers())); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { assertTrue(Modifier.isPrivate(field.getModifiers())); assertTrue( Modifier.isFinal(field.getModifiers()) || (Modifier.isVolatile(field.getModifiers()) && Modifier.isTransient(field.getModifiers()))); } }
AtomicReferenceFieldUpdaterImpl( Class<T> tclass, Class<V> vclass, String fieldName, Class<?> caller) { Field field = null; Class fieldClass = null; int modifiers = 0; try { field = tclass.getDeclaredField(fieldName); modifiers = field.getModifiers(); sun.reflect.misc.ReflectUtil.ensureMemberAccess(caller, tclass, null, modifiers); sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); fieldClass = field.getType(); } catch (Exception ex) { throw new RuntimeException(ex); } if (vclass != fieldClass) throw new ClassCastException(); if (vclass.isPrimitive()) throw new IllegalArgumentException("Must be reference type"); if (!Modifier.isVolatile(modifiers)) throw new IllegalArgumentException("Must be volatile type"); this.cclass = (Modifier.isProtected(modifiers) && caller != tclass) ? caller : null; this.tclass = tclass; if (vclass == Object.class) this.vclass = null; else this.vclass = vclass; offset = unsafe.objectFieldOffset(field); }
/** * Returns array of all methods that are accessible from given class, upto limit (usually <code> * Object.class</code>). Abstract methods are ignored. */ public static Method[] getAccessibleMethods(Class clazz, Class limit) { Package topPackage = clazz.getPackage(); List<Method> methodList = new ArrayList<Method>(); int topPackageHash = topPackage == null ? 0 : topPackage.hashCode(); boolean top = true; do { if (clazz == null) { break; } Method[] declaredMethods = clazz.getDeclaredMethods(); for (Method method : declaredMethods) { if (Modifier.isVolatile(method.getModifiers())) { continue; } // if (Modifier.isAbstract(method.getModifiers())) { // continue; // } if (top == true) { // add all top declared methods methodList.add(method); continue; } int modifier = method.getModifiers(); if (Modifier.isPrivate(modifier) == true) { continue; // ignore super private methods } if (Modifier.isAbstract(modifier) == true) { // ignore super abstract methods continue; } if (Modifier.isPublic(modifier) == true) { addMethodIfNotExist(methodList, method); // add super public methods continue; } if (Modifier.isProtected(modifier) == true) { addMethodIfNotExist(methodList, method); // add super protected methods continue; } // add super default methods from the same package Package pckg = method.getDeclaringClass().getPackage(); int pckgHash = pckg == null ? 0 : pckg.hashCode(); if (pckgHash == topPackageHash) { addMethodIfNotExist(methodList, method); } } top = false; } while ((clazz = clazz.getSuperclass()) != limit); Method[] methods = new Method[methodList.size()]; for (int i = 0; i < methods.length; i++) { methods[i] = methodList.get(i); } return methods; }
/** setModifiers - set the attribute values related to modifiers here */ protected void setModifiers() { Field javaFieldTarget = (Field) getTarget(); // For JDK reflection, I don't think we can set the initializer int modifiers = getSourceField().getModifiers(); javaFieldTarget.setStatic(java.lang.reflect.Modifier.isStatic(modifiers)); javaFieldTarget.setTransient(java.lang.reflect.Modifier.isTransient(modifiers)); javaFieldTarget.setVolatile(java.lang.reflect.Modifier.isVolatile(modifiers)); javaFieldTarget.setFinal(java.lang.reflect.Modifier.isFinal(modifiers)); // Set visibility if (java.lang.reflect.Modifier.isPublic(modifiers)) javaFieldTarget.setJavaVisibility(JavaVisibilityKind.PUBLIC_LITERAL); else if (java.lang.reflect.Modifier.isPrivate(modifiers)) javaFieldTarget.setJavaVisibility(JavaVisibilityKind.PRIVATE_LITERAL); else if (java.lang.reflect.Modifier.isProtected(modifiers)) javaFieldTarget.setJavaVisibility(JavaVisibilityKind.PROTECTED_LITERAL); else javaFieldTarget.setJavaVisibility(JavaVisibilityKind.PACKAGE_LITERAL); }
private Method lookForMethod(String name, int nParams, Class clazz) { Method result = null; for (Method method : clazz.getMethods()) { if (method.getName().equals(name) && method.getParameterTypes().length == nParams) { if (Modifier.isVolatile(method.getModifiers())) continue; if (result != null) { throw new ModelSourceException( "More than one method " + "named '" + name + "' with " + nParams + " parameters found."); } result = method; } } return result; }
LockedUpdater(Class<T> tclass, String fieldName, Class<?> caller) { Field field = null; int modifiers = 0; try { field = tclass.getDeclaredField(fieldName); modifiers = field.getModifiers(); sun.reflect.misc.ReflectUtil.ensureMemberAccess(caller, tclass, null, modifiers); sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass); } catch (Exception ex) { throw new RuntimeException(ex); } Class fieldt = field.getType(); if (fieldt != long.class) throw new IllegalArgumentException("Must be long type"); if (!Modifier.isVolatile(modifiers)) throw new IllegalArgumentException("Must be volatile type"); this.cclass = (Modifier.isProtected(modifiers) && caller != tclass) ? caller : null; this.tclass = tclass; offset = unsafe.objectFieldOffset(field); }
/** Return the set of modifiers defined by the modifiers integer (java.lang.reflect). */ public static Set<ModifierKind> getModifiers(int mod) { Set<ModifierKind> set = new TreeSet<ModifierKind>(); if (Modifier.isAbstract(mod)) { set.add(ModifierKind.ABSTRACT); } if (Modifier.isFinal(mod)) { set.add(ModifierKind.FINAL); } if (Modifier.isNative(mod)) { set.add(ModifierKind.NATIVE); } if (Modifier.isPrivate(mod)) { set.add(ModifierKind.PRIVATE); } if (Modifier.isProtected(mod)) { set.add(ModifierKind.PROTECTED); } if (Modifier.isPublic(mod)) { set.add(ModifierKind.PUBLIC); } if (Modifier.isStatic(mod)) { set.add(ModifierKind.STATIC); } if (Modifier.isStrict(mod)) { set.add(ModifierKind.STRICTFP); } if (Modifier.isSynchronized(mod)) { set.add(ModifierKind.SYNCHRONIZED); } if (Modifier.isTransient(mod)) { set.add(ModifierKind.TRANSIENT); } if (Modifier.isVolatile(mod)) { set.add(ModifierKind.VOLATILE); } return set; }
public boolean isVolatile() { return Modifier.isVolatile(getModifiers()); }
public boolean doSkip(Field field) { int mod = field.getModifiers(); return Modifier.isFinal(mod) || Modifier.isTransient(mod) || Modifier.isVolatile(mod); }
// ------------------------------------------------------------------------- private void serializeObject(StringBuilder p_buf, Field p_field, Object p_obj) throws Exception { String l_name = null; Field[] l_flds = null; int l_mod = 0; String[] l_args = null; SerializeXML l_ann = null; boolean l_substitute = false; if (p_field == null) { return; } l_ann = p_field.getAnnotation(SerializeXML.class); // System.out.println ("Field [" + p_field.getName() + "] Ann [" + l_ann + "] "); l_mod = p_field.getModifiers(); if (Modifier.isTransient(l_mod)) { return; } l_name = CoreUtils.truncateClassName(p_field.getName()); if (l_ann != null) { if ((l_ann.xmlTag() != null) && (l_ann.xmlTag().length() > 0)) { l_name = l_ann.xmlTag(); } // l_substitute = l_ann.substitute(); } if (p_field.getType().isArray()) { if (SerializableDO.class.isAssignableFrom(p_field.getType().getComponentType())) { if (p_obj != null) { addStartNode(p_buf, l_name); for (int l_i = 0; l_i < Array.getLength(p_obj); l_i++) { if (l_substitute) { // System.out.println ("*****" // + p_field.getType ().getComponentType ().getName() + " " + Array.get(p_obj, // l_i).getClass().getName()); addStartNode( p_buf, CoreUtils.truncateClassName(Array.get(p_obj, l_i).getClass().getName()), Array.get(p_obj, l_i).getClass(), null); } else { addStartNode( p_buf, CoreUtils.truncateClassName(p_field.getType().getComponentType().getName()), p_field.getType().getComponentType(), null); } l_flds = p_field.getType().getComponentType().getDeclaredFields(); for (int l_j = 0; l_j < l_flds.length; l_j++) { // System.err.println ("FIELD " // + l_flds [l_j]); if (Array.get(p_obj, l_i) != null) { serializeObject( p_buf, l_flds[l_j], CoreUtils.getFieldValue( l_flds[l_j], (SerializableDO) Array.get(p_obj, l_i)) // l_flds [l_j].get (Array.get(p_obj, l_i)) ); } } if (l_substitute) { addEndNode( p_buf, CoreUtils.truncateClassName(Array.get(p_obj, l_i).getClass().getName())); } else { addEndNode( p_buf, CoreUtils.truncateClassName(p_field.getType().getComponentType().getName())); } } addEndNode(p_buf, l_name); } } else if (CoreUtils.isTypeSupported(p_field.getType().getComponentType().getName())) { if (p_obj != null) { if (CoreUtils.isTypeByteArray(p_field.getType())) { addStartNode(p_buf, l_name); p_buf.append(CoreUtils.getStringValue(p_obj)); addEndNode(p_buf, l_name); } else { for (int l_i = 0; l_i < Array.getLength(p_obj); l_i++) { addStartNode(p_buf, l_name); p_buf.append(CoreUtils.getStringValue(Array.get(p_obj, l_i))); addEndNode(p_buf, l_name); } } } } else { l_args = new String[1]; l_args[0] = p_field.getType().getComponentType().getName(); throw new CoreException( "Exception for [" + p_field.getType().getComponentType().getName() + "]"); } } else { if (SerializableDO.class.isAssignableFrom(p_field.getType())) { if (p_obj != null) { // if (! l_substitute) { // addStartNode(p_buf, l_name); // } addStartNode( p_buf, l_name // CoreUtils.truncateClassName(p_obj.getClass ().getName()) , p_obj.getClass(), null); l_flds = p_obj.getClass().getDeclaredFields(); for (int l_i = 0; l_i < l_flds.length; l_i++) { serializeObject( p_buf, l_flds[l_i], CoreUtils.getFieldValue(l_flds[l_i], (SerializableDO) p_obj)); } addEndNode( p_buf, l_name // CoreUtils.truncateClassName(p_obj.getClass ().getName()) ); // if (! l_substitute) { // addEndNode(p_buf, l_name); // } } } else if (CoreUtils.isTypeSupported(p_field.getType().getName())) { // l_ann = // p_field.getAnnotation(SerializeXML.class); // System.out.println ("Field 4 [" + // p_field.getName() + "] Ann [" + l_ann + "] "); addStartNode(p_buf, l_name); p_buf.append(CoreUtils.getStringValue(p_obj, !Modifier.isVolatile(l_mod))); addEndNode(p_buf, l_name); } else { l_args = new String[1]; l_args[0] = p_field.getType().getName(); System.out.println(l_args[0]); throw new CoreException(MessageCodes.ECOD_CANNOT_SERIALIZE_FIELD, l_args); } } }
public boolean isVolatile() { return Modifier.isVolatile(this.code); }