當前位置: 首頁>>代碼示例>>Java>>正文


Java Field.getModifiers方法代碼示例

本文整理匯總了Java中java.lang.reflect.Field.getModifiers方法的典型用法代碼示例。如果您正苦於以下問題:Java Field.getModifiers方法的具體用法?Java Field.getModifiers怎麽用?Java Field.getModifiers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.reflect.Field的用法示例。


在下文中一共展示了Field.getModifiers方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getStatments

import java.lang.reflect.Field; //導入方法依賴的package包/類
protected static Map<String, SQLStmt> getStatments(Procedure proc) {
    Class<? extends Procedure> c = proc.getClass();
    Map<String, SQLStmt> stmts = new HashMap<String, SQLStmt>();
    for (Field f : c.getDeclaredFields()) {
        int modifiers = f.getModifiers();
        if (Modifier.isTransient(modifiers) == false &&
            Modifier.isPublic(modifiers) == true &&
            Modifier.isStatic(modifiers) == false) {
            try {
                Object o = f.get(proc);
                if (o instanceof SQLStmt) {
                    stmts.put(f.getName(), (SQLStmt)o);
                }
            } catch (Exception ex) {
                throw new RuntimeException("Failed to retrieve " + f + " from " + c.getSimpleName(), ex);
            }
        }
    } // FOR
    return (stmts);
}
 
開發者ID:faclc4,項目名稱:HTAPBench,代碼行數:21,代碼來源:Procedure.java

示例2: write

import java.lang.reflect.Field; //導入方法依賴的package包/類
public void write(Object object) throws IllegalArgumentException, IllegalAccessException {

		alreadyWritten.clear();
		alreadyWritten.put(object, object);

		Class<?> clazz = object.getClass();
		while (clazz != Object.class) {
			Field[] fields = clazz.getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				int modifiers = field.getModifiers();
				if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isVolatile(modifiers)) {
					// Skip
					continue;
				}
				if (field.isSynthetic()) {
					// Skip
					continue;
				}

				writeProperty(field.getName(), field.get(object));
			}

			clazz = clazz.getSuperclass();
		}
	}
 
開發者ID:Puppygames,項目名稱:thjson,代碼行數:27,代碼來源:POJOtoTHJSONConverter.java

示例3: buildAndBind

import java.lang.reflect.Field; //導入方法依賴的package包/類
protected <T> void buildAndBind(Class<?> currentClazz, String path, List<Component> components,
		Set<Class<?>> nestedClasses) {
	List<Field> fields = getFieldsInDeclareOrder(currentClazz);

	for (Field field : fields) {
		if ((field.getModifiers() & Modifier.STATIC) != 0) {
			continue;
		}
		if (boundProperties.containsKey(path + field.getName())) {
			// property already bound, skip
			continue;
		}
		if (nestedClasses.contains(field.getType())) {
			buildAndBind(field.getType(), path + field.getName() + ".", components,
					nestedClasses);
		} else {
			components.add(createAndBind(field, path));
		}
	}
}
 
開發者ID:ljessendk,項目名稱:easybinder,代碼行數:21,代碼來源:AutoBinder.java

示例4: validate

import java.lang.reflect.Field; //導入方法依賴的package包/類
private void validate(Object object, Class<?> clazz) throws Exception {
    for (Field field : FieldUtils.getAllFields(clazz)) {
        int modifiers = field.getModifiers();

        // Ignore static fields
        if (Modifier.isStatic(modifiers)) {
            continue;
        }

        assertTrue("Field is private", Modifier.isPrivate(modifiers));
        assertTrue("Field is final", Modifier.isFinal(modifiers));

        Method getter = clazz.getMethod(getterName(field.getName()));
        assertNotNull("Getter exists", getter);
        assertTrue("Getter is public", Modifier.isPublic(getter.getModifiers()));
    }

    // Check that hashCode, toString and equals are defined
    assertNotNull(clazz.getDeclaredMethod("hashCode").invoke(object));
    assertNotNull(clazz.getDeclaredMethod("toString").invoke(object));
    assertTrue((Boolean) clazz.getDeclaredMethod("equals", Object.class).invoke(object, object));
}
 
