injectableproviderfactory.java

来自「resetful样式的ws样例,一种面向资源的webservices服务」· Java 代码 · 共 470 行 · 第 1/2 页

JAVA
470
字号
        if (p.getAnnotation() == null) return null;                ComponentContext ic = new AnnotationObjectContext(p.getAnnotations());                // Find a per request injectable with Parameter        Injectable i = getInjectable(p.getAnnotation().annotationType(), ic, p.getAnnotation(),                 p, Scope.PerRequest);        if (i != null) return i;                // Find a per request, undefined or singleton injectable with parameter Type        return getInjectable(                p.getAnnotation().annotationType(),                 ic,                 p.getAnnotation(),                 p.getParameterType(),                Arrays.asList(Scope.PerRequest, Scope.Undefined, Scope.Singleton)                );    }        public List<Injectable> getInjectable(List<Parameter> ps) {        List<Injectable> is = new ArrayList<Injectable>();        for (Parameter p : ps)            is.add(getInjectable(p));                return is;    }            public static final class AccessibleObjectContext implements ComponentContext {        private AccessibleObject accesibleObject;                private Annotation[] annotations;                public AccessibleObjectContext() { }                public AccessibleObjectContext(AccessibleObject ao) {            this.accesibleObject = ao;        }                public AccessibleObjectContext(AccessibleObject ao, Annotation[] annotations) {            this.accesibleObject = ao;            this.annotations = annotations;        }                public void setAccesibleObject(AccessibleObject ao) {            this.accesibleObject = ao;        }                public void setAccesibleObject(AccessibleObject ao, Annotation[] annotations) {            this.accesibleObject = ao;            this.annotations = annotations;        }                // ComponentContext                public AccessibleObject getAccesibleObject() {            return accesibleObject;        }            public Annotation[] getAnnotations() {            if (annotations != null) return annotations;            return accesibleObject.getAnnotations();        }    }        public static final class AnnotationObjectContext implements ComponentContext {        private Annotation[] annotations;                public AnnotationObjectContext() { }                public AnnotationObjectContext(Annotation[] annotations) {            this.annotations = annotations;                    }                public void setAnnotations(Annotation[] annotations) {            this.annotations = annotations;        }                // ComponentContext                public AccessibleObject getAccesibleObject() {            return null;        }            public Annotation[] getAnnotations() {            return annotations;        }    }        //        /**     * Inject onto a singleton provider.     *      * @param o the singleton instance     */    public void injectResources(final Object o) {        AccessibleObjectContext aoc = new AccessibleObjectContext();                Class oClass = o.getClass();        while (oClass != Object.class) {            for (final Field f : oClass.getDeclaredFields()) {                if (getFieldValue(o, f) != null) continue;                                aoc.setAccesibleObject(f);                final Annotation[] as = f.getAnnotations();                for (Annotation a : as) {                    Injectable i = getInjectable(                            a.annotationType(), aoc, a, f.getGenericType(),                             Arrays.asList(Scope.Singleton, Scope.Undefined));                    if (i != null) {                        setFieldValue(o, f, i.getValue(null));                    }                }                            }            oClass = oClass.getSuperclass();        }                MethodList ml = new MethodList(o.getClass().getMethods());        for (AnnotatedMethod m : ml.                hasNotMetaAnnotation(HttpMethod.class).                hasNotAnnotation(Path.class).                hasNumParams(1).                hasReturnType(void.class).                nameStartsWith("set")) {            final Annotation[] as = m.getAnnotations();            aoc.setAccesibleObject(m.getMethod(), as);            final Type t = m.getGenericParameterTypes()[0];            for (Annotation a : as) {                Injectable i = getInjectable(                        a.annotationType(), aoc, a, t,                         Arrays.asList(Scope.Singleton, Scope.Undefined));                if (i != null) {                    setMethodValue(o, m, i.getValue(null));                }            }        }            }        private void setFieldValue(final Object resource, final Field f, final Object value) {        AccessController.doPrivileged(new PrivilegedAction<Object>() {            public Object run() {                try {                    if (!f.isAccessible()) {                        f.setAccessible(true);                    }                    f.set(resource, value);                    return null;                } catch (IllegalAccessException e) {                    throw new ContainerException(e);                }            }        });    }        private Object getFieldValue(final Object resource, final Field f) {        return AccessController.doPrivileged(new PrivilegedAction<Object>() {            public Object run() {                try {                    if (!f.isAccessible()) {                        f.setAccessible(true);                    }                    return f.get(resource);                } catch (IllegalAccessException e) {                    throw new ContainerException(e);                }            }        });    }    private void setMethodValue(Object o, AnnotatedMethod m, Object value) {        try {            m.getMethod().invoke(o, value);        } catch (Exception ex) {            throw new ContainerException(ex);        }    }        public static class ConstructorInjectablePair<T> {        Constructor<T> con;        List<Injectable> is;                ConstructorInjectablePair(Constructor<T> con, List<Injectable> is) {            this.con = con;            this.is = is;        }    }        /**     * Get the most suitable constructor. The constructor with the most     * parameters and that has the most parameters associated with      * Injectable instances will be chosen.     *      * @param c the class to instantiate     * @return a constructor and list of injectables for the constructor      *         parameters.     */    @SuppressWarnings("unchecked")    public <T> ConstructorInjectablePair<T> getConstructor(Class<T> c) {        if (c.getConstructors().length == 0)            return null;                SortedSet<ConstructorInjectablePair<T>> cs = new TreeSet<ConstructorInjectablePair<T>>(                new Comparator<ConstructorInjectablePair<T>>() {            public int compare(ConstructorInjectablePair<T> o1, ConstructorInjectablePair<T> o2) {                int p = Collections.frequency(o1.is, null) - Collections.frequency(o2.is, null);                if (p != 0)                    return p;                                return o2.con.getParameterTypes().length - o1.con.getParameterTypes().length;            }        });                AnnotationObjectContext aoc = new AnnotationObjectContext();        for (Constructor con : c.getConstructors()) {            List<Injectable> is = new ArrayList<Injectable>();            int ps = con.getParameterTypes().length;            for (int p = 0; p < ps; p++) {                Type pgtype = con.getGenericParameterTypes()[p];                Annotation[] as = con.getParameterAnnotations()[p];                aoc.setAnnotations(as);                Injectable i = null;                for (Annotation a : as) {                    i = getInjectable(                            a.annotationType(), aoc, a, pgtype,                             Arrays.asList(Scope.Singleton, Scope.Undefined));                }                is.add(i);            }            cs.add(new ConstructorInjectablePair<T>(con, is));        }                        return cs.first();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?