예제 #1
0
 /**
  * Parse a string, assuming that it is of the type expected by a given NamedParameter.
  *
  * <p>This method does not deal with sets; if the NamedParameter is set valued, then the provided
  * string should correspond to a single member of the set. It is up to the caller to call parse
  * once for each value that should be parsed as a member of the set.
  *
  * @return a non-null reference to the parsed value.
  */
 @Override
 @SuppressWarnings("unchecked")
 public <T> T parse(final NamedParameterNode<T> np, final String value) throws ParseException {
   final ClassNode<T> iface;
   try {
     iface = (ClassNode<T>) getNode(np.getFullArgName());
   } catch (final NameResolutionException e) {
     throw new IllegalStateException(
         "Could not parse validated named parameter argument type.  NamedParameter is "
             + np.getFullName()
             + " argument type is "
             + np.getFullArgName());
   }
   Class<?> clazz;
   String fullName;
   try {
     clazz = classForName(iface.getFullName());
     fullName = null;
   } catch (final ClassNotFoundException e) {
     clazz = null;
     fullName = iface.getFullName();
   }
   try {
     if (clazz != null) {
       return (T) parameterParser.parse(clazz, value);
     } else {
       return parameterParser.parse(fullName, value);
     }
   } catch (final UnsupportedOperationException e) {
     try {
       final Node impl = getNode(value);
       if (impl instanceof ClassNode && isImplementation(iface, (ClassNode<?>) impl)) {
         return (T) impl;
       }
       throw new ParseException(
           "Name<"
               + iface.getFullName()
               + "> "
               + np.getFullName()
               + " cannot take non-subclass "
               + impl.getFullName(),
           e);
     } catch (final NameResolutionException e2) {
       throw new ParseException(
           "Name<"
               + iface.getFullName()
               + "> "
               + np.getFullName()
               + " cannot take non-class "
               + value,
           e);
     }
   }
 }
  private static ClassHierarchyProto.Node serializeNode(Node n) {
    List<ClassHierarchyProto.Node> children = new ArrayList<>();
    for (Node child : n.getChildren()) {
      children.add(serializeNode(child));
    }
    if (n instanceof ClassNode) {
      ClassNode<?> cn = (ClassNode<?>) n;
      ConstructorDef<?>[] injectable = cn.getInjectableConstructors();
      ConstructorDef<?>[] all = cn.getAllConstructors();
      List<ConstructorDef<?>> others = new ArrayList<>(Arrays.asList(all));
      others.removeAll(Arrays.asList(injectable));

      List<ClassHierarchyProto.ConstructorDef> injectableConstructors = new ArrayList<>();
      for (ConstructorDef<?> inj : injectable) {
        injectableConstructors.add(serializeConstructorDef(inj));
      }
      List<ClassHierarchyProto.ConstructorDef> otherConstructors = new ArrayList<>();
      for (ConstructorDef<?> other : others) {
        otherConstructors.add(serializeConstructorDef(other));
      }
      List<String> implFullNames = new ArrayList<>();
      for (ClassNode<?> impl : cn.getKnownImplementations()) {
        implFullNames.add(impl.getFullName());
      }
      return newClassNode(
          cn.getName(),
          cn.getFullName(),
          cn.isInjectionCandidate(),
          cn.isExternalConstructor(),
          cn.isUnit(),
          injectableConstructors,
          otherConstructors,
          implFullNames,
          children);
    } else if (n instanceof NamedParameterNode) {
      NamedParameterNode<?> np = (NamedParameterNode<?>) n;
      return newNamedParameterNode(
          np.getName(),
          np.getFullName(),
          np.getSimpleArgName(),
          np.getFullArgName(),
          np.isSet(),
          np.isList(),
          np.getDocumentation(),
          np.getShortName(),
          np.getDefaultInstanceAsStrings(),
          children);
    } else if (n instanceof PackageNode) {
      return newPackageNode(n.getName(), n.getFullName(), children);
    } else {
      throw new IllegalStateException("Encountered unknown type of Node: " + n);
    }
  }
