Package com.alibaba.dubbo.common.bytecode

Examples of com.alibaba.dubbo.common.bytecode.ClassGenerator


        }
        // 完全没有Adaptive方法,则不需要生成Adaptive类
        if(! hasAdaptiveAnnotation)
            throw new IllegalStateException("No adaptive method on extension " + type.getName() + ", refuse to create the adaptive class!");
       
        ClassGenerator cg = ClassGenerator.newInstance(classLoader);
        cg.setClassName(type.getName() + "$Adpative");
        cg.addInterface(type);
        cg.addDefaultConstructor();
       
        for (Method method : methods) {
            Class<?> rt = method.getReturnType();
            Class<?>[] pts = method.getParameterTypes();

            Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class);
            StringBuilder code = new StringBuilder(512);
            if (adaptiveAnnotation == null) {
                code.append("throw new UnsupportedOperationException(\"method ")
                        .append(method.toString()).append(" of interface ")
                        .append(type.getName()).append(" is not adaptive method!\");");
            } else {
                int urlTypeIndex = -1;
                for (int i = 0; i < pts.length; ++i) {
                    if (pts[i].equals(URL.class)) {
                        urlTypeIndex = i;
                        break;
                    }
                }
                // 有类型为URL的参数
                if (urlTypeIndex != -1) {
                    // Null Point check
                    String s = String.format("if (arg%d == null)  { throw new IllegalArgumentException(\"url == null\"); }",
                                    urlTypeIndex);
                    code.append(s);
                   
                    s = String.format("%s url = arg%d;", URL.class.getName(), urlTypeIndex);
                    code.append(s);
                }
                // 参数没有URL类型
                else {
                    String attribMethod = null;
                   
                    // 找到参数的URL属性
                    LBL_PTS:
                    for (int i = 0; i < pts.length; ++i) {
                        Method[] ms = pts[i].getMethods();
                        for (Method m : ms) {
                            String name = m.getName();
                            if ((name.startsWith("get") || name.length() > 3)
                                    && Modifier.isPublic(m.getModifiers())
                                    && !Modifier.isStatic(m.getModifiers())
                                    && m.getParameterTypes().length == 0
                                    && m.getReturnType() == URL.class) {
                                urlTypeIndex = i;
                                attribMethod = name;
                                break LBL_PTS;
                            }
                        }
                    }
                    if(attribMethod == null) {
                        throw new IllegalStateException("fail to create adative class for interface " + type.getName()
                            + ": not found url parameter or url attribute in parameters of method " + method.getName());
                    }
                   
                    // Null point check
                    String s = String.format("if (arg%d == null)  { throw new IllegalArgumentException(\"%s argument == null\"); }",
                                    urlTypeIndex, pts[urlTypeIndex].getName());
                    code.append(s);
                    s = String.format("if (arg%d.%s() == null)  { throw new IllegalArgumentException(\"%s argument %s() == null\"); }",
                                    urlTypeIndex, attribMethod, pts[urlTypeIndex].getName(), attribMethod);
                    code.append(s);

                    s = String.format("%s url = arg%d.%s();",URL.class.getName(), urlTypeIndex, attribMethod);
                    code.append(s);
                }
               
                String[] value = adaptiveAnnotation.value();
                // 没有设置Key,则使用“扩展点接口名的点分隔 作为Key
                if(value.length == 0) {
                    char[] charArray = type.getSimpleName().toCharArray();
                    StringBuilder sb = new StringBuilder(128);
                    for (int i = 0; i < charArray.length; i++) {
                        if(Character.isUpperCase(charArray[i])) {
                            if(i != 0) {
                                sb.append(".");
                            }
                            sb.append(Character.toLowerCase(charArray[i]));
                        }
                        else {
                            sb.append(charArray[i]);
                        }
                    }
                    value = new String[] {sb.toString()};
                }
               
                String defaultExtName = cachedDefaultName;
                String getNameCode = null;
                for (int i = value.length - 1; i >= 0; --i) {
                    if(i == value.length - 1) {
                        if(null != defaultExtName) {
                            if(!"protocol".equals(value[i]))
                                getNameCode = String.format("url.getParameter(\"%s\", \"%s\")", value[i], defaultExtName);
                            else
                                getNameCode = String.format("( url.getProtocol() == null ? \"%s\" : url.getProtocol() )", defaultExtName);
                        }
                        else {
                            if(!"protocol".equals(value[i]))
                                getNameCode = String.format("url.getParameter(\"%s\")", value[i]);
                            else
                                getNameCode = "url.getProtocol()";
                        }
                    }
                    else {
                        if(!"protocol".equals(value[i]))
                            getNameCode = String.format("url.getParameter(\"%s\", %s)", value[i], getNameCode);
                        else
                            getNameCode = String.format("( url.getProtocol() == null ? (%s) : url.getProtocol() )", getNameCode);
                    }
                }
                code.append("String extName = ").append(getNameCode).append(";");
                // check extName == null?
                String s = String.format("if(extName == null) {" +
                    "throw new IllegalStateException(\"Fail to get extension(%s) name from url(\" + url.toString() + \") use keys(%s)\"); }",
                        type.getName(), Arrays.toString(value));
                code.append(s);
               
                s = String.format("%s extension = (%<s)%s.getExtensionLoader(%s.class).getExtension(extName);",
                        type.getName(), ExtensionLoader.class.getName(), type.getName());
                code.append(s);
               
                // return statement
                if (!rt.equals(void.class)) {
                    code.append("return ");
                }

                s = String.format("extension.%s(", method.getName());
                code.append(s);
                for (int i = 0; i < pts.length; i++) {
                    if (i != 0)
                        code.append(", ");
                    code.append("arg").append(i);
                }
                code.append(");");
            }
           
            cg.addMethod(method.getName(), method.getModifiers(), rt, pts,
                    method.getExceptionTypes(), code.toString());
        }
        return cg.toClass();
    }