開發者ID:carlanton,項目名稱:mpd-tools,代碼行數:23,代碼來源:DataTypeTest.java

示例5: HackedField

import java.lang.reflect.Field; //導入方法依賴的package包/類
/** @param modifiers the modifiers this field must have */
HackedField(final Class<C> clazz, final String name, int modifiers) throws HackAssertionException {
	Field field = null;
	try {
		if (clazz == null) return;
		field = clazz.getDeclaredField(name);
		if (modifiers > 0 && (field.getModifiers() & modifiers) != modifiers)
			fail(new HackAssertionException(field + " does not match modifiers: " + modifiers));
		field.setAccessible(true);
	} catch (final NoSuchFieldException e) {
		HackAssertionException hae = new HackAssertionException(e);
		hae.setHackedClass(clazz);
		hae.setHackedFieldName(name);
		fail(hae);
	} finally { mField = field; }
}
 
開發者ID:alibaba,項目名稱:atlas,代碼行數:17,代碼來源:Hack.java

示例6: HackedField

import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
 * @param modifiers the modifiers this field must have
 */
HackedField(final Class<C> clazz, final String name, int modifiers) throws HackAssertionException {
  Field field = null;
  try {
    if (clazz == null) {
      return;
    }
    field = clazz.getDeclaredField(name);
    if (modifiers > 0 && (field.getModifiers() & modifiers) != modifiers) {
      fail(new HackAssertionException(field + " does not match modifiers: " + modifiers));
    }
    field.setAccessible(true);
  } catch (final NoSuchFieldException e) {
    HackAssertionException hae = new HackAssertionException(e);
    hae.setHackedClass(clazz);
    hae.setHackedFieldName(name);
    fail(hae);
  } finally {
    mField = field;
  }
}
 
開發者ID:erguotou520,項目名稱:weex-uikit,代碼行數:24,代碼來源:WXHack.java

示例7: ListenerSet

