Search in sources :

Example 1 with Finder

use of com.google.inject.persist.finder.Finder in project guice by google.

the class JpaFinderProxy method getFinderDescriptor.

private JpaFinderProxy.FinderDescriptor getFinderDescriptor(MethodInvocation invocation) {
    Method method = invocation.getMethod();
    JpaFinderProxy.FinderDescriptor finderDescriptor = finderCache.get(method);
    if (null != finderDescriptor) {
        return finderDescriptor;
    }
    //otherwise reflect and cache finder info...
    finderDescriptor = new JpaFinderProxy.FinderDescriptor();
    //determine return type
    finderDescriptor.returnClass = invocation.getMethod().getReturnType();
    finderDescriptor.returnType = determineReturnType(finderDescriptor.returnClass);
    //determine finder query characteristics
    Finder finder = invocation.getMethod().getAnnotation(Finder.class);
    String query = finder.query();
    if (!"".equals(query.trim())) {
        finderDescriptor.setQuery(query);
    } else {
        finderDescriptor.setNamedQuery(finder.namedQuery());
    }
    //determine parameter annotations
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    Object[] discoveredAnnotations = new Object[parameterAnnotations.length];
    for (int i = 0; i < parameterAnnotations.length; i++) {
        Annotation[] annotations = parameterAnnotations[i];
        //each annotation per param
        for (Annotation annotation : annotations) {
            //discover the named, first or max annotations then break out
            Class<? extends Annotation> annotationType = annotation.annotationType();
            if (Named.class.equals(annotationType) || javax.inject.Named.class.equals(annotationType)) {
                discoveredAnnotations[i] = annotation;
                finderDescriptor.isBindAsRawParameters = false;
                break;
            } else if (FirstResult.class.equals(annotationType)) {
                discoveredAnnotations[i] = annotation;
                break;
            } else if (MaxResults.class.equals(annotationType)) {
                discoveredAnnotations[i] = annotation;
                break;
            }
        //leave as null for no binding
        }
    }
    //set the discovered set to our finder cache object
    finderDescriptor.parameterAnnotations = discoveredAnnotations;
    //discover the returned collection implementation if this finder returns a collection
    if (JpaFinderProxy.ReturnType.COLLECTION.equals(finderDescriptor.returnType) && finderDescriptor.returnClass != Collection.class) {
        finderDescriptor.returnCollectionType = finder.returnAs();
        try {
            finderDescriptor.returnCollectionTypeConstructor = finderDescriptor.returnCollectionType.getConstructor();
            //UGH!
            finderDescriptor.returnCollectionTypeConstructor.setAccessible(true);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("Finder's collection return type specified has no default constructor! returnAs: " + finderDescriptor.returnCollectionType, e);
        }
    }
    //cache it
    cacheFinderDescriptor(method, finderDescriptor);
    return finderDescriptor;
}
Also used : Named(com.google.inject.name.Named) FirstResult(com.google.inject.persist.finder.FirstResult) Finder(com.google.inject.persist.finder.Finder) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Collection(java.util.Collection)

Example 2 with Finder

use of com.google.inject.persist.finder.Finder in project guice by google.

the class JpaPersistModule method bindFinder.

private <T> void bindFinder(Class<T> iface) {
    if (!isDynamicFinderValid(iface)) {
        return;
    }
    InvocationHandler finderInvoker = new InvocationHandler() {

        @Inject
        JpaFinderProxy finderProxy;

        @Override
        public Object invoke(final Object thisObject, final Method method, final Object[] args) throws Throwable {
            // Don't intercept non-finder methods like equals and hashcode.
            if (!method.isAnnotationPresent(Finder.class)) {
                // and hashcode as a proxy (!) for the proxy's equals and hashcode.
                return method.invoke(this, args);
            }
            return finderProxy.invoke(new MethodInvocation() {

                @Override
                public Method getMethod() {
                    return method;
                }

                @Override
                public Object[] getArguments() {
                    return null == args ? new Object[0] : args;
                }

                @Override
                public Object proceed() throws Throwable {
                    return method.invoke(thisObject, args);
                }

                @Override
                public Object getThis() {
                    throw new UnsupportedOperationException("Bottomless proxies don't expose a this.");
                }

                @Override
                public AccessibleObject getStaticPart() {
                    throw new UnsupportedOperationException();
                }
            });
        }
    };
    requestInjection(finderInvoker);
    // Proxy must produce instance of type given.
    @SuppressWarnings("unchecked") T proxy = (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { iface }, finderInvoker);
    bind(iface).toInstance(proxy);
}
Also used : Finder(com.google.inject.persist.finder.Finder) DynamicFinder(com.google.inject.persist.finder.DynamicFinder) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject)

Example 3 with Finder

use of com.google.inject.persist.finder.Finder in project roboguice by roboguice.

the class JpaFinderProxy method getFinderDescriptor.

