private static ClassHierarchyProto.ConstructorDef serializeConstructorDef(ConstructorDef<?> def) {
   List<ClassHierarchyProto.ConstructorArg> args = new ArrayList<>();
   for (ConstructorArg arg : def.getArgs()) {
     args.add(
         newConstructorArg(arg.getType(), arg.getNamedParameterName(), arg.isInjectionFuture()));
   }
   return newConstructorDef(def.getClassName(), args);
 }
示例#2
0
  /**
   * @param args
   * @throws FileNotFoundException
   * @throws MalformedURLException
   */
  public static void main(final String[] args)
      throws FileNotFoundException, MalformedURLException, UnsupportedEncodingException {
    int i = 0;
    String doc = null;
    String jar = null;
    boolean tangTests = false;
    while (i < args.length) {
      if (args[i].equals("--doc")) {
        i++;
        doc = args[i];
      } else if (args[i].equals("--jar")) {
        i++;
        jar = args[i];
      } else if (args[i].equals("--tang-tests")) {
        tangTests = true;
      }

      i++;
    }

    final Tint t;
    if (jar != null) {
      final File f = new File(jar);
      if (!f.exists()) {
        throw new FileNotFoundException(jar);
      }
      t = new Tint(new URL[] {f.toURI().toURL()}, tangTests);
    } else {
      t = new Tint(new URL[0], tangTests);
    }

    if (doc != null) {
      try (final PrintStream out = new PrintStream(doc, "UTF-8")) {
        out.println("<html><head><title>TangDoc</title>");

        out.println("<style>");
        out.println(
            "body { font-family: 'Segoe UI', 'Comic Sans MS'; font-size:12pt; font-weight: 200; "
                + "margin: 1em; column-count: 2; }");
        out.println(".package { font-size:18pt; font-weight: 500; column-span: all; }");
        //      out.println(".class { break-after: never; }");
        //      out.println(".doc { break-before: never; }");
        out.println(".decl-margin { padding: 8pt; break-inside: avoid; }");
        out.println(".module-margin { padding: 8pt; column-span: all; break-inside: avoid; }");
        out.println(".decl { background-color: aliceblue; padding: 6pt;}");
        out.println(".fullName { font-size: 11pt; font-weight: 400; }");
        out.println(".simpleName { font-size: 11pt; font-weight: 400; }");
        out.println(".constructorArg { padding-left: 16pt; }");
        out.println("." + SETTERS + " { padding-top: 6pt; font-size: 10pt; }");
        out.println("." + USES + " { padding-top: 6pt; font-size: 10pt; }");
        out.println("pre { font-size: 10pt; }");
        out.println("</style>");

        out.println("</head><body>");

        String currentPackage = "";
        for (final Node n : t.getNamesUsedAndSet()) {
          final String fullName = n.getFullName();
          final String[] tok = fullName.split("\\.");
          final StringBuffer sb = new StringBuffer(tok[0]);
          for (int j = 1; j < tok.length; j++) {
            if (tok[j].matches("^[A-Z].*") || j > 4) {
              break;
            } else {
              sb.append("." + tok[j]);
            }
          }
          final String pack = sb.toString();
          if (!currentPackage.equals(pack)) {
            currentPackage = pack;
            out.println(t.endPackage());
            out.println(t.startPackage(currentPackage));
          }
          if (n instanceof NamedParameterNode<?>) {
            out.println(t.toHtmlString((NamedParameterNode<?>) n, currentPackage));
          } else if (n instanceof ClassNode<?>) {
            out.println(t.toHtmlString((ClassNode<?>) n, currentPackage));
          } else {
            throw new IllegalStateException();
          }
        }
        out.println("</div>");
        out.println(t.endPackage());
        out.println("<div class='package'>Module definitions</div>");
        for (final Field f : t.modules.keySet()) {
          final String moduleName = ReflectionUtilities.getFullName(f);
          //        String declaringClassName =
          // ReflectionUtilities.getFullName(f.getDeclaringClass());
          out.println(
              "<div class='module-margin' id='"
                  + moduleName
                  + "'><div class='decl'><span class='fullName'>"
                  + moduleName
                  + "</span>");
          out.println("<pre>");
          final String conf = t.modules.get(f).toPrettyString();
          final String[] tok = conf.split("\n");
          for (final String line : tok) {
            out.println(stripPrefix(line, "no.such.prefix")); // t.modules.get(f).toPrettyString());
          }
          //        List<Entry<String,String>> lines = t.modules.get(f).toStringPairs();
          //        for(Entry<String,String> line : lines) {
          //          String k = t.stripPrefix(line.getKey(), declaringClassName);
          //          String v = t.stripPrefix(line.getValue(), declaringClassName);
          //          out.println(k+"="+v);
          //        }
          out.println("</pre>");
          out.println("</div></div>");
        }

        out.println("<div class='package'>Interfaces and injectable classes</div>");
        for (final ClassNode<?> c : t.knownClasses) {
          if (t.classFilter(tangTests, c.getFullName())) {
            Class<?> clz = null;
            try {
              clz = t.ch.classForName(c.getFullName());
            } catch (final ClassNotFoundException e) {
              // TODO[JIRA REEF-864] Clarify handling in this case
              e.printStackTrace();
            }
            final String typ =
                clz == null ? "undefined" : clz.isInterface() ? "interface" : "class";
            out.println(
                "<div class='module-margin' id='"
                    + c.getFullName()
                    + "'><div class='decl'>"
                    + "<span class='fullName'>"
                    + typ
                    + " "
                    + c.getFullName()
                    + "</span>");
            for (final ConstructorDef<?> d : c.getInjectableConstructors()) {
              out.println("<div class='uses'>" + c.getFullName() + "(");
              for (final ConstructorArg a : d.getArgs()) {
                if (a.getNamedParameterName() != null) {
                  out.print(
                      "<div class='constructorArg'><a href='#"
                          + a.getType()
                          + "'>"
                          + stripPrefix(a.getType(), "xxx")
                          + "</a> <a href='#"
                          + a.getNamedParameterName()
                          + "'>"
                          + a.getNamedParameterName()
                          + "</a></div>");
                } else {
                  out.print(
                      "<div class='constructorArg'><a href='#"
                          + a.getType()
                          + "'>"
                          + stripPrefix(a.getType(), "xxx")
                          + "</a></div>");
                }
              }
              out.println(")</div>");
            }
            out.println("</div></div>");
          }
          /*
          out.println("<h1>Default usage of classes and constants</h1>");
          for(String s : t.usages.keySet()) {
            out.println("<h2>" + s + "</h2>");
            for(Node n : t.usages.getValuesForKey(s)) {
              out.println("<p>" + n.getFullName() + "</p>");
            }
          } */
        }
        out.println("</body></html>");
      }
    }
  }