View Full Code Here


      cpf.append("ret[i] = (").append(ccn).append(")builder.parseFrom($1);");
    }
    cwt.append(" } }");
    cpf.append(" } return ret; }");

    ClassGenerator cg = ClassGenerator.newInstance(cl);
    cg.setClassName(bcn);
    cg.setSuperClass(Builder.class);
    cg.addDefaultConstructor();
    if( builder != null )
      cg.addField("public static " + BUILDER_CLASS_NAME + " builder;");
    cg.addMethod("public Class getType(){ return " + cn + ".class; }");
    cg.addMethod(cwt.toString());
    cg.addMethod(cpf.toString());
    try
    {
      Class<?> wc = cg.toClass();
      // set static field.
      if( builder != null )
        wc.getField("builder").set(null, builder);
      return (Builder<?>)wc.newInstance();
    }
    catch(RuntimeException e)
    {
      throw e;
    }
    catch(Throwable e)
    {
      throw new RuntimeException(e.getMessage());
    }
    finally
    {
      cg.release();
    }
  }
View Full Code Here

      crf.append("int len = $2.readInt(); for(int i=0;i<len;i++) ret.put($2.readObject(), $2.readObject());");
    }
    cwf.append(" }");
    crf.append(" }");

    ClassGenerator cg = ClassGenerator.newInstance(cl);
    cg.setClassName(bcn);
    cg.setSuperClass(AbstractObjectBuilder.class);
    cg.addDefaultConstructor();
    cg.addField("public static java.lang.reflect.Field[] fields;");
    cg.addField("public static " + BUILDER_CLASS_NAME + "[] builders;");
    if( !dn )
      cg.addField("public static java.lang.reflect.Constructor constructor;");
    cg.addMethod("public Class getType(){ return " + cn + ".class; }");
    cg.addMethod(cwf.toString());
    cg.addMethod(crf.toString());
    cg.addMethod(cni.toString());
    try
    {
      Class<?> wc = cg.toClass();
      // set static field
      wc.getField("fields").set(null, fs);
      wc.getField("builders").set(null, builders.toArray(new Builder<?>[0]));
      if( !dn )
        wc.getField("constructor").set(null, con);
      return (Builder<?>)wc.newInstance();
    }
    catch(RuntimeException e)
    {
      throw e;
    }
    catch(Throwable e)
    {
      throw new RuntimeException(e.getMessage(), e);
    }
    finally
    {
      cg.release();
    }
  }
