Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.skife.jdbi.v2.sqlobject;
  
  import  com.fasterxml.classmate.ResolvedType;
  import  com.fasterxml.classmate.TypeBindings;
  import  com.fasterxml.classmate.members.ResolvedMethod;
 
 import java.util.List;
 
 abstract class ResultReturnThing
 {
     public Object map(ResolvedMethod methodQuery qHandleDing h)
     {
         if (method.getRawMember().isAnnotationPresent(Mapper.class)) {
             final ResultSetMapper mapper;
             try {
                 mapper = method.getRawMember().getAnnotation(Mapper.class).value().newInstance();
             }
             catch (Exception e) {
                 throw new UnableToCreateStatementException("unable to access mapper"e);
             }
             return result(q.map(mapper), h);
         }
         else {
             return result(q.mapTo(mapTo(method)), h);
         }
     }
 
     static ResultReturnThing forType(ResolvedMethod method)
     {
         ResolvedType return_type = method.getReturnType();
         if (return_type == null) {
             throw new IllegalStateException(String.format(
                     "Method %s#%s is annotated as if it should return a value, but the method is void.",
                     method.getDeclaringType().getErasedType().getName(),
                     method.getName()));
         } else if (return_type.isInstanceOf(ResultBearing.class)) {
             return new ResultBearingResultReturnThing(method);
         }
         else if (return_type.isInstanceOf(Iterable.class)) {
             return new IterableReturningThing(method);
         }
         else if (return_type.isInstanceOf(Iterator.class)) {
             return new IteratorResultReturnThing(method);
         }
         else {
             return new SingleValueResultReturnThing(method);
         }
     }
 
     protected abstract Object result(ResultBearing qHandleDing baton);
 
     protected abstract Class<?> mapTo(ResolvedMethod method);
 
 
     static class SingleValueResultReturnThing extends ResultReturnThing
     {
         private final Class<?> returnType;
         private final Class<?> containerType;
 
         public SingleValueResultReturnThing(ResolvedMethod method)
         {
             if (method.getRawMember().isAnnotationPresent(SingleValueResult.class)) {
                 SingleValueResult svr = method.getRawMember().getAnnotation(SingleValueResult.class);
                 // try to guess generic type
                 if(SingleValueResult.Default.class == svr.value()){
                     TypeBindings typeBindings = method.getReturnType().getTypeBindings();
                     if(typeBindings.size() == 1){
                         this. = typeBindings.getBoundType(0).getErasedType();
                     }else{
                         throw new IllegalArgumentException("Ambiguous generic information. SingleValueResult type could not be fetched.");
                     }
 
                 }else{
                     this. = svr.value();
                 }
                 this. = method.getReturnType().getErasedType();
             }
             else {
                 this. = method.getReturnType().getErasedType();
                 this. = null;
             }
 
         }
 
         @Override
         protected Object result(ResultBearing qHandleDing baton)
         {
             return null ==  ? q.first() : q.first();
         }
 
         @Override
        protected Class<?> mapTo(ResolvedMethod method)
        {
            return ;
        }
    }
    static class ResultBearingResultReturnThing extends ResultReturnThing
    {
        private final ResolvedType resolvedType;
        public ResultBearingResultReturnThing(ResolvedMethod method)
        {
            // extract T from Query<T>
            ResolvedType query_type = method.getReturnType();
            List<ResolvedType> query_return_types = query_type.typeParametersFor(org.skife.jdbi.v2.Query.class);
            this. = query_return_types.get(0);
        }
        @Override
        protected Object result(ResultBearing qHandleDing baton)
        {
            return q;
        }
        @Override
        protected Class<?> mapTo(ResolvedMethod method)
        {
            return .getErasedType();
        }
    }
    static class IteratorResultReturnThing extends ResultReturnThing
    {
        private final ResolvedType resolvedType;
        public IteratorResultReturnThing(ResolvedMethod method)
        {
            ResolvedType query_type = method.getReturnType();
            List<ResolvedType> query_return_types = query_type.typeParametersFor(Iterator.class);
            this. = query_return_types.get(0);
        }
        @Override
        protected Object result(ResultBearing qfinal HandleDing baton)
        {
            baton.retain("iterator");
            final ResultIterator itty = q.iterator();
            return new ResultIterator()
            {
                public void close()
                {
                    itty.close();
                }
                public boolean hasNext()
                {
                    boolean has_next = itty.hasNext();
                    if (!has_next) {
                        baton.release("iterator");
                    }
                    return itty.hasNext();
                }
                public Object next()
                {
                    Object rs = itty.next();
                    boolean has_next = itty.hasNext();
                    if (!has_next) {
                        baton.release("iterator");
                    }
                    return rs;
                }
                public void remove()
                {
                    itty.remove();
                }
            };
        }
        @Override
        protected Class<?> mapTo(ResolvedMethod method)
        {
            return .getErasedType();
        }
    }
    static class IterableReturningThing extends ResultReturnThing
    {
        private final ResolvedType resolvedType;
        private final Class<?> erased_type;
        public IterableReturningThing(ResolvedMethod method)
        {
            // extract T from List<T>
            ResolvedType query_type = method.getReturnType();
            List<ResolvedType> query_return_types = query_type.typeParametersFor(Iterable.class);
            this. = query_return_types.get(0);
             = method.getReturnType().getErasedType();
        }
        @Override
        protected Object result(ResultBearing qHandleDing baton)
        {
            return q.list();
        }
        @Override
        protected Class<?> mapTo(ResolvedMethod method)
        {
            return .getErasedType();
        }
    }
New to GrepCode? Check out our FAQ X