Search in sources :

Example 46 with AccessibleObject

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;
}
Also used : NameNotFoundException(org.apache.geode.cache.query.NameNotFoundException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AccessibleObject(java.lang.reflect.AccessibleObject) Member(java.lang.reflect.Member)

Example 47 with AccessibleObject

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;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) AccessibleObject(java.lang.reflect.AccessibleObject)

Example 48 with AccessibleObject

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());
}
Also used : Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) PrivilegedActionException(java.security.PrivilegedActionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject)

Example 49 with AccessibleObject

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;
}
Also used : Optional(org.qi4j.api.common.Optional) MetaInfo(org.qi4j.api.common.MetaInfo) PropertyModel(org.qi4j.runtime.property.PropertyModel) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) AccessibleObject(java.lang.reflect.AccessibleObject) ValueConstraintsInstance(org.qi4j.runtime.composite.ValueConstraintsInstance) Annotation(java.lang.annotation.Annotation)

Example 50 with AccessibleObject

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;
}
Also used : UsesInstance(org.qi4j.runtime.composite.UsesInstance) MixinModel(org.qi4j.runtime.composite.MixinModel) HashMap(java.util.HashMap) PropertyModel(org.qi4j.runtime.property.PropertyModel) PropertyInstance(org.qi4j.runtime.property.PropertyInstance) InjectionContext(org.qi4j.runtime.injection.InjectionContext) TransientStateInstance(org.qi4j.runtime.composite.TransientStateInstance) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject) Property(org.qi4j.api.property.Property)

Aggregations

AccessibleObject (java.lang.reflect.AccessibleObject)54 ArrayList (java.util.ArrayList)17 Method (java.lang.reflect.Method)14 Field (java.lang.reflect.Field)9 Annotation (java.lang.annotation.Annotation)8 PropertyModel (org.qi4j.runtime.property.PropertyModel)6 HashSet (java.util.HashSet)5 MethodInvocation (org.aopalliance.intercept.MethodInvocation)4 Property (org.qi4j.api.property.Property)4 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 BuilderEntityState (org.qi4j.runtime.unitofwork.BuilderEntityState)3 DynamicFinder (com.google.inject.persist.finder.DynamicFinder)2 Finder (com.google.inject.persist.finder.Finder)2 Constructor (java.lang.reflect.Constructor)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 DeploymentReflectionIndex (org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex)2 ResourceInjectionTargetMetaData (org.jboss.metadata.javaee.spec.ResourceInjectionTargetMetaData)2 Module (org.jboss.modules.Module)2