示例#3
0
  /**
   * 生成kratos的核心信息配置文件
   *
   * @author gaoxianglong
   * @exception Exception
   * @return boolean 生成结果
   */
  public boolean createCoreXml(File savePath) {
    boolean result = false;
    try {
      /* 创建配置文件的根目录<beans/>标签 */
      Beans beans = new Beans();
      /* 创建<bean/>子标签 */
      Bean bean0 = new Bean();
      bean0.setId("jdbcTemplate");
      bean0.setClass_("org.springframework.jdbc.core.JdbcTemplate");
      /* 创建<property/>子标签 */
      Property dataSource = new Property();
      dataSource.setName("dataSource");
      dataSource.setRef("dataSourceGroup");
      List<Property> propertys = new ArrayList<Property>();
      propertys.add(dataSource);
      bean0.setProperty(propertys);

      Bean bean1 = new Bean();
      bean1.setId("shardRule");
      bean1.setClass_("com.sharksharding.core.shard.ShardRule");
      bean1.setInit_method("init");
      /* 创建<property/>子标签 */
      Property isShard = new Property();
      isShard.setName("isShard");
      isShard.setValue("true");
      Property wr_index = new Property();
      wr_index.setName("wr_index");
      wr_index.setValue(this.getWr_index());
      Property shardMode = new Property();
      shardMode.setName("shardMode");
      shardMode.setValue(this.isShardMode());
      Property consistent = new Property();
      consistent.setName("consistent");
      consistent.setValue(this.getConsistent());
      Property dbRuleArray = new Property();
      dbRuleArray.setName("dbRuleArray");
      dbRuleArray.setValue(this.getDbRuleArray());
      Property tbRuleArray = new Property();
      tbRuleArray.setName("tbRuleArray");
      tbRuleArray.setValue(this.getTbRuleArray());
      Property tbSuffix = new Property();
      tbSuffix.setName("tbSuffix");
      tbSuffix.setValue(this.getTbSuffix());
      propertys = new ArrayList<Property>();
      propertys.add(isShard);
      propertys.add(wr_index);
      propertys.add(shardMode);
      propertys.add(consistent);
      propertys.add(dbRuleArray);
      propertys.add(tbRuleArray);
      propertys.add(tbSuffix);
      bean1.setProperty(propertys);
      /* 创建<constructor_arg/>子标签 */
      // ConstructorArg constructor_arg = new ConstructorArg();
      // constructor_arg.setName("isShard");
      // constructor_arg.setValue(this.isShard());
      // bean1.setConstructor_arg(constructor_arg);
      /* 创建<bean/>子标签 */
      Bean bean2 = new Bean();
      bean2.setId("dataSourceGroup");
      bean2.setClass_("com.sharksharding.core.config.SharkDatasourceGroup");
      /* 创建<property/>子标签 */
      Property targetDataSources = new Property();
      targetDataSources.setName("targetDataSources");
      /* 创建<map/>子标签 */
      Map map = new Map();
      map.setKey_type("java.lang.Integer");
      List<Entry> entrys = new ArrayList<Entry>();
      for (int i = 0; i < Integer.parseInt(dbSize); i++) {
        /* 创建<entry/>子标签 */
        Entry entry = new Entry();
        entry.setKey(String.valueOf(i));
        entry.setValue_ref("dataSource" + (i + 1));
        entrys.add(entry);
      }
      map.setEntry(entrys);
      targetDataSources.setMap(map);
      propertys = new ArrayList<Property>();
      propertys.add(targetDataSources);
      bean2.setProperty(propertys);
      /* 创建<bean/>子标签 */
      Bean bean3 = new Bean();
      bean3.setId("sqlTemplate");
      bean3.setClass_("com.sharksharding.sql.SQLTemplate");
      /* 创建<constructor_arg/>子标签 */
      ConstructorArg constructor_arg1 = new ConstructorArg();
      constructor_arg1.setName("path");
      constructor_arg1.setValue(this.getSqlPath());
      bean3.setConstructor_arg(constructor_arg1);
      List<Bean> beanList = new ArrayList<Bean>();
      beanList.add(bean0);
      beanList.add(bean1);
      beanList.add(bean2);
      beanList.add(bean3);
      beans.setBean(beanList);
      if (this.getIsShow()) marshaller.marshal(beans, System.out);
      marshaller.marshal(beans, savePath);
      result = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
示例#4
0
  private Node register(final String s) {
    final Class<?> c;
    try {
      c = classForName(s);
    } catch (final ClassNotFoundException e1) {
      return null;
    }
    try {
      final Node n = getAlreadyBoundNode(c);
      return n;
    } catch (final NameResolutionException e) {
      // node not bound yet
    }
    // First, walk up the class hierarchy, registering all out parents. This
    // can't be loopy.
    if (c.getSuperclass() != null) {
      register(ReflectionUtilities.getFullName(c.getSuperclass()));
    }
    for (final Class<?> i : c.getInterfaces()) {
      register(ReflectionUtilities.getFullName(i));
    }
    // Now, we'd like to register our enclosing classes. This turns out to be
    // safe.
    // Thankfully, Java doesn't allow:
    // class A implements A.B { class B { } }

    // It also doesn't allow cycles such as:
    // class A implements B.BB { interface AA { } }
    // class B implements A.AA { interface BB { } }

    // So, even though grafting arbitrary DAGs together can give us cycles, Java
    // seems
    // to have our back on this one.
    final Class<?> enclosing = c.getEnclosingClass();
    if (enclosing != null) {
      register(ReflectionUtilities.getFullName(enclosing));
    }

    // Now register the class. This has to be after the above so we know our
    // parents (superclasses and enclosing packages) are already registered.
    final Node n = registerClass(c);

    // Finally, do things that might introduce cycles that invlove c.
    // This has to be below registerClass, which ensures that any cycles
    // this stuff introduces are broken.
    for (final Class<?> innerClass : c.getDeclaredClasses()) {
      register(ReflectionUtilities.getFullName(innerClass));
    }
    if (n instanceof ClassNode) {
      final ClassNode<?> cls = (ClassNode<?>) n;
      for (final ConstructorDef<?> def : cls.getInjectableConstructors()) {
        for (final ConstructorArg arg : def.getArgs()) {
          register(arg.getType());
          if (arg.getNamedParameterName() != null) {
            final NamedParameterNode<?> np =
                (NamedParameterNode<?>) register(arg.getNamedParameterName());
            try {
              // TODO: When handling sets, need to track target of generic parameter, and check the
              // type here!
              if (!np.isSet()
                  && !np.isList()
                  && !ReflectionUtilities.isCoercable(
                      classForName(arg.getType()), classForName(np.getFullArgName()))) {
                throw new ClassHierarchyException(
                    "Named parameter type mismatch in "
                        + cls.getFullName()
                        + ".  Constructor expects a "
                        + arg.getType()
                        + " but "
                        + np.getName()
                        + " is a "
                        + np.getFullArgName());
              }
            } catch (final ClassNotFoundException e) {
              throw new ClassHierarchyException(
                  "Constructor refers to unknown class " + arg.getType(), e);
            }
          }
        }
      }
    } else if (n instanceof NamedParameterNode) {
      final NamedParameterNode<?> np = (NamedParameterNode<?>) n;
      register(np.getFullArgName());
    }
    return n;
  }