use of java.lang.reflect.AccessibleObject in project geode by apache.
the class AttributeDescriptor method getReadMember.
Member getReadMember(Class targetClass) throws NameNotFoundException {
// mapping: public field (same name), method (getAttribute()),
// method (attribute())
List key = new ArrayList();
key.add(targetClass);
key.add(_name);
Member m = (Member) _cache.get(key);
if (m != null)
return m;
m = getReadField(targetClass);
if (m == null)
m = getReadMethod(targetClass);
if (m != null)
_cache.putIfAbsent(key, m);
else
throw new NameNotFoundException(LocalizedStrings.AttributeDescriptor_NO_PUBLIC_ATTRIBUTE_NAMED_0_WAS_FOUND_IN_CLASS_1.toLocalizedString(new Object[] { _name, targetClass.getName() }));
// override security for nonpublic derived classes with public members
((AccessibleObject) m).setAccessible(true);
return m;
}
use of java.lang.reflect.AccessibleObject in project android_frameworks_base by ResurrectionRemix.
the class ViewDebug method getExportedPropertyFields.
private static Field[] getExportedPropertyFields(Class<?> klass) {
if (sFieldsForClasses == null) {
sFieldsForClasses = new HashMap<Class<?>, Field[]>();
}
if (sAnnotations == null) {
sAnnotations = new HashMap<AccessibleObject, ExportedProperty>(512);
}
final HashMap<Class<?>, Field[]> map = sFieldsForClasses;
Field[] fields = map.get(klass);
if (fields != null) {
return fields;
}
try {
final Field[] declaredFields = klass.getDeclaredFieldsUnchecked(false);
final ArrayList<Field> foundFields = new ArrayList<Field>();
for (final Field field : declaredFields) {
// Fields which can't be resolved have a null type.
if (field.getType() != null && field.isAnnotationPresent(ExportedProperty.class)) {
field.setAccessible(true);
foundFields.add(field);
sAnnotations.put(field, field.getAnnotation(ExportedProperty.class));
}
}
fields = foundFields.toArray(new Field[foundFields.size()]);
map.put(klass, fields);
} catch (NoClassDefFoundError e) {
throw new AssertionError(e);
}
return fields;
}
use of java.lang.reflect.AccessibleObject in project jdk8u_jdk by JetBrains.
the class Statement method invokeInternal.
private Object invokeInternal() throws Exception {
Object target = getTarget();
String methodName = getMethodName();
if (target == null || methodName == null) {
throw new NullPointerException((target == null ? "target" : "methodName") + " should not be null");
}
Object[] arguments = getArguments();
if (arguments == null) {
arguments = emptyArray;
}
// case this method.
if (target == Class.class && methodName.equals("forName")) {
return ClassFinder.resolveClass((String) arguments[0], this.loader);
}
Class<?>[] argClasses = new Class<?>[arguments.length];
for (int i = 0; i < arguments.length; i++) {
argClasses[i] = (arguments[i] == null) ? null : arguments[i].getClass();
}
AccessibleObject m = null;
if (target instanceof Class) {
/*
For class methods, simluate the effect of a meta class
by taking the union of the static methods of the
actual class, with the instance methods of "Class.class"
and the overloaded "newInstance" methods defined by the
constructors.
This way "System.class", for example, will perform both
the static method getProperties() and the instance method
getSuperclass() defined in "Class.class".
*/
if (methodName.equals("new")) {
methodName = "newInstance";
}
// Provide a short form for array instantiation by faking an nary-constructor.
if (methodName.equals("newInstance") && ((Class) target).isArray()) {
Object result = Array.newInstance(((Class) target).getComponentType(), arguments.length);
for (int i = 0; i < arguments.length; i++) {
Array.set(result, i, arguments[i]);
}
return result;
}
if (methodName.equals("newInstance") && arguments.length != 0) {
// ignored elsewhere.
if (target == Character.class && arguments.length == 1 && argClasses[0] == String.class) {
return new Character(((String) arguments[0]).charAt(0));
}
try {
m = ConstructorFinder.findConstructor((Class) target, argClasses);
} catch (NoSuchMethodException exception) {
m = null;
}
}
if (m == null && target != Class.class) {
m = getMethod((Class) target, methodName, argClasses);
}
if (m == null) {
m = getMethod(Class.class, methodName, argClasses);
}
} else {
/*
This special casing of arrays is not necessary, but makes files
involving arrays much shorter and simplifies the archiving infrastrcure.
The Array.set() method introduces an unusual idea - that of a static method
changing the state of an instance. Normally statements with side
effects on objects are instance methods of the objects themselves
and we reinstate this rule (perhaps temporarily) by special-casing arrays.
*/
if (target.getClass().isArray() && (methodName.equals("set") || methodName.equals("get"))) {
int index = ((Integer) arguments[0]).intValue();
if (methodName.equals("get")) {
return Array.get(target, index);
} else {
Array.set(target, index, arguments[1]);
return null;
}
}
m = getMethod(target.getClass(), methodName, argClasses);
}
if (m != null) {
try {
if (m instanceof Method) {
return MethodUtil.invoke((Method) m, target, arguments);
} else {
return ((Constructor) m).newInstance(arguments);
}
} catch (IllegalAccessException iae) {
throw new Exception("Statement cannot invoke: " + methodName + " on " + target.getClass(), iae);
} catch (InvocationTargetException ite) {
Throwable te = ite.getTargetException();
if (te instanceof Exception) {
throw (Exception) te;
} else {
throw ite;
}
}
}
throw new NoSuchMethodException(toString());
}
use of java.lang.reflect.AccessibleObject in project qi4j-sdk by Qi4j.
the class EntityAssemblyImpl method newPropertyModel.
@Override
protected PropertyModel newPropertyModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
boolean optional = first(filter(isType(Optional.class), annotations)) != null;
ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericPropertyInfo.propertyTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
ValueConstraintsInstance valueConstraintsInstance = null;
if (valueConstraintsModel.isConstrained()) {
valueConstraintsInstance = valueConstraintsModel.newInstance();
}
MetaInfo metaInfo = stateDeclarations.metaInfoFor(accessor);
Object defaultValue = stateDeclarations.initialValueOf(accessor);
boolean useDefaults = metaInfo.get(UseDefaults.class) != null || stateDeclarations.useDefaults(accessor);
boolean immutable = this.immutable || metaInfo.get(Immutable.class) != null;
PropertyModel propertyModel = new PropertyModel(accessor, immutable, useDefaults, valueConstraintsInstance, metaInfo, defaultValue);
return propertyModel;
}
use of java.lang.reflect.AccessibleObject in project qi4j-sdk by Qi4j.
the class ServiceModel method newInstance.
public ServiceInstance newInstance(final ModuleInstance module) {
Object[] mixins = mixinsModel.newMixinHolder();
Map<AccessibleObject, Property<?>> properties = new HashMap<>();
for (PropertyModel propertyModel : stateModel.properties()) {
Object initialValue = propertyModel.initialValue(module);
if (propertyModel.accessor().equals(identityMethod)) {
initialValue = identity;
}
Property<?> property = new PropertyInstance<>(propertyModel, initialValue);
properties.put(propertyModel.accessor(), property);
}
TransientStateInstance state = new TransientStateInstance(properties);
ServiceInstance compositeInstance = new ServiceInstance(this, module, mixins, state);
// Instantiate all mixins
int i = 0;
UsesInstance uses = UsesInstance.EMPTY_USES.use(this);
InjectionContext injectionContext = new InjectionContext(compositeInstance, uses, state);
for (MixinModel mixinModel : mixinsModel.mixinModels()) {
mixins[i++] = mixinModel.newInstance(injectionContext);
}
return compositeInstance;
}
Aggregations