Package net.sf.cglib.core

Examples of net.sf.cglib.core.ClassEmitter


public class AbstractClassStubBuilder {

  public static Object buildInterfaceStub(final Class<?> clazz, ClassLoader classLoader) throws InstantiationException, IllegalAccessException {
    Enhancer enhancer = new Enhancer() {
      public void generateClass(ClassVisitor v) throws Exception {
        ClassEmitter ce = new ClassEmitter(v);
        ce.begin_class(Constants.V1_5,
            Constants.ACC_PUBLIC,
            getClassName(),
            null,
            new Type[]{Type.getType(clazz)},
            null);
        EmitUtils.null_constructor(ce);
        List methods = new ArrayList();
        getMethods(Object.class, new Class[]{clazz}, methods);

        for (Iterator iterator = methods.iterator(); iterator
        .hasNext();) {
          Method method = (Method) iterator.next();

          if (Modifier.isAbstract(method.getModifiers())) {
            MethodInfo methodInfo = ReflectUtils.getMethodInfo(method);
            Signature signature = methodInfo.getSignature();
            Type returnType = signature.getReturnType();

            CodeEmitter e = ce.begin_method(method.getModifiers() & ~Modifier.ABSTRACT,
                signature, methodInfo.getExceptionTypes(),
                methodInfo.getAttribute());

            e.zero_or_null(returnType);
            e.return_value();

            Type[] argumentTypes = methodInfo.getSignature().getArgumentTypes();
            int size = 0;
            if (argumentTypes != null) {
              for (int i = 0; i < argumentTypes.length; i++) {
                size += argumentTypes[i].getSize();
              }
            }

            // 1 is for this
            e.visitMaxs(returnType.getSize(), size + 1);

            e.end_method();
          }
        }

        ce.end_class();
      }
    };

    enhancer.setCallbackType(NoOp.class);
    enhancer.setInterfaces(new Class[]{clazz});
View Full Code Here


    if ((Modifier.ABSTRACT & clazz.getModifiers()) != 0) {
      Enhancer enhancer = new Enhancer() {
        @Override
        public void generateClass(ClassVisitor v) throws Exception {
          ClassEmitter ce = new ClassEmitter(v);
          ce.begin_class(Constants.V1_5,
              Constants.ACC_PUBLIC,
              getClassName(),
              Type.getType(clazz),
              null,
              null);
          EmitUtils.null_constructor(ce);
          List methods = new ArrayList();
          getMethods(clazz, null, methods);

          for (Iterator iterator = methods.iterator(); iterator
          .hasNext();) {
            Method method = (Method) iterator.next();

            if (Modifier.isAbstract(method.getModifiers())) {
              MethodInfo methodInfo = ReflectUtils.getMethodInfo(method);
              Signature signature = methodInfo.getSignature();
              Type returnType = signature.getReturnType();

              CodeEmitter e = ce.begin_method(method.getModifiers() & ~Modifier.ABSTRACT,
                  signature, methodInfo.getExceptionTypes(),
                  methodInfo.getAttribute());

              e.zero_or_null(returnType);
              e.return_value();

              Type[] argumentTypes = methodInfo.getSignature().getArgumentTypes();
              int size = 0;
              if (argumentTypes != null) {
                for (int i = 0; i < argumentTypes.length; i++) {
                  size += argumentTypes[i].getSize();
                }
              }

              // 1 is for this
              e.visitMaxs(returnType.getSize(), size + 1);

              e.end_method();
            }
          }

          ce.end_class();
        }
      };
      enhancer.setSuperclass(clazz);
      enhancer.setCallbackType(MethodInterceptor.class);
      enhancer.setClassLoader(classLoader != null ?
View Full Code Here

/*  94 */     String[] names = (String[])this.props.keySet().toArray(new String[size]);
/*  95 */     Type[] types = new Type[size];
/*  96 */     for (int i = 0; i < size; i++) {
/*  97 */       types[i] = ((Type)this.props.get(names[i]));
/*     */     }
/*  99 */     ClassEmitter ce = new ClassEmitter(v);
/* 100 */     ce.begin_class(46, 1, getClassName(), this.superclass != null ? Type.getType(this.superclass) : Constants.TYPE_OBJECT, null, null);
/*     */
/* 106 */     EmitUtils.null_constructor(ce);
/* 107 */     EmitUtils.add_properties(ce, names, types);
/* 108 */     ce.end_class();
/*     */   }
View Full Code Here

/*     */     }
/*     */
/*     */     public void generateClass(ClassVisitor v) {
/*  94 */       Type sourceType = Type.getType(this.source);
/*  95 */       Type targetType = Type.getType(this.target);
/*  96 */       ClassEmitter ce = new ClassEmitter(v);
/*  97 */       ce.begin_class(46, 1, getClassName(), BeanCopier.BEAN_COPIER, null, "<generated>");
/*     */
/* 104 */       EmitUtils.null_constructor(ce);
/* 105 */       CodeEmitter e = ce.begin_method(1, BeanCopier.COPY, null, null);
/* 106 */       PropertyDescriptor[] getters = ReflectUtils.getBeanGetters(this.source);
/* 107 */       PropertyDescriptor[] setters = ReflectUtils.getBeanGetters(this.target);
/*     */
/* 109 */       Map names = new HashMap();
/* 110 */       for (int i = 0; i < getters.length; i++) {
/* 111 */         names.put(getters[i].getName(), getters[i]);
/*     */       }
/* 113 */       Local targetLocal = e.make_local();
/* 114 */       Local sourceLocal = e.make_local();
/* 115 */       if (this.useConverter) {
/* 116 */         e.load_arg(1);
/* 117 */         e.checkcast(targetType);
/* 118 */         e.store_local(targetLocal);
/* 119 */         e.load_arg(0);
/* 120 */         e.checkcast(sourceType);
/* 121 */         e.store_local(sourceLocal);
/*     */       } else {
/* 123 */         e.load_arg(1);
/* 124 */         e.checkcast(targetType);
/* 125 */         e.load_arg(0);
/* 126 */         e.checkcast(sourceType);
/*     */       }
/* 128 */       for (int i = 0; i < setters.length; i++) {
/* 129 */         PropertyDescriptor setter = setters[i];
/* 130 */         PropertyDescriptor getter = (PropertyDescriptor)names.get(setter.getName());
/* 131 */         if (getter != null) {
/* 132 */           MethodInfo read = ReflectUtils.getMethodInfo(getter.getReadMethod());
/* 133 */           MethodInfo write = ReflectUtils.getMethodInfo(setter.getWriteMethod());
/* 134 */           if (this.useConverter) {
/* 135 */             Type setterType = write.getSignature().getArgumentTypes()[0];
/* 136 */             e.load_local(targetLocal);
/* 137 */             e.load_arg(2);
/* 138 */             e.load_local(sourceLocal);
/* 139 */             e.invoke(read);
/* 140 */             e.box(read.getSignature().getReturnType());
/* 141 */             EmitUtils.load_class(e, setterType);
/* 142 */             e.push(write.getSignature().getName());
/* 143 */             e.invoke_interface(BeanCopier.CONVERTER, BeanCopier.CONVERT);
/* 144 */             e.unbox_or_zero(setterType);
/* 145 */             e.invoke(write);
/* 146 */           } else if (compatible(getter, setter)) {
/* 147 */             e.dup2();
/* 148 */             e.invoke(read);
/* 149 */             e.invoke(write);
/*     */           }
/*     */         }
/*     */       }
/* 153 */       e.return_value();
/* 154 */       e.end_method();
/* 155 */       ce.end_class();
/*     */     }
View Full Code Here

/*     */   protected Object nextInstance(Object instance) {
/*  98 */     throw new IllegalStateException("InterfaceMaker does not cache");
/*     */   }
/*     */
/*     */   public void generateClass(ClassVisitor v) throws Exception {
/* 102 */     ClassEmitter ce = new ClassEmitter(v);
/* 103 */     ce.begin_class(46, 513, getClassName(), null, null, "<generated>");
/*     */
/* 109 */     for (Iterator it = this.signatures.keySet().iterator(); it.hasNext(); ) {
/* 110 */       Signature sig = (Signature)it.next();
/* 111 */       Type[] exceptions = (Type[])this.signatures.get(sig);
/* 112 */       ce.begin_method(1025, sig, exceptions, null).end_method();
/*     */     }
/*     */
/* 117 */     ce.end_class();
/*     */   }
View Full Code Here

/*  66 */       return super.create(name);
/*     */     }
/*     */
/*     */     public void generateClass(ClassVisitor v) {
/*  70 */       Type targetType = Type.getType(this.target);
/*  71 */       ClassEmitter ce = new ClassEmitter(v);
/*  72 */       ce.begin_class(46, 1, getClassName(), targetType, null, "<generated>");
/*     */
/*  79 */       ce.declare_field(18, "CGLIB$RWBean", targetType, null, null);
/*     */
/*  81 */       CodeEmitter e = ce.begin_method(1, ImmutableBean.CSTRUCT_OBJECT, null, null);
/*  82 */       e.load_this();
/*  83 */       e.super_invoke_constructor();
/*  84 */       e.load_this();
/*  85 */       e.load_arg(0);
/*  86 */       e.checkcast(targetType);
/*  87 */       e.putfield("CGLIB$RWBean");
/*  88 */       e.return_value();
/*  89 */       e.end_method();
/*     */
/*  91 */       PropertyDescriptor[] descriptors = ReflectUtils.getBeanProperties(this.target);
/*  92 */       Method[] getters = ReflectUtils.getPropertyMethods(descriptors, true, false);
/*  93 */       Method[] setters = ReflectUtils.getPropertyMethods(descriptors, false, true);
/*     */
/*  95 */       for (int i = 0; i < getters.length; i++) {
/*  96 */         MethodInfo getter = ReflectUtils.getMethodInfo(getters[i]);
/*  97 */         e = EmitUtils.begin_method(ce, getter, 1);
/*  98 */         e.load_this();
/*  99 */         e.getfield("CGLIB$RWBean");
/* 100 */         e.invoke(getter);
/* 101 */         e.return_value();
/* 102 */         e.end_method();
/*     */       }
/*     */
/* 105 */       for (int i = 0; i < setters.length; i++) {
/* 106 */         MethodInfo setter = ReflectUtils.getMethodInfo(setters[i]);
/* 107 */         e = EmitUtils.begin_method(ce, setter, 1);
/* 108 */         e.throw_exception(ImmutableBean.ILLEGAL_STATE_EXCEPTION, "Bean is immutable");
/* 109 */         e.end_method();
/*     */       }
/*     */
/* 112 */       ce.end_class();
/*     */     }
View Full Code Here

/*  467 */           modifiers = modifiers & 0xFFFFFFFB | 0x1;
/*      */         }
/*  469 */         return ReflectUtils.getMethodInfo(method, modifiers);
/*      */       }
/*      */     });
/*  473 */     ClassEmitter e = new ClassEmitter(v);
/*  474 */     e.begin_class(46, 1, getClassName(), Type.getType(sc), this.useFactory ? TypeUtils.add(TypeUtils.getTypes(this.interfaces), FACTORY) : TypeUtils.getTypes(this.interfaces), "<generated>");
/*      */
/*  482 */     List constructorInfo = CollectionUtils.transform(constructors, MethodInfoTransformer.getInstance());
/*      */
/*  484 */     e.declare_field(2, "CGLIB$BOUND", Type.BOOLEAN_TYPE, null, null);
/*  485 */     if (!this.interceptDuringConstruction) {
/*  486 */       e.declare_field(2, "CGLIB$CONSTRUCTED", Type.BOOLEAN_TYPE, null, null);
/*      */     }
/*  488 */     e.declare_field(26, "CGLIB$THREAD_CALLBACKS", THREAD_LOCAL, null, null);
/*  489 */     e.declare_field(26, "CGLIB$STATIC_CALLBACKS", CALLBACK_ARRAY, null, null);
/*  490 */     if (this.serialVersionUID != null) {
/*  491 */       e.declare_field(26, "serialVersionUID", Type.LONG_TYPE, this.serialVersionUID, null);
/*      */     }
/*      */
/*  494 */     for (int i = 0; i < this.callbackTypes.length; i++) {
/*  495 */       e.declare_field(2, getCallbackField(i), this.callbackTypes[i], null, null);
/*      */     }
/*      */
/*  498 */     emitMethods(e, methods, actualMethods);
/*  499 */     emitConstructors(e, constructorInfo);
/*  500 */     emitSetThreadCallbacks(e);
/*  501 */     emitSetStaticCallbacks(e);
/*  502 */     emitBindCallbacks(e);
/*      */
/*  504 */     if (this.useFactory) {
/*  505 */       int[] keys = getCallbackKeys();
/*  506 */       emitNewInstanceCallbacks(e);
/*  507 */       emitNewInstanceCallback(e);
/*  508 */       emitNewInstanceMultiarg(e, constructorInfo);
/*  509 */       emitGetCallback(e, keys);
/*  510 */       emitSetCallback(e, keys);
/*  511 */       emitGetCallbacks(e);
/*  512 */       emitSetCallbacks(e);
/*      */     }
/*      */
/*  515 */     e.end_class();
/*      */   }
View Full Code Here

