Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.skife.jdbi.v2.sqlobject;
  
  import  com.fasterxml.classmate.MemberResolver;
  import  com.fasterxml.classmate.ResolvedType;
  import  com.fasterxml.classmate.ResolvedTypeWithMembers;
  import  com.fasterxml.classmate.TypeResolver;
  import  com.fasterxml.classmate.members.ResolvedMethod;
  import  net.sf.cglib.proxy.Enhancer;
  import  net.sf.cglib.proxy.Factory;
 import  net.sf.cglib.proxy.MethodInterceptor;
 import  net.sf.cglib.proxy.MethodProxy;
 
 import java.util.List;
 import java.util.Map;
 
 class SqlObject
 {
     private static final TypeResolver                                  typeResolver  = new TypeResolver();
     private static final Map<MethodHandler>                          mixinHandlers = new HashMap<MethodHandler>();
     private static final ConcurrentMap<Class<?>, Map<MethodHandler>> handlersCache = new ConcurrentHashMap<Class<?>, Map<MethodHandler>>();
     private static final ConcurrentMap<Class<?>, Factory>              factories     = new ConcurrentHashMap<Class<?>, Factory>();
 
     static {
         .putAll(TransactionalHelper.handlers());
         .putAll(GetHandleHelper.handlers());
         .putAll(TransmogrifierHelper.handlers());
     }
 
     @SuppressWarnings("unchecked")
     static <T> T buildSqlObject(final Class<T> sqlObjectTypefinal HandleDing handle)
     {
         Factory f;
         if (.containsKey(sqlObjectType)) {
             f = .get(sqlObjectType);
         }
         else {
             Enhancer e = new Enhancer();
             e.setClassLoader(sqlObjectType.getClassLoader());
 
             List<Classinterfaces = new ArrayList<Class>();
             interfaces.add(CloseInternalDoNotUseThisClass.class);
             if (sqlObjectType.isInterface()) {
                 interfaces.add(sqlObjectType);
             }
             else {
                 e.setSuperclass(sqlObjectType);
             }
             e.setInterfaces(interfaces.toArray(new Class[interfaces.size()]));
             final SqlObject so = new SqlObject(buildHandlersFor(sqlObjectType), handle);
             e.setCallback(new MethodInterceptor()
             {
                 @Override
                 public Object intercept(Object oMethod methodObject[] objects, MethodProxy methodProxythrows Throwable
                 {
                     return so.invoke(omethodobjectsmethodProxy);
                 }
             });
             T t = (T) e.create();
             .putIfAbsent(sqlObjectType, (Factory) t);
             return t;
         }
 
         final SqlObject so = new SqlObject(buildHandlersFor(sqlObjectType), handle);
         return (T) f.newInstance(new MethodInterceptor()
         {
             @Override
             public Object intercept(Object oMethod methodObject[] objects, MethodProxy methodProxythrows Throwable
             {
                 return so.invoke(omethodobjectsmethodProxy);
             }
         });
     }
 
     private static Map<MethodHandlerbuildHandlersFor(Class<?> sqlObjectType)
     {
         if (.containsKey(sqlObjectType)) {
             return .get(sqlObjectType);
         }
 
         final MemberResolver mr = new MemberResolver();
         final ResolvedType sql_object_type = .resolve(sqlObjectType);
 
         final ResolvedTypeWithMembers d = mr.resolve(sql_object_typenullnull);
 
         final Map<MethodHandlerhandlers = new HashMap<MethodHandler>();
         for (final ResolvedMethod method : d.getMemberMethods()) {
             final Method raw_method = method.getRawMember();
 
             if (raw_method.isAnnotationPresent(SqlQuery.class)) {
                 handlers.put(raw_methodnew QueryHandler(sqlObjectTypemethod, ResultReturnThing.forType(method)));
             }
             else if (raw_method.isAnnotationPresent(SqlUpdate.class)) {
                 handlers.put(raw_methodnew UpdateHandler(sqlObjectTypemethod));
             }
            else if (raw_method.isAnnotationPresent(SqlBatch.class)) {
                handlers.put(raw_methodnew BatchHandler(sqlObjectTypemethod));
            }
            else if (raw_method.isAnnotationPresent(SqlCall.class)) {
                handlers.put(raw_methodnew CallHandler(sqlObjectTypemethod));
            }
            else if(raw_method.isAnnotationPresent(CreateSqlObject.class)) {
                handlers.put(raw_methodnew CreateSqlObjectHandler(raw_method.getReturnType()));
            }
            else if (method.getName().equals("close") && method.getRawMember().getParameterTypes().length == 0) {
                handlers.put(raw_methodnew CloseHandler());
            }
            else if (raw_method.isAnnotationPresent(Transaction.class)) {
                handlers.put(raw_methodnew PassThroughTransactionHandler(raw_methodraw_method.getAnnotation(Transaction.class)));
            }
            else if (.containsKey(raw_method)) {
                handlers.put(raw_method.get(raw_method));
            }
            else {
                handlers.put(raw_methodnew PassThroughHandler(raw_method));
            }
        }
        // this is an implicit mixin, not an explicit one, so we need to *always* add it
        handlers.putAll(CloseInternalDoNotUseThisClass.Helper.handlers());
        handlers.putAll(EqualsHandler.handler());
        handlers.putAll(ToStringHandler.handler(sqlObjectType.getName()));
        handlers.putAll(HashCodeHandler.handler());
        return handlers;
    }
    private final Map<MethodHandlerhandlers;
    private final HandleDing           ding;
    public SqlObject(Map<MethodHandlerhandlersHandleDing ding)
    {
        this. = handlers;
        this. = ding;
    }
    public Object invoke(Object proxyMethod methodObject[] args, MethodProxy mpthrows Throwable
    {
        try {
            .retain(method.toString());
            return .get(method).invoke(proxyargsmp);
        }
        finally {
            .release(method.toString());
        }
    }
    public static void close(Object sqlObject)
    {
        if (!(sqlObject instanceof CloseInternalDoNotUseThisClass)) {
            throw new IllegalArgumentException(sqlObject + " is not a sql object");
        }
        CloseInternalDoNotUseThisClass closer = (CloseInternalDoNotUseThisClasssqlObject;
        closer.___jdbi_close___();
    }
    static String getSql(SqlCall qMethod m)
    {
        if (..equals(q.value())) {
            return m.getName();
        }
        else {
            return q.value();
        }
    }
    static String getSql(SqlQuery qMethod m)
    {
        if (..equals(q.value())) {
            return m.getName();
        }
        else {
            return q.value();
        }
    }
    static String getSql(SqlUpdate qMethod m)
    {
        if (..equals(q.value())) {
            return m.getName();
        }
        else {
            return q.value();
        }
    }
    static String getSql(SqlBatch qMethod m)
    {
        if (..equals(q.value())) {
            return m.getName();
        }
        else {
            return q.value();
        }
    }
New to GrepCode? Check out our FAQ X