View Full Code Here

    cwt.append("if( $1 == null ){ $2.writeUTF(null); }else{ $2.writeUTF(v.name()); } }");

    StringBuilder cpf = new StringBuilder("public Object parseFrom(").append(GenericObjectInput.class.getName()).append(" in) throws java.io.IOException{"); // parseFrom code.
    cpf.append("String name = $1.readUTF(); if( name == null ) return null; return (").append(cn).append(")Enum.valueOf(").append(cn).append(".class, name); }");

    ClassGenerator cg = ClassGenerator.newInstance(cl);
    cg.setClassName(bcn);
    cg.setSuperClass(Builder.class);
    cg.addDefaultConstructor();
    cg.addMethod("public Class getType(){ return " + cn + ".class; }");
    cg.addMethod(cwt.toString());
    cg.addMethod(cpf.toString());
    try
    {
      Class<?> wc = cg.toClass();
      return (Builder<?>)wc.newInstance();
    }
    catch(RuntimeException e)
    {
      throw e;
    }
    catch(Throwable e)
    {
      throw new RuntimeException(e.getMessage(), e);
    }
    finally
    {
      cg.release();
    }
  }
View Full Code Here

      cpf.append("ret[i] = (").append(ccn).append(")builder.parseFrom($1);");
    }
    cwt.append(" } }");
    cpf.append(" } return ret; }");

    ClassGenerator cg = ClassGenerator.newInstance(cl);
    cg.setClassName(bcn);
    cg.setSuperClass(Builder.class);
    cg.addDefaultConstructor();
    if( builder != null )
      cg.addField("public static " + BUILDER_CLASS_NAME + " builder;");
    cg.addMethod("public Class getType(){ return " + cn + ".class; }");
    cg.addMethod(cwt.toString());
    cg.addMethod(cpf.toString());
    try
    {
      Class<?> wc = cg.toClass();
      // set static field.
      if( builder != null )
        wc.getField("builder").set(null, builder);
      return (Builder<?>)wc.newInstance();
    }
    catch(RuntimeException e)
    {
      throw e;
    }
    catch(Throwable e)
    {
      throw new RuntimeException(e.getMessage());
    }
    finally
    {
      cg.release();
    }
  }
View Full Code Here

      crf.append("int len = $2.readInt(); for(int i=0;i<len;i++) ret.put($2.readObject(), $2.readObject());");
    }
    cwf.append(" }");
    crf.append(" }");

    ClassGenerator cg = ClassGenerator.newInstance(cl);
    cg.setClassName(bcn);
    cg.setSuperClass(AbstractObjectBuilder.class);
    cg.addDefaultConstructor();
    cg.addField("public static java.lang.reflect.Field[] fields;");
    cg.addField("public static " + BUILDER_CLASS_NAME + "[] builders;");
    if( !dn )
      cg.addField("public static java.lang.reflect.Constructor constructor;");
    cg.addMethod("public Class getType(){ return " + cn + ".class; }");
    cg.addMethod(cwf.toString());
    cg.addMethod(crf.toString());
    cg.addMethod(cni.toString());
    try
    {
      Class<?> wc = cg.toClass();
      // set static field
      wc.getField("fields").set(null, fs);
      wc.getField("builders").set(null, builders.toArray(new Builder<?>[0]));
      if( !dn )
        wc.getField("constructor").set(null, con);
      return (Builder<?>)wc.newInstance();
    }
    catch(RuntimeException e)
    {
      throw e;
    }
    catch(Throwable e)
    {
      throw new RuntimeException(e.getMessage(), e);
    }
    finally
    {
      cg.release();
    }
  }
View Full Code Here

    cwt.append("if( $1 == null ){ $2.writeUTF(null); }else{ $2.writeUTF(v.name()); } }");

    StringBuilder cpf = new StringBuilder("public Object parseFrom(").append(GenericObjectInput.class.getName()).append(" in) throws java.io.IOException{"); // parseFrom code.
    cpf.append("String name = $1.readUTF(); if( name == null ) return null; return (").append(cn).append(")Enum.valueOf(").append(cn).append(".class, name); }");

    ClassGenerator cg = ClassGenerator.newInstance(cl);
    cg.setClassName(bcn);
    cg.setSuperClass(Builder.class);
    cg.addDefaultConstructor();
    cg.addMethod("public Class getType(){ return " + cn + ".class; }");
    cg.addMethod(cwt.toString());
    cg.addMethod(cpf.toString());
    try
    {
      Class<?> wc = cg.toClass();
      return (Builder<?>)wc.newInstance();
    }
    catch(RuntimeException e)
    {
      throw e;
    }
    catch(Throwable e)
    {
      throw new RuntimeException(e.getMessage(), e);
    }
    finally
    {
      cg.release();
    }
  }