/*     */       {
/*     */         Constructor constructor;
/*  86 */         throw new IllegalArgumentException("interface does not match any known constructor");
/*     */       }
/*     */       Constructor constructor;
/*  89 */       ClassEmitter ce = new ClassEmitter(v);
/*  90 */       ce.begin_class(46, 1, getClassName(), CONSTRUCTOR_DELEGATE, new Type[] { Type.getType(this.iface) }, "<generated>");
/*     */
/*  96 */       Type declaring = Type.getType(constructor.getDeclaringClass());
/*  97 */       EmitUtils.null_constructor(ce);
/*  98 */       CodeEmitter e = ce.begin_method(1, ReflectUtils.getSignature(newInstance), ReflectUtils.getExceptionTypes(newInstance), null);
/*     */
/* 102 */       e.new_instance(declaring);
/* 103 */       e.dup();
/* 104 */       e.load_args();
/* 105 */       e.invoke_constructor(declaring, ReflectUtils.getSignature(constructor));
/* 106 */       e.return_value();
/* 107 */       e.end_method();
/* 108 */       ce.end_class();
/*     */     }
View Full Code Here

/*     */     }
/*     */
/*     */     public void generateClass(ClassVisitor cv) {
/*  97 */       MethodInfo method = ReflectUtils.getMethodInfo(ReflectUtils.findInterfaceMethod(this.iface));
/*     */
/*  99 */       ClassEmitter ce = new ClassEmitter(cv);
/* 100 */       ce.begin_class(46, 1, getClassName(), MULTICAST_DELEGATE, new Type[] { Type.getType(this.iface) }, "<generated>");
/*     */
/* 106 */       EmitUtils.null_constructor(ce);
/*     */
/* 109 */       emitProxy(ce, method);
/*     */
/* 112 */       CodeEmitter e = ce.begin_method(1, NEW_INSTANCE, null, null);
/* 113 */       e.new_instance_this();
/* 114 */       e.dup();
/* 115 */       e.invoke_constructor_this();
/* 116 */       e.return_value();
/* 117 */       e.end_method();
/*     */
/* 120 */       e = ce.begin_method(1, ADD_DELEGATE, null, null);
/* 121 */       e.load_this();
/* 122 */       e.load_arg(0);
/* 123 */       e.checkcast(Type.getType(this.iface));
/* 124 */       e.invoke_virtual_this(ADD_HELPER);
/* 125 */       e.return_value();
/* 126 */       e.end_method();
/*     */
/* 128 */       ce.end_class();
/*     */     }
View Full Code Here