예제 #3
0
  public String toHtmlString(final ClassNode<?> n, final String pack) {
    final String fullName = stripPrefix(n.getFullName(), pack);

    final String type;
    try {
      if (ch.classForName(n.getFullName()).isInterface()) {
        type = "interface";
      } else {
        type = "class";
      }
    } catch (final ClassNotFoundException e) {
      throw new RuntimeException(e);
    }
    final StringBuffer sb = new StringBuffer();

    sb.append("<div class='decl-margin' id='" + n.getFullName() + "'>");
    sb.append("<div class='decl'>");
    sb.append(cell(type, "simpleName") + cell(fullName, FULLNAME));
    final String instance;
    if (n.getDefaultImplementation() != null) {
      instance = " = " + stripPrefix(n.getDefaultImplementation(), pack);
    } else {
      instance = "";
    }
    sb.append(cell(instance, "simpleName"));
    sb.append(cell("", "fullName")); // TODO[REEF-1118]: Support documentation string
    final StringBuffer uses = new StringBuffer();
    for (final String u : getUsesOf(n)) {
      uses.append("<a href='#" + u + "'>" + stripPrefix(u, pack) + "</a> ");
    }
    sb.append(cell(uses, USES));
    final StringBuffer settersStr = new StringBuffer();
    for (final String f : getSettersOf(n)) {
      settersStr.append("<a href='#" + f + "'>" + stripPrefix(f, pack) + "</a> ");
    }
    sb.append(cell(settersStr, SETTERS));
    sb.append("</div>");
    sb.append("</div>");
    return row(sb);
  }
  @Override
  public ClassHierarchy merge(ClassHierarchy ch) {
    if (this == ch) {
      return this;
    }
    if (!(ch instanceof ProtocolBufferClassHierarchy)) {
      throw new UnsupportedOperationException(
          "Cannot merge with class hierarchies of type: " + ch.getClass().getName());
    }

    final ProtocolBufferClassHierarchy pch = (ProtocolBufferClassHierarchy) ch;
    for (final String key : pch.lookupTable.keySet()) {
      if (!this.lookupTable.containsKey(key)) {
        this.lookupTable.put(key, pch.lookupTable.get(key));
      }

      for (final Node n : ch.getNamespace().getChildren()) {
        if (!this.namespace.contains(n.getFullName())) {
          if (n instanceof NamedParameter) {
            final NamedParameterNode np = (NamedParameterNode) n;
            new NamedParameterNodeImpl<>(
                this.namespace,
                np.getName(),
                np.getFullName(),
                np.getFullArgName(),
                np.getSimpleArgName(),
                np.isSet(),
                np.isList(),
                np.getDocumentation(),
                np.getShortName(),
                np.getDefaultInstanceAsStrings());
          } else if (n instanceof ClassNode) {
            final ClassNode cn = (ClassNode) n;
            new ClassNodeImpl(
                namespace,
                cn.getName(),
                cn.getFullName(),
                cn.isUnit(),
                cn.isInjectionCandidate(),
                cn.isExternalConstructor(),
                cn.getInjectableConstructors(),
                cn.getAllConstructors(),
                cn.getDefaultImplementation());
          }
        }
      }
    }
    return this;
  }
예제 #5
0
 private void processDefaultAnnotation(final Class<?> cmb) {
   final DefaultImplementation di = cmb.getAnnotation(DefaultImplementation.class);
   // XXX hack: move to helper method + unify with rest of Tang!
   if (di != null) {
     final String diName =
         di.value() == Void.class ? di.name() : ReflectionUtilities.getFullName(di.value());
     final ClassNode<?> cn = (ClassNode<?>) ch.getNode(cmb);
     final String cnS = cn.getFullName();
     if (!usages.contains(diName, cnS)) {
       usages.put(diName, cnS);
       if (!knownClasses.contains(cn)) {
         knownClasses.add(cn);
       }
     }
   }
 }