private JpaFinderProxy.FinderDescriptor getFinderDescriptor(MethodInvocation invocation) {
    Method method = invocation.getMethod();
    JpaFinderProxy.FinderDescriptor finderDescriptor = finderCache.get(method);
    if (null != finderDescriptor) {
        return finderDescriptor;
    }
    //otherwise reflect and cache finder info...
    finderDescriptor = new JpaFinderProxy.FinderDescriptor();
    //determine return type
    finderDescriptor.returnClass = invocation.getMethod().getReturnType();
    finderDescriptor.returnType = determineReturnType(finderDescriptor.returnClass);
    //determine finder query characteristics
    Finder finder = invocation.getMethod().getAnnotation(Finder.class);
    String query = finder.query();
    if (!"".equals(query.trim())) {
        finderDescriptor.setQuery(query);
    } else {
        finderDescriptor.setNamedQuery(finder.namedQuery());
    }
    //determine parameter annotations
    Annotation[][] parameterAnnotations = method.getParameterAnnotations();
    Object[] discoveredAnnotations = new Object[parameterAnnotations.length];
    for (int i = 0; i < parameterAnnotations.length; i++) {
        Annotation[] annotations = parameterAnnotations[i];
        //each annotation per param
        for (Annotation annotation : annotations) {
            //discover the named, first or max annotations then break out
            Class<? extends Annotation> annotationType = annotation.annotationType();
            if (Named.class.equals(annotationType) || javax.inject.Named.class.equals(annotationType)) {
                discoveredAnnotations[i] = annotation;
                finderDescriptor.isBindAsRawParameters = false;
                break;
            } else if (FirstResult.class.equals(annotationType)) {
                discoveredAnnotations[i] = annotation;
                break;
            } else if (MaxResults.class.equals(annotationType)) {
                discoveredAnnotations[i] = annotation;
                break;
            }
        //leave as null for no binding
        }
    }
    //set the discovered set to our finder cache object
    finderDescriptor.parameterAnnotations = discoveredAnnotations;
    //discover the returned collection implementation if this finder returns a collection
    if (JpaFinderProxy.ReturnType.COLLECTION.equals(finderDescriptor.returnType) && finderDescriptor.returnClass != Collection.class) {
        finderDescriptor.returnCollectionType = finder.returnAs();
        try {
            finderDescriptor.returnCollectionTypeConstructor = finderDescriptor.returnCollectionType.getConstructor();
            //UGH!
            finderDescriptor.returnCollectionTypeConstructor.setAccessible(true);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("Finder's collection return type specified has no default constructor! returnAs: " + finderDescriptor.returnCollectionType, e);
        }
    }
    //cache it
    cacheFinderDescriptor(method, finderDescriptor);
    return finderDescriptor;
}
Also used : Named(com.google.inject.name.Named) FirstResult(com.google.inject.persist.finder.FirstResult) Finder(com.google.inject.persist.finder.Finder) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Collection(java.util.Collection)

Example 4 with Finder

use of com.google.inject.persist.finder.Finder in project roboguice by roboguice.

the class JpaPersistModule method bindFinder.

private <T> void bindFinder(Class<T> iface) {
    if (!isDynamicFinderValid(iface)) {
        return;
    }
    InvocationHandler finderInvoker = new InvocationHandler() {

        @Inject
        JpaFinderProxy finderProxy;

        public Object invoke(final Object thisObject, final Method method, final Object[] args) throws Throwable {
            // Don't intercept non-finder methods like equals and hashcode.
            if (!method.isAnnotationPresent(Finder.class)) {
                // and hashcode as a proxy (!) for the proxy's equals and hashcode. 
                return method.invoke(this, args);
            }
            return finderProxy.invoke(new MethodInvocation() {

                public Method getMethod() {
                    return method;
                }

                public Object[] getArguments() {
                    return null == args ? new Object[0] : args;
                }

                public Object proceed() throws Throwable {
                    return method.invoke(thisObject, args);
                }

                public Object getThis() {
                    throw new UnsupportedOperationException("Bottomless proxies don't expose a this.");
                }

                public AccessibleObject getStaticPart() {
                    throw new UnsupportedOperationException();
                }
            });
        }
    };
    requestInjection(finderInvoker);
    // Proxy must produce instance of type given.
    @SuppressWarnings("unchecked") T proxy = (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { iface }, finderInvoker);
    bind(iface).toInstance(proxy);
}
Also used : Finder(com.google.inject.persist.finder.Finder) DynamicFinder(com.google.inject.persist.finder.DynamicFinder) AccessibleObject(java.lang.reflect.AccessibleObject) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Aggregations

Finder (com.google.inject.persist.finder.Finder)4 Method (java.lang.reflect.Method)4 Named (com.google.inject.name.Named)2 DynamicFinder (com.google.inject.persist.finder.DynamicFinder)2 FirstResult (com.google.inject.persist.finder.FirstResult)2 Annotation (java.lang.annotation.Annotation)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 Collection (java.util.Collection)2 MethodInvocation (org.aopalliance.intercept.MethodInvocation)2