View Full Code Here

      cpf.append("ret[i] = (").append(ccn).append(")builder.parseFrom($1);");
    }
    cwt.append(" } }");
    cpf.append(" } return ret; }");

    ClassGenerator cg = ClassGenerator.newInstance(cl);
    cg.setClassName(bcn);
    cg.setSuperClass(Builder.class);
    cg.addDefaultConstructor();
    if( builder != null )
      cg.addField("public static " + BUILDER_CLASS_NAME + " builder;");
    cg.addMethod("public Class getType(){ return " + cn + ".class; }");
    cg.addMethod(cwt.toString());
    cg.addMethod(cpf.toString());
    try
    {
      Class<?> wc = cg.toClass();
      // set static field.
      if( builder != null )
        wc.getField("builder").set(null, builder);
      return (Builder<?>)wc.newInstance();
    }
    catch(RuntimeException e)
    {
      throw e;
    }
    catch(Throwable e)
    {
      throw new RuntimeException(e.getMessage());
    }
    finally
    {
      cg.release();
    }
  }
View Full Code Here

      crf.append("int len = $2.readInt(); for(int i=0;i<len;i++) ret.put($2.readObject(), $2.readObject());");
    }
    cwf.append(" }");
    crf.append(" }");

    ClassGenerator cg = ClassGenerator.newInstance(cl);
    cg.setClassName(bcn);
    cg.setSuperClass(AbstractObjectBuilder.class);
    cg.addDefaultConstructor();
    cg.addField("public static java.lang.reflect.Field[] fields;");
    cg.addField("public static " + BUILDER_CLASS_NAME + "[] builders;");
    if( !dn )
      cg.addField("public static java.lang.reflect.Constructor constructor;");
    cg.addMethod("public Class getType(){ return " + cn + ".class; }");
    cg.addMethod(cwf.toString());
    cg.addMethod(crf.toString());
    cg.addMethod(cni.toString());
    try
    {
      Class<?> wc = cg.toClass();
      // set static field
      wc.getField("fields").set(null, fs);
      wc.getField("builders").set(null, builders.toArray(new Builder<?>[0]));
      if( !dn )
        wc.getField("constructor").set(null, con);
      return (Builder<?>)wc.newInstance();
    }
    catch(RuntimeException e)
    {
      throw e;
    }
    catch(Throwable e)
    {
      throw new RuntimeException(e.getMessage(), e);
    }
    finally
    {
      cg.release();
    }
  }
View Full Code Here

    cwt.append("if( $1 == null ){ $2.writeUTF(null); }else{ $2.writeUTF(v.name()); } }");

    StringBuilder cpf = new StringBuilder("public Object parseFrom(").append(GenericObjectInput.class.getName()).append(" in) throws java.io.IOException{"); // parseFrom code.
    cpf.append("String name = $1.readUTF(); if( name == null ) return null; return (").append(cn).append(")Enum.valueOf(").append(cn).append(".class, name); }");

    ClassGenerator cg = ClassGenerator.newInstance(cl);
    cg.setClassName(bcn);
    cg.setSuperClass(Builder.class);
    cg.addDefaultConstructor();
    cg.addMethod("public Class getType(){ return " + cn + ".class; }");
    cg.addMethod(cwt.toString());
    cg.addMethod(cpf.toString());
    try
    {
      Class<?> wc = cg.toClass();
      return (Builder<?>)wc.newInstance();
    }
    catch(RuntimeException e)
    {
      throw e;
    }
    catch(Throwable e)
    {
      throw new RuntimeException(e.getMessage(), e);
    }
    finally
    {
      cg.release();
    }
  }
View Full Code Here

TOP

Related Classes of com.alibaba.dubbo.common.bytecode.ClassGenerator

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.