本文整理匯總了Java中java.lang.reflect.Field類的典型用法代碼示例。如果您正苦於以下問題:Java Field類的具體用法?Java Field怎麽用?Java Field使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Field類屬於java.lang.reflect包,在下文中一共展示了Field類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createAndroidNClassLoader
import java.lang.reflect.Field; //導入依賴的package包/類
private static AndroidNClassLoader createAndroidNClassLoader(PathClassLoader originalClassLoader, Application application) throws Exception {
//let all element ""
final AndroidNClassLoader androidNClassLoader = new AndroidNClassLoader("", originalClassLoader, application);
final Field pathListField = ShareReflectUtil.findField(originalClassLoader, "pathList");
final Object originPathList = pathListField.get(originalClassLoader);
// To avoid 'dex file register with multiple classloader' exception on Android O, we must keep old
// dexPathList in original classloader so that after the newly loaded base dex was bound to
// AndroidNClassLoader we can still load class in base dex from original classloader.
Object newPathList = recreateDexPathList(originPathList, androidNClassLoader);
// Update new classloader's pathList.
pathListField.set(androidNClassLoader, newPathList);
return androidNClassLoader;
}
示例2: 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);
}
示例3: scan
import java.lang.reflect.Field; //導入依賴的package包/類
public PostProcessor[] scan(final Object instance) {
return FluentIterable
.from(ReflectionUtils.getDeclaredFields(instance.getClass(), true))
.filter(new Predicate<Field>() {
@Override
public boolean apply(Field input) {
return input.isAnnotationPresent(Random.PostProcessor.class)
&& PostProcessor.class.isAssignableFrom(input.getType());
}
})
.transform(new Function<Field, PostProcessor>() {
@Override
public PostProcessor apply(Field field) {
try {
if (!ReflectionUtils.makeFieldAccessible(field)) {
return null;
}
return (PostProcessor) field.get(instance);
} catch (IllegalAccessException e) {
throw new RandomitoException(e);
}
}
})
.filter(Predicates.<PostProcessor>notNull())
.toArray(PostProcessor.class);
}
示例4: scanField
import java.lang.reflect.Field; //導入依賴的package包/類
@Override
protected void scanField(Field field, long offset) {
Class<?> type = field.getType();
if (VALUE_CLASS.isAssignableFrom(type) && !CONSTANT_VALUE_CLASS.isAssignableFrom(type)) {
assert !Modifier.isFinal(field.getModifiers()) : "Value field must not be declared final because it is modified by register allocator: " + field;
OperandModeAnnotation annotation = getOperandModeAnnotation(field);
assert annotation != null : "Field must have operand mode annotation: " + field;
EnumSet<OperandFlag> flags = getFlags(field);
assert verifyFlags(field, type, flags);
annotation.values.add(new ValueFieldInfo(offset, field.getName(), type, field.getDeclaringClass(), flags));
annotation.directCount++;
} else if (VALUE_ARRAY_CLASS.isAssignableFrom(type)) {
OperandModeAnnotation annotation = getOperandModeAnnotation(field);
assert annotation != null : "Field must have operand mode annotation: " + field;
EnumSet<OperandFlag> flags = getFlags(field);
assert verifyFlags(field, type.getComponentType(), flags);
annotation.values.add(new ValueFieldInfo(offset, field.getName(), type, field.getDeclaringClass(), flags));
} else {
assert getOperandModeAnnotation(field) == null : "Field must not have operand mode annotation: " + field;
assert field.getAnnotation(LIRInstruction.State.class) == null : "Field must not have state annotation: " + field;
super.scanField(field, offset);
}
}
示例5: findFieldsNotAnnotatedWith
import java.lang.reflect.Field; //導入依賴的package包/類
public static SaveField[] findFieldsNotAnnotatedWith(Class<? extends Annotation> annotation, Class<?>... classes) {
List<SaveField> fields = new LinkedList<>();
int i = 0;
for (Class<?> clazz : classes) {
for (Field f : clazz.getDeclaredFields()) {
if (!f.isAnnotationPresent(annotation)) {
fields.add(new SaveField(f, i));
}
i++;
}
i = 0;
}
return fields.toArray(new SaveField[fields.size()]);
}
示例6: startEmbeddedActivity
import java.lang.reflect.Field; //導入依賴的package包/類
public EmbeddedActivityRecord startEmbeddedActivity(String bundleName) throws Exception{
EmbeddedActivityRecord activityRecord = new EmbeddedActivityRecord();
activityRecord.id = "embedded_"+mParent.getClass().getSimpleName();
Field mThemeResourceF = AndroidHack.findField(mParent,"mThemeResource");
int mThemeResource = (Integer)mThemeResourceF.get(mParent);
Intent intent = new Intent();
intent.setClassName(mParent,EmbeddedActivity.class.getName());
intent.putExtra("themeId",mThemeResource);
intent.putExtra("bundleName",bundleName);
ActivityInfo info = intent.resolveActivityInfo(mParent.getPackageManager(), PackageManager.GET_ACTIVITIES);
activityRecord.activity = (EmbeddedActivity) ActivityThread_startActivityNow.invoke(AndroidHack.getActivityThread(),
mParent, activityRecord.id, intent, info, activityRecord.activity, null, null);
activityRecord.activityInfo = info;
return activityRecord;
}
示例7: getField
import java.lang.reflect.Field; //導入依賴的package包/類
/**
* Get class field
*
* @param fieldName Field's name
* @param type Field's type class
* @param <V> Field's type
* @return {@link FieldWrapper} object or empty, if field wasn't found
*/
@Contract("null, null -> fail")
@SuppressWarnings("unchecked")
public <V> Optional<FieldWrapper<V>> getField(String fieldName, Class<V> type) {
/* Check arguments */
if(fieldName == null) throw new IllegalStateException("Field name shouldn't be null!");
if(type == null) throw new IllegalStateException("Field type shouldn't be null!");
/* Try to find cached field */
FieldInfo fieldInfo = new FieldInfo(fieldName, type);
/* Get field */
Integer found = FIELD_INDEX.get(fieldInfo);
Field[] field = new Field[] { found != null ? FIELD_CACHE.get(found) : null };
field[0] = field[0] != null ? field[0] : findDeclaredField(fieldName, type);
if(field[0] == null) return Optional.empty();
/* Wrap field */
return Optional.of((FieldWrapper<V>)FIELDWRAPPER_CACHE.computeIfAbsent(found,
k -> MethodHandleFieldWrapper.of(this, field[0], type)));
}
示例8: getProperties
import java.lang.reflect.Field; //導入依賴的package包/類
@Override
public List<Property> getProperties() {
if (proxy.properties == null) {
proxy.properties = new ArrayList<>();
proxy.propertyAccessors = new HashMap<>();
for (Field f : getAccessibleFields()) {
ODataProperty annotation = f.getAnnotation(ODataProperty.class);
if (annotation != null) {
String name = annotation.name();
RealAccessors<JpaOlingoEntity> accessors = new RealAccessors<>(this, f);
proxy.properties.add(new Property(null, name, annotation.valueType(), accessors.get(this)));
proxy.propertyAccessors.put(name, accessors);
}
}
}
return proxy.properties;
}
示例9: merge
import java.lang.reflect.Field; //導入依賴的package包/類
/**
* 合並pipeline參數設置
*/
public void merge(PipelineParameter pipelineParameter) {
try {
Field[] fields = this.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
// Skip static and final fields.
if (Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
continue;
}
ReflectionUtils.makeAccessible(field);
Object srcValue = field.get(pipelineParameter);
if (srcValue != null) { // 忽略null值
field.set(this, srcValue);
}
}
} catch (Exception e) {
// ignore
}
}
示例10: getIdTable
import java.lang.reflect.Field; //導入依賴的package包/類
protected Map<Integer, String> getIdTable(@RTypes String resName) throws IllegalAccessException, ClassNotFoundException {
// R資源類全稱
String rClassName = mPackageName + ".R$" + resName;
if (mResourceCache.hasIdTableCache(rClassName)) {
return mResourceCache.getIdTableCache(rClassName);
}
Class stringRClass = Class.forName(rClassName);
Field[] fields = stringRClass.getDeclaredFields();
Map<Integer, String> idTable = new HashMap<>();
for (Field field : fields) {
field.setAccessible(true);
int id = (int) field.get(null);
String name = field.getName();
idTable.put(id, name);
}
mResourceCache.cacheIdTable(rClassName, idTable);
return idTable;
}
示例11: getDomainIDClass
import java.lang.reflect.Field; //導入依賴的package包/類
/**
* 獲取實體類ID Class
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static Class<? extends Serializable> getDomainIDClass(Class<? extends BaseDomain> clazz){
/**
* 如果是聯合主鍵實體類則返回domainID,因為聯合主鍵類必須實現getDomainID()方法
*/
IdClass annotation = clazz.getAnnotation(IdClass.class);
if (annotation != null) {
return annotation.value();
}
try {
Field f = ReflectUtil.getDeclaredField(clazz, getDomainIDName(clazz));
if (f != null) {
return (Class<? extends Serializable>) f.getType();
}
} catch (Exception e) {
e.printStackTrace();
throw createFindIdException(clazz, e);
}
throw createIDNotfoundException(clazz);
}
示例12: init
import java.lang.reflect.Field; //導入依賴的package包/類
@PostConstruct
public void init() throws
IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Collection<SiteOption> siteOptionKeyValues = siteOptionRepository.findAll();
Map<String, Object> options = new Hashtable<>();
for (SiteOption siteOption : siteOptionKeyValues) {
options.put(siteOption.getName(), siteOption.getValue());
}
for (String key : options.keySet()) {
for (Field f : this.getClass().getDeclaredFields()) {
if (f.getName().toUpperCase().equals(key.toUpperCase())) {
setSiteOptionProperty(key, options.get(key));
}
}
}
}
示例13: closeBlocking
import java.lang.reflect.Field; //導入依賴的package包/類
@Override
public void closeBlocking() throws InterruptedException {
super.closeBlocking();
try {
Field channelField = this.getClass().getSuperclass().getDeclaredField("channel");
channelField.setAccessible(true);
SocketChannel channel = (SocketChannel) channelField.get(this);
if (channel != null && channel.isOpen()) {
Socket socket = channel.socket();
if (socket != null)
socket.close();
}
}
catch (Exception ex) {
System.out.println("Exception in Websocket close hack.");
ex.printStackTrace();
}
}
示例14: getHiddenFields
import java.lang.reflect.Field; //導入依賴的package包/類
public static Set<Field> getHiddenFields(final Class<?> clazz) {
final Field[] fields = clazz.getDeclaredFields();
final Set<Field> declaredFieldSet = CollectionUtils.arrayToSet(fields);
final Set<Field> ret = new HashSet<>();
Class<?> currentClass = clazz.getSuperclass();
while (currentClass != null) {
final Field[] superFields = currentClass.getDeclaredFields();
for (Field superField : superFields) {
final Field match = getMatch(declaredFieldSet, superField);
if (!Modifier.isPrivate(superField.getModifiers()) && !Modifier.isStatic(superField.getModifiers())
&& match != null) {
ret.add(match);
}
}
currentClass = currentClass.getSuperclass();
}
return ret;
}
示例15: setField
import java.lang.reflect.Field; //導入依賴的package包/類
/**
* Sets the field, bypassing scope restriction.
*
* @param subject Object where the field belongs
* @param fieldName name of the field to set
* @param value value to set to the field.
* @param <T> subject type
* @param <U> value type
* @throws TestUtilsException if there are reflection errors while setting
* the field
*/
public static <T, U> void setField(T subject, String fieldName, U value)
throws TestUtilsException {
@SuppressWarnings("unchecked")
Class clazz = subject.getClass();
try {
while (clazz != null) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(subject, value);
break;
} catch (NoSuchFieldException ex) {
if (clazz == clazz.getSuperclass()) {
break;
}
clazz = clazz.getSuperclass();
}
}
} catch (SecurityException | IllegalArgumentException |
IllegalAccessException e) {
throw new TestUtilsException("setField failed", e);
}
}