import java.lang.reflect.Field; //導入方法依賴的package包/類
public ListenerSet() {
    eventTypes = new Vector<>();
    try {
        Class<?> eventClass = Class.forName("java.awt.AWTEvent");
        Field[] fields = eventClass.getFields();
        theWholeMask = 0;
        long eventMask;
        for (Field field : fields) {
            if ((field.getModifiers()
                    & (Modifier.PUBLIC | Modifier.STATIC)) != 0
                    && field.getType().equals(Long.TYPE)
                    && field.getName().endsWith("_EVENT_MASK")) {
                eventMask = (Long) field.get(null);
                eventTypes.add(new EventType(eventMask));
                theWholeMask = theWholeMask | eventMask;
            }
        }
    } catch (ClassNotFoundException | IllegalAccessException e) {
        JemmyProperties.getCurrentOutput().printStackTrace(e);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:EventTool.java

示例8: getDeclaredSUID

import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
 * Returns explicit serial version UID value declared by given class, or
 * null if none.
 */
private static Long getDeclaredSUID(Class<?> cl) {
    try {
        Field f = cl.getDeclaredField("serialVersionUID");
        int mask = Modifier.STATIC | Modifier.FINAL;
        if ((f.getModifiers() & mask) == mask) {
            f.setAccessible(true);
            return Long.valueOf(f.getLong(null));
        }
    } catch (Exception ex) {
    }
    return null;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:17,代碼來源:ObjectStreamClass.java

示例9: ClassFormat

import java.lang.reflect.Field; //導入方法依賴的package包/類
public ClassFormat(Class<?> type) {
    this.name = type.getSimpleName();

    int approxSize = 0;

    LinkedList<FieldFormat> fields = new LinkedList<>();

    do {
        Field[] declaredFields = type.getDeclaredFields();

        if (declaredFields.length > 0) {
            List<FieldFormat> typeFields = new ArrayList<>(declaredFields.length);

            for (Field field : declaredFields) {
                if (!field.isSynthetic()) {
                    int mod = field.getModifiers();

                    if (!Modifier.isStatic(mod) && field.getAnnotation(ToStringIgnore.class) == null) {
                        field.setAccessible(true);

                        typeFields.add(new FieldFormat(field));

                        // This is really approximate.
                        approxSize += field.getName().length() * 2;
                    }
                }
            }

            if (!typeFields.isEmpty()) {
                fields.addAll(0, typeFields);
            }
        }

        type = type.getSuperclass();
    }
    while (type != null && type != Object.class);

    this.fields = fields;
    this.approxSize = approxSize;
}
 
開發者ID:hekate-io,項目名稱:hekate,代碼行數:41,代碼來源:ToString.java

示例10: matchInheritance

import java.lang.reflect.Field; //導入方法依賴的package包/類
public static boolean matchInheritance(final Field subclassField, final Field superclassField) {
	if (Modifier.isStatic(superclassField.getModifiers()) || subclassField.equals(superclassField)) {
		return false;
	}

	final Package subclassPackage = superclassField.getDeclaringClass().getPackage();
	final Package superclassPackage = superclassField.getDeclaringClass().getPackage();
	final int superFieldModifiers = superclassField.getModifiers();

	return isAccessable(subclassPackage, superclassPackage, superFieldModifiers);
}
 
開發者ID:daima,項目名稱:solo-spring,代碼行數:12,代碼來源:Reflections.java

示例11: toString

import java.lang.reflect.Field; //導入方法依賴的package包/類
@Override
public String toString() {
	final Class<?> thisClass = getClass();
	final Map<String,Object> values = new Object2ObjectOpenHashMap<>();
	for (final Field f : thisClass.getDeclaredFields()) {
		if ((f.getModifiers() & Modifier.PUBLIC) == 0 || (f.getModifiers() & Modifier.STATIC) != 0) continue;
		try {
			values.put(f.getName(), f.get(this));
		} catch (final IllegalAccessException e) {
			values.put(f.getName(), "<THIS SHOULD NOT HAPPEN>");
		}
	}
	return values.toString();
}
 
開發者ID:LAW-Unimi,項目名稱:BUbiNG,代碼行數:15,代碼來源:StartupConfiguration.java

示例12: save

import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
 * Save current settings to configuration.
 */
public void save() {
	try {
		if (!confFile.exists() && !confFile.createNewFile()) {
			// Return if config file cannot be found and cannot be created.
			return;
		}
		JsonObject json = Json.object();
		for (Field field : this.getClass().getDeclaredFields()) {
			// Skip private and static fields.
			int mod = field.getModifiers();
			if (Modifier.isPrivate(mod) || Modifier.isStatic(mod)) {
				continue;
			}
			// Access via reflection, add value to json object.
			field.setAccessible(true);
			String name = field.getName();
			Object value = field.get(this);
			if (value instanceof Boolean) {
				json.set(name, (boolean) value);
			} else if (value instanceof Integer) {
				json.set(name, (int) value);
			} else if (value instanceof String) {
				json.set(name, (String) value);
			} else {
				JsonValue converted = convert(field.getType(), value);
				if (converted != null) {
					json.set(name, converted);
				}
			}
		}
		// Write json to file
		StringWriter w = new StringWriter();
		json.writeTo(w, WriterConfig.PRETTY_PRINT);
		Files.writeFile(confFile.getAbsolutePath(), w.toString());
	} catch (Exception e) {
		Recaf.INSTANCE.logging.error(e);
	}
}
 
開發者ID:Col-E,項目名稱:Recaf,代碼行數:42,代碼來源:Config.java

示例13: CASUpdater

import java.lang.reflect.Field; //導入方法依賴的package包/類
CASUpdater(final Class<T> tclass, final String fieldName,
           final Class<?> caller) {
    final Field field;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
          sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } 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);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:39,代碼來源:AtomicLongFieldUpdater.java

示例14: AtomicReferenceFieldUpdaterImpl

import java.lang.reflect.Field; //導入方法依賴的package包/類
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass,
                                final Class<V> vclass,
                                final String fieldName,
                                final Class<?> caller) {
    final Field field;
    final Class<?> fieldClass;
    final int modifiers;
    try {
        field = AccessController.doPrivileged(
            new PrivilegedExceptionAction<Field>() {
                public Field run() throws NoSuchFieldException {
                    return tclass.getDeclaredField(fieldName);
                }
            });
        modifiers = field.getModifiers();
        sun.reflect.misc.ReflectUtil.ensureMemberAccess(
            caller, tclass, null, modifiers);
        ClassLoader cl = tclass.getClassLoader();
        ClassLoader ccl = caller.getClassLoader();
        if ((ccl != null) && (ccl != cl) &&
            ((cl == null) || !isAncestor(cl, ccl))) {
            sun.reflect.misc.ReflectUtil.checkPackageAccess(tclass);
        }
        fieldClass = field.getType();
    } catch (PrivilegedActionException pae) {
        throw new RuntimeException(pae.getException());
    } 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");

    // Access to protected field members is restricted to receivers only
    // of the accessing class, or one of its subclasses, and the
    // accessing class must in turn be a subclass (or package sibling)
    // of the protected member's defining class.
    // If the updater refers to a protected field of a declaring class
    // outside the current package, the receiver argument will be
    // narrowed to the type of the accessing class.
    this.cclass = (Modifier.isProtected(modifiers) &&
                   tclass.isAssignableFrom(caller) &&
                   !isSamePackage(tclass, caller))
                  ? caller : tclass;
    this.tclass = tclass;
    this.vclass = vclass;
    this.offset = U.objectFieldOffset(field);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:54,代碼來源:AtomicReferenceFieldUpdater.java

示例15: computeStructuralUID

import java.lang.reflect.Field; //導入方法依賴的package包/類
public static long computeStructuralUID(boolean hasWriteObject, Class<?> cl) {
    ByteArrayOutputStream devnull = new ByteArrayOutputStream(512);

    long h = 0;
    try {

        if ((!java.io.Serializable.class.isAssignableFrom(cl)) ||
            (cl.isInterface())){
            return 0;
        }

        if (java.io.Externalizable.class.isAssignableFrom(cl)) {
            return 1;
        }

        MessageDigest md = MessageDigest.getInstance("SHA");
        DigestOutputStream mdo = new DigestOutputStream(devnull, md);
        DataOutputStream data = new DataOutputStream(mdo);

        //In the old case, for the caller class, the write Method wasn't considered
        // for rep-id calculations correctly, but for parent classes it was taken
        // into account.  That is the reason there is the klude of getting the write
        // Object method in there

        // Get SUID of parent
        Class<?> parent = cl.getSuperclass();
        if ((parent != null) && (parent != java.lang.Object.class)) {
            boolean hasWriteObjectFlag = false;
            Class [] args = {java.io.ObjectOutputStream.class};
            Method hasWriteObjectMethod = ObjectStreamClassUtil_1_3.getDeclaredMethod(parent, "writeObject", args,
                   Modifier.PRIVATE, Modifier.STATIC);
            if (hasWriteObjectMethod != null)
                hasWriteObjectFlag = true;
            data.writeLong(ObjectStreamClassUtil_1_3.computeStructuralUID(hasWriteObjectFlag, parent));
        }

        if (hasWriteObject)
            data.writeInt(2);
        else
            data.writeInt(1);

        /* Sort the field names to get a deterministic order */
        Field[] field = ObjectStreamClassUtil_1_3.getDeclaredFields(cl);
        Arrays.sort(field, compareMemberByName);

        for (int i = 0; i < field.length; i++) {
            Field f = field[i];

                            /* Include in the hash all fields except those that are
                             * transient or static.
                             */
            int m = f.getModifiers();
            if (Modifier.isTransient(m) || Modifier.isStatic(m))
                continue;

            data.writeUTF(f.getName());
            data.writeUTF(getSignature(f.getType()));
        }

        /* Compute the hash value for this class.
         * Use only the first 64 bits of the hash.
         */
        data.flush();
        byte hasharray[] = md.digest();
        int minimum = Math.min(8, hasharray.length);
        for (int i = minimum; i > 0; i--) {
            h += (long)(hasharray[i] & 255) << (i * 8);
        }
    } catch (IOException ignore) {
        /* can't happen, but be deterministic anyway. */
        h = -1;
    } catch (NoSuchAlgorithmException complain) {
        throw new SecurityException(complain.getMessage());
    }
    return h;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:77,代碼來源:ObjectStreamClassUtil_1_3.java


注:本文中的java.lang.reflect.Field.getModifiers方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。