/* 209 */       boolean isStatic = TypeUtils.isStatic(methodInfo.getModifiers());
/* 210 */       if ((this.target == null ^ isStatic)) {
/* 211 */         throw new IllegalArgumentException("Static method " + (isStatic ? "not " : "") + "expected");
/*     */       }
/*     */
/* 214 */       ClassEmitter ce = new ClassEmitter(v);
/*     */
/* 216 */       ce.begin_class(46, 1, getClassName(), METHOD_DELEGATE, new Type[] { Type.getType(this.iface) }, "<generated>");
/*     */
/* 222 */       ce.declare_field(26, "eqMethod", Constants.TYPE_STRING, null, null);
/* 223 */       EmitUtils.null_constructor(ce);
/*     */
/* 226 */       MethodInfo proxied = ReflectUtils.getMethodInfo(this.iface.getDeclaredMethods()[0]);
/* 227 */       CodeEmitter e = EmitUtils.begin_method(ce, proxied, 1);
/* 228 */       e.load_this();
/* 229 */       e.super_getfield("target", Constants.TYPE_OBJECT);
/* 230 */       e.checkcast(methodInfo.getClassInfo().getType());
/* 231 */       e.load_args();
/* 232 */       e.invoke(methodInfo);
/* 233 */       e.return_value();
/* 234 */       e.end_method();
/*     */
/* 237 */       e = ce.begin_method(1, NEW_INSTANCE, null, null);
/* 238 */       e.new_instance_this();
/* 239 */       e.dup();
/* 240 */       e.dup2();
/* 241 */       e.invoke_constructor_this();
/* 242 */       e.getfield("eqMethod");
/* 243 */       e.super_putfield("eqMethod", Constants.TYPE_STRING);
/* 244 */       e.load_arg(0);
/* 245 */       e.super_putfield("target", Constants.TYPE_OBJECT);
/* 246 */       e.return_value();
/* 247 */       e.end_method();
/*     */
/* 250 */       e = ce.begin_static();
/* 251 */       e.push(methodInfo.getSignature().toString());
/* 252 */       e.putfield("eqMethod");
/* 253 */       e.return_value();
/* 254 */       e.end_method();
/*     */
/* 256 */       ce.end_class();
/*     */     }
View Full Code Here

TOP

Related Classes of net.sf.cglib.core.ClassEmitter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.