下面列出了java.lang.reflect.Modifier#isTransient ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Checks if the field is accepted as a JAXB property.
*/
static boolean isFieldAccepted(Field field, XmlAccessType accessType) {
// We only accept non static fields which are not marked @XmlTransient or has transient modifier
if (field.isAnnotationPresent(XmlTransient.class)
|| Modifier.isTransient(field.getModifiers())) {
return false;
}
if (Modifier.isStatic(field.getModifiers())) {
return field.isAnnotationPresent(XmlAttribute.class);
}
if (accessType == XmlAccessType.PUBLIC_MEMBER
&& !Modifier.isPublic(field.getModifiers())) {
return false;
}
if (accessType == XmlAccessType.NONE
|| accessType == XmlAccessType.PROPERTY) {
return checkJaxbAnnotation(field.getAnnotations());
}
return true;
}
protected void loadConfig() {
config = getConfigFromDisk();
for (Field field : getClass().getDeclaredFields()) {
if (Modifier.isTransient(field.getModifiers())
|| !field.getType().isPrimitive() && field.getType() != String.class) {
continue;
}
ConfigNode node = field.getAnnotation(ConfigNode.class);
String path = field.getName();
if (node != null && !Strings.isNullOrEmpty(path)) {
path = node.path();
}
field.setAccessible(true);
setField(path, field);
}
}
/**
* @param field
* @return true, if the given field should be compared.
*/
protected boolean accept(final Field field)
{
if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
// Reject field from inner class.
return false;
}
if (field.getName().equals("handler") == true) {
// Handler of Javassist proxy should be ignored.
return false;
}
if (Modifier.isTransient(field.getModifiers()) == true) {
// transients.
return false;
}
if (Modifier.isStatic(field.getModifiers()) == true) {
// transients.
return false;
}
return true;
}
private static void collectFieldAccessors(@Nonnull Class<?> aClass, @Nonnull List<MutableAccessor> accessors) {
Class<?> currentClass = aClass;
do {
for (Field field : currentClass.getDeclaredFields()) {
int modifiers = field.getModifiers();
//noinspection deprecation
if (!Modifier.isStatic(modifiers) &&
(field.getAnnotation(OptionTag.class) != null ||
field.getAnnotation(Tag.class) != null ||
field.getAnnotation(Attribute.class) != null ||
field.getAnnotation(Property.class) != null ||
field.getAnnotation(Text.class) != null ||
field.getAnnotation(CollectionBean.class) != null ||
(Modifier.isPublic(modifiers) &&
// we don't want to allow final fields of all types, but only supported
(!Modifier.isFinal(modifiers) || Collection.class.isAssignableFrom(field.getType())) &&
!Modifier.isTransient(modifiers) &&
field.getAnnotation(Transient.class) == null))) {
accessors.add(new FieldAccessor(field));
}
}
}
while ((currentClass = currentClass.getSuperclass()) != null && currentClass.getAnnotation(Transient.class) == null);
}
/**
* Returns whether or not to append the given <code>Field</code>.
* <ul>
* <li>Transient fields are appended only if {@link #isAppendTransients()} returns <code>true</code>.
* <li>Static fields are appended only if {@link #isAppendStatics()} returns <code>true</code>.
* <li>Inner class fields are not appened.</li>
* </ul>
*
* @param field
* The Field to test.
* @return Whether or not to append the given <code>Field</code>.
*/
protected boolean accept(Field field) {
if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
// Reject field from inner class.
return false;
}
if (Modifier.isTransient(field.getModifiers()) && !this.isAppendTransients()) {
// Reject transient fields.
return false;
}
if (Modifier.isStatic(field.getModifiers()) && !this.isAppendStatics()) {
// Reject static fields.
return false;
}
if (this.excludeFieldNames != null
&& Arrays.binarySearch(this.excludeFieldNames, field.getName()) >= 0) {
// Reject fields from the getExcludeFieldNames list.
return false;
}
return true;
}
/**
* Creates a map of the classes fields.
*/
protected HashMap getFieldMap(Class cl)
{
HashMap fieldMap = new HashMap();
for (; cl != null; cl = cl.getSuperclass()) {
Field[] fields = cl.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (Modifier.isTransient(field.getModifiers()) ||
Modifier.isStatic(field.getModifiers()))
continue;
// XXX: could parameterize the handler to only deal with public
field.setAccessible(true);
fieldMap.put(field.getName(), field);
}
}
return fieldMap;
}
/**
* Checks whether the field should be serialized or not
*
* @param keyName
* key name
* @param field
* The field to be serialized
* @param getter
* Getter method for field
* @return true if the field should be serialized, otherwise false
*/
public static boolean serializeField(String keyName, Field field, Method getter) {
log.trace("serializeField - keyName: {} field: {} method: {}", new Object[] { keyName, field, getter });
// if "field" is a class or is transient, skip it
if ("class".equals(keyName)) {
return false;
}
if (field != null) {
if (Modifier.isTransient(field.getModifiers())) {
log.trace("Skipping {} because its transient", keyName);
return false;
} else if (field.isAnnotationPresent(DontSerialize.class)) {
log.trace("Skipping {} because its marked with @DontSerialize", keyName);
return false;
}
}
if (getter != null && getter.isAnnotationPresent(DontSerialize.class)) {
log.trace("Skipping {} because its marked with @DontSerialize", keyName);
return false;
}
log.trace("Serialize field: {}", field);
return true;
}
/**
* Returns whether or not to append the given <code>Field</code>.
* <ul>
* <li>Transient fields are appended only if {@link #isAppendTransients()} returns <code>true</code>.
* <li>Static fields are appended only if {@link #isAppendStatics()} returns <code>true</code>.
* <li>Inner class fields are not appened.</li>
* </ul>
*
* @param field
* The Field to test.
* @return Whether or not to append the given <code>Field</code>.
*/
protected boolean accept(Field field) {
if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
// Reject field from inner class.
return false;
}
if (Modifier.isTransient(field.getModifiers()) && !this.isAppendTransients()) {
// Reject transient fields.
return false;
}
if (Modifier.isStatic(field.getModifiers()) && !this.isAppendStatics()) {
// Reject static fields.
return false;
}
if (this.getExcludeFieldNames() != null
&& Arrays.binarySearch(this.getExcludeFieldNames(), field.getName()) >= 0) {
// Reject fields from the getExcludeFieldNames list.
return false;
}
return true;
}
public static Map<String, Field> getBeanPropertyFields(Class cl) {
Map<String, Field> properties = new HashMap<String, Field>();
for(; cl != null; cl = cl.getSuperclass()) {
Field[] fields = cl.getDeclaredFields();
for(Field field : fields) {
if (Modifier.isTransient(field.getModifiers())
|| Modifier.isStatic(field.getModifiers())) {
continue;
}
field.setAccessible(true);
properties.put(field.getName(), field);
}
}
return properties;
}
private static boolean isTransient(AnnotatedElement annotatedElement) {
int modifiers = ((Member) annotatedElement).getModifiers();
if (Modifier.isTransient(modifiers)) {
return true;
}
return false;
}
private static boolean hasValidField(RecordedEvent e) throws Exception {
RecordedObject object = e.getValue("object");
Set<Long> visited = new HashSet<>();
while (object != null) {
Long address = object.getValue("address");
if (visited.contains(address)) {
return false;
}
visited.add(address);
RecordedObject referrer = object.getValue("referrer");
RecordedObject fieldObject = referrer != null ? referrer.getValue("field") : null;
if (fieldObject != null) {
String name = fieldObject.getValue("name");
if (name != null && name.equals("testField")) {
int modifiers = (short) fieldObject.getValue("modifiers");
if (!Modifier.isStatic(modifiers)) {
throw new Exception("Field should be static");
}
if (!Modifier.isPublic(modifiers)) {
throw new Exception("Field should be private");
}
if (!Modifier.isFinal(modifiers)) {
throw new Exception("Field should be final");
}
if (Modifier.isTransient(modifiers)) {
throw new Exception("Field should not be transient");
}
if (Modifier.isVolatile(modifiers)) {
throw new Exception("Field should not be volatile");
}
return true;
}
}
object = referrer != null ? referrer.getValue("object") : null;
}
return false;
}
private static ValueDescriptor createField(Field field) {
int mod = field.getModifiers();
if (Modifier.isTransient(mod)) {
return null;
}
if (Modifier.isStatic(mod)) {
return null;
}
Class<?> fieldType = field.getType();
if (!Type.isKnownType(fieldType)) {
return null;
}
boolean constantPool = Thread.class == fieldType || fieldType == Class.class;
Type type = createType(fieldType);
String fieldName = field.getName();
Name name = field.getAnnotation(Name.class);
String useName = fieldName;
if (name != null) {
useName = name.value();
}
List<jdk.jfr.AnnotationElement> ans = new ArrayList<>();
for (Annotation a : resolveRepeatedAnnotations(field.getAnnotations())) {
AnnotationElement ae = createAnnotation(a);
if (ae != null) {
ans.add(ae);
}
}
return PrivateAccess.getInstance().newValueDescriptor(useName, type, ans, 0, constantPool, fieldName);
}
public boolean isTransientModifier(){
return Modifier.isTransient(readMethod.getModifiers());
}
/**
* Creates a map of the classes fields.
*/
protected HashMap getFieldMap(Class cl)
{
HashMap fieldMap = new HashMap();
for (; cl != null; cl = cl.getSuperclass()) {
Field []fields = cl.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (Modifier.isTransient(field.getModifiers())
|| Modifier.isStatic(field.getModifiers()))
continue;
else if (fieldMap.get(field.getName()) != null)
continue;
// XXX: could parameterize the handler to only deal with public
try {
field.setAccessible(true);
} catch (Throwable e) {
e.printStackTrace();
}
Class type = field.getType();
FieldDeserializer deser;
if (String.class.equals(type))
deser = new StringFieldDeserializer(field);
else if (byte.class.equals(type)) {
deser = new ByteFieldDeserializer(field);
}
else if (short.class.equals(type)) {
deser = new ShortFieldDeserializer(field);
}
else if (int.class.equals(type)) {
deser = new IntFieldDeserializer(field);
}
else if (long.class.equals(type)) {
deser = new LongFieldDeserializer(field);
}
else if (float.class.equals(type)) {
deser = new FloatFieldDeserializer(field);
}
else if (double.class.equals(type)) {
deser = new DoubleFieldDeserializer(field);
}
else if (boolean.class.equals(type)) {
deser = new BooleanFieldDeserializer(field);
}
else if (java.sql.Date.class.equals(type)) {
deser = new SqlDateFieldDeserializer(field);
}
else if (java.sql.Timestamp.class.equals(type)) {
deser = new SqlTimestampFieldDeserializer(field);
}
else if (java.sql.Time.class.equals(type)) {
deser = new SqlTimeFieldDeserializer(field);
}
else {
deser = new ObjectFieldDeserializer(field);
}
fieldMap.put(field.getName(), deser);
}
}
return fieldMap;
}
/**
* Creates a map of the classes fields.
*/
protected HashMap getFieldMap(Class cl)
{
HashMap fieldMap = new HashMap();
for (; cl != null; cl = cl.getSuperclass()) {
Field []fields = cl.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (Modifier.isTransient(field.getModifiers())
|| Modifier.isStatic(field.getModifiers()))
continue;
else if (fieldMap.get(field.getName()) != null)
continue;
// XXX: could parameterize the handler to only deal with public
try {
field.setAccessible(true);
} catch (Throwable e) {
e.printStackTrace();
}
Class type = field.getType();
FieldDeserializer deser;
if (String.class.equals(type))
deser = new StringFieldDeserializer(field);
else if (byte.class.equals(type)) {
deser = new ByteFieldDeserializer(field);
}
else if (short.class.equals(type)) {
deser = new ShortFieldDeserializer(field);
}
else if (int.class.equals(type)) {
deser = new IntFieldDeserializer(field);
}
else if (long.class.equals(type)) {
deser = new LongFieldDeserializer(field);
}
else if (float.class.equals(type)) {
deser = new FloatFieldDeserializer(field);
}
else if (double.class.equals(type)) {
deser = new DoubleFieldDeserializer(field);
}
else if (boolean.class.equals(type)) {
deser = new BooleanFieldDeserializer(field);
}
else if (java.sql.Date.class.equals(type)) {
deser = new SqlDateFieldDeserializer(field);
}
else if (java.sql.Timestamp.class.equals(type)) {
deser = new SqlTimestampFieldDeserializer(field);
}
else if (java.sql.Time.class.equals(type)) {
deser = new SqlTimeFieldDeserializer(field);
}
else {
deser = new ObjectFieldDeserializer(field);
}
fieldMap.put(field.getName(), deser);
}
}
return fieldMap;
}
/**
* Do the provided modifiers indicate that this is public?
* @param modifiers the flags to check
* @return true if public but not final, static or transient.
*/
public static boolean isPublicField(int modifiers)
{
return (Modifier.isPublic(modifiers) && !Modifier.isFinal(modifiers)
&& !Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers));
}
/** Returns true if the field is transient. */
final boolean isTransient() {
return Modifier.isTransient(getModifiers());
}
private static void declaredOneField(Field field, Map<Integer, List<Field>> orderFiledMap,
Map<Integer, Field> indexFiledMap, Map<String, Field> ignoreMap, ExcelIgnoreUnannotated excelIgnoreUnannotated,
Boolean convertAllFiled) {
ExcelIgnore excelIgnore = field.getAnnotation(ExcelIgnore.class);
if (excelIgnore != null) {
ignoreMap.put(field.getName(), field);
return;
}
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
boolean noExcelProperty = excelProperty == null
&& ((convertAllFiled != null && !convertAllFiled) || excelIgnoreUnannotated != null);
if (noExcelProperty) {
ignoreMap.put(field.getName(), field);
return;
}
boolean isStaticFinalOrTransient =
(Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()))
|| Modifier.isTransient(field.getModifiers());
if (excelProperty == null && isStaticFinalOrTransient) {
ignoreMap.put(field.getName(), field);
return;
}
if (excelProperty != null && excelProperty.index() >= 0) {
if (indexFiledMap.containsKey(excelProperty.index())) {
throw new ExcelCommonException("The index of '" + indexFiledMap.get(excelProperty.index()).getName()
+ "' and '" + field.getName() + "' must be inconsistent");
}
indexFiledMap.put(excelProperty.index(), field);
return;
}
int order = Integer.MAX_VALUE;
if (excelProperty != null) {
order = excelProperty.order();
}
List<Field> orderFiledList = orderFiledMap.get(order);
if (orderFiledList == null) {
orderFiledList = new ArrayList<Field>();
orderFiledMap.put(order, orderFiledList);
}
orderFiledList.add(field);
}
/**
* Return true if this field is transient
*/
public boolean isTransient() {
return Modifier.isTransient(getModifiers());
}
/**
* Returns true if given element is transient.
*
* @return true if given element is transient.
*/
default boolean isTransient()
{
return Modifier.isTransient(this.getModifiers());
}