예제 #6
0
  @SuppressWarnings("unchecked")
  public Tint(final URL[] jars, final boolean checkTang) {
    final Object[] args = new Object[jars.length + 6];
    for (int i = 0; i < jars.length; i++) {
      args[i] = jars[i];
    }
    args[args.length - 1] = new TypeAnnotationsScanner();
    args[args.length - 2] = new SubTypesScanner();
    args[args.length - 3] = new MethodAnnotationsScanner();
    args[args.length - 4] = new MethodParameterScanner();
    args[args.length - 5] = "com.microsoft";
    args[args.length - 6] = "org.apache";
    final Reflections r = new Reflections(args);
    //    Set<Class<?>> classes = new MonotonicSet<>();
    final Set<String> strings = new TreeSet<>();
    final Set<String> moduleBuilders = new MonotonicSet<>();

    // Workaround bug in Reflections by keeping things stringly typed, and using Tang to parse them.
    //  Set<Constructor<?>> injectConstructors =
    // (Set<Constructor<?>>)(Set)r.getMethodsAnnotatedWith(Inject.class);
    //  for(Constructor<?> c : injectConstructors) {
    //    classes.add(c.getDeclaringClass());
    //  }
    final Set<String> injectConstructors =
        r.getStore().getConstructorsAnnotatedWith(ReflectionUtilities.getFullName(Inject.class));
    for (final String s : injectConstructors) {
      strings.add(s.replaceAll("\\.<.+$", ""));
    }
    final Set<String> parameterConstructors =
        r.getStore()
            .get(MethodParameterScanner.class, ReflectionUtilities.getFullName(Parameter.class));
    for (final String s : parameterConstructors) {
      strings.add(s.replaceAll("\\.<.+$", ""));
    }
    //    Set<Class> r.getConstructorsWithAnyParamAnnotated(Parameter.class);
    //    for(Constructor<?> c : parameterConstructors) {
    //      classes.add(c.getDeclaringClass());
    //    }
    final Set<String> defaultStrings =
        r.getStore()
            .get(
                TypeAnnotationsScanner.class,
                ReflectionUtilities.getFullName(DefaultImplementation.class));
    strings.addAll(defaultStrings);
    strings.addAll(
        r.getStore()
            .get(
                TypeAnnotationsScanner.class,
                ReflectionUtilities.getFullName(NamedParameter.class)));
    strings.addAll(
        r.getStore()
            .get(TypeAnnotationsScanner.class, ReflectionUtilities.getFullName(Unit.class)));
    //    classes.addAll(r.getTypesAnnotatedWith(DefaultImplementation.class));
    //    classes.addAll(r.getTypesAnnotatedWith(NamedParameter.class));
    //    classes.addAll(r.getTypesAnnotatedWith(Unit.class));

    strings.addAll(
        r.getStore().get(SubTypesScanner.class, ReflectionUtilities.getFullName(Name.class)));

    moduleBuilders.addAll(
        r.getStore()
            .get(
                SubTypesScanner.class,
                ReflectionUtilities.getFullName(ConfigurationModuleBuilder.class)));
    //    classes.addAll(r.getSubTypesOf(Name.class));

    ch =
        Tang.Factory.getTang()
            .getDefaultClassHierarchy(
                jars, (Class<? extends ExternalConstructor<?>>[]) new Class[0]);
    //    for(String s : defaultStrings) {
    //      if(classFilter(checkTang, s)) {
    //        try {
    //          ch.getNode(s);
    //        } catch(ClassHierarchyException | NameResolutionException | ClassNotFoundException e)
    // {
    //          System.err.println(e.getMessage());
    //        }
    //      }
    //    }

    for (final String s : strings) {
      if (classFilter(checkTang, s)) {
        try {
          ch.getNode(s);
        } catch (ClassHierarchyException | NameResolutionException e) {
          System.err.println(e.getMessage());
        }
      }
    }
    for (final String s : moduleBuilders) {
      if (classFilter(checkTang, s)) {
        try {
          ch.getNode(s);
        } catch (ClassHierarchyException | NameResolutionException e) {
          e.printStackTrace();
        }
      }
    }

    final NodeVisitor<Node> v =
        new AbstractClassHierarchyNodeVisitor() {

          @Override
          public boolean visit(final NamedParameterNode<?> node) {
            final String nodeS = node.getFullName();
            for (final String s : node.getDefaultInstanceAsStrings()) {
              if (!usages.contains(s, nodeS)) {
                usages.put(s, nodeS);
              }
            }
            return true;
          }

          @Override
          public boolean visit(final PackageNode node) {
            return true;
          }

          @Override
          public boolean visit(final ClassNode<?> node) {
            final String nodeS = node.getFullName();
            for (final ConstructorDef<?> d : node.getInjectableConstructors()) {
              for (final ConstructorArg a : d.getArgs()) {
                if (a.getNamedParameterName() != null
                    && !usages.contains(a.getNamedParameterName(), nodeS)) {
                  usages.put(a.getNamedParameterName(), nodeS);
                }
              }
            }
            if (!knownClasses.contains(node)) {
              knownClasses.add(node);
            }
            return true;
          }
        };
    int numClasses;
    do {
      numClasses = knownClasses.size();

      Walk.preorder(v, null, ch.getNamespace());

      for (final ClassNode<?> cn : knownClasses) {
        try {
          final String s = cn.getFullName();
          if (classFilter(checkTang, s)) {
            final Class<?> c = ch.classForName(s);
            processDefaultAnnotation(c);
            processConfigurationModules(c);
          }
        } catch (final ClassNotFoundException e) {
          e.printStackTrace();
        }
      }

      for (final Entry<Field, ConfigurationModule> entry : modules.entrySet()) {
        final String fS = ReflectionUtilities.getFullName(entry.getKey());
        final Set<NamedParameterNode<?>> nps = entry.getValue().getBoundNamedParameters();
        for (final NamedParameterNode<?> np : nps) {
          final String npS = np.getFullName();
          if (!setters.contains(npS, fS)) {
            setters.put(npS, fS);
          }
        }
      }
    } while (numClasses
        != knownClasses
            .size()); // Note naive fixed point evaluation here.  Semi-naive would be faster.
  }
예제 #7
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>");
      }
    }
  }
예제 #8
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;
  }