@Override
  public List<MdTable> getTables() {
    List<MdTable> tables = new ArrayList<>();

    for (String xmlFile : reflections.getStore().getResources(XML_FILE)) {
      try (InputStream is = getClass().getClassLoader().getResourceAsStream(xmlFile)) {
        List<MdTable> ts = getTables(is);
        int numTables = 0;
        if (ts != null) {

          tables.addAll(ts);
          numTables = ts.size();

          for (MdTable t : ts) {
            t.getMetadata().put(TABLE_SOURCE_KEY, xmlFile);
          }
        }
        log.info(String.format("%d tables loaded from XML file: %s", numTables, xmlFile));
      } catch (Exception ex) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("Error while loading tables from XML file %s: %s", xmlFile, ex));
        }
      }
    }
    return tables;
  }
示例#2
0
  /**
   * collect saved Reflections resources from all urls that contains the given packagePrefix and
   * matches the given resourceNameFilter and de-serializes them using the default serializer {@link
   * org.reflections.serializers.XmlSerializer} or using the optionally supplied optionalSerializer
   *
   * <p>it is preferred to use a designated resource prefix (for example META-INF/reflections but
   * not just META-INF), so that relevant urls could be found much faster
   *
   * @param optionalSerializer - optionally supply one serializer instance. if not specified or
   *     null, {@link org.reflections.serializers.XmlSerializer} will be used
   */
  public static Reflections collect(
      final String packagePrefix,
      final Predicate<String> resourceNameFilter,
      @Nullable Serializer... optionalSerializer) {
    Serializer serializer =
        optionalSerializer != null && optionalSerializer.length == 1
            ? optionalSerializer[0]
            : new XmlSerializer();

    Collection<URL> urls = ClasspathHelper.forPackage(packagePrefix);
    if (urls.isEmpty()) return null;
    long start = System.currentTimeMillis();
    final Reflections reflections = new Reflections();
    Iterable<Vfs.File> files = Vfs.findFiles(urls, packagePrefix, resourceNameFilter);
    for (final Vfs.File file : files) {
      InputStream inputStream = null;
      try {
        inputStream = file.openInputStream();
        reflections.merge(serializer.read(inputStream));
      } catch (IOException e) {
        throw new ReflectionsException("could not merge " + file, e);
      } finally {
        close(inputStream);
      }
    }

    if (log != null) {
      Store store = reflections.getStore();
      int keys = 0;
      int values = 0;
      for (String index : store.keySet()) {
        keys += store.get(index).keySet().size();
        values += store.get(index).size();
      }

      log.info(
          format(
              "Reflections took %d ms to collect %d url%s, producing %d keys and %d values [%s]",
              System.currentTimeMillis() - start,
              urls.size(),
              urls.size() > 1 ? "s" : "",
              keys,
              values,
              Joiner.on(", ").join(urls)));
    }
    return reflections;
  }
  public String toString(Reflections reflections) {
    if (reflections.getStore().get(TypesScanner.class).isEmpty()
        || reflections.getStore().get(TypeElementsScanner.class).isEmpty()) {
      if (log != null)
        log.warn("JavaCodeSerializer needs TypeScanner and TypeElemenetsScanner configured");
    }

    StringBuilder sb = new StringBuilder();

    List<String> prevPaths = Lists.newArrayList();
    int indent = 1;

    List<String> keys = Lists.newArrayList(reflections.getStore().get(TypesScanner.class).keySet());
    Collections.sort(keys);
    for (String fqn : keys) {
      List<String> typePaths = Lists.newArrayList(fqn.split("\\."));

      // skip indention
      int i = 0;
      while (i < Math.min(typePaths.size(), prevPaths.size())
          && typePaths.get(i).equals(prevPaths.get(i))) {
        i++;
      }

      // indent left
      for (int j = prevPaths.size(); j > i; j--) {
        sb.append(repeat("\t", --indent)).append("}\n");
      }

      // indent right - add packages
      for (int j = i; j < typePaths.size() - 1; j++) {
        sb.append(repeat("\t", indent++))
            .append("public interface ")
            .append(getNonDuplicateName(typePaths.get(j), typePaths, j))
            .append(" extends IPackage")
            .append(" {\n");
      }

      // indent right - add class
      String className = typePaths.get(typePaths.size() - 1);

      // get fields and methods
      List<String> fields = Lists.newArrayList();
      final Multimap<String, String> methods =
          Multimaps.newSetMultimap(
              new HashMap<String, Collection<String>>(),
              new Supplier<Set<String>>() {
                public Set<String> get() {
                  return Sets.newHashSet();
                }
              });

      for (String element : reflections.getStore().get(TypeElementsScanner.class, fqn)) {
        if (element.contains("(")) {
          // method
          if (!element.startsWith("<")) {
            int i1 = element.indexOf('(');
            String name = element.substring(0, i1);
            String params = element.substring(i1 + 1, element.indexOf(")"));

            String paramsDescriptor = "";
            if (params.length() != 0) {
              paramsDescriptor =
                  tokenSeparator
                      + params
                          .replace('.', pathSeparator)
                          .replace(", ", tokenSeparator)
                          .replace("[]", arrayDescriptor);
            }
            String normalized = name + paramsDescriptor;
            methods.put(name, normalized);
          }
        } else {
          // field
          fields.add(element);
        }
      }

      // add class and it's fields and methods
      sb.append(repeat("\t", indent++))
          .append("public interface ")
          .append(getNonDuplicateName(className, typePaths, typePaths.size() - 1))
          .append(" extends IClass")
          .append(" {\n");

      // add fields
      if (!fields.isEmpty()) {
        for (String field : fields) {
          sb.append(repeat("\t", indent))
              .append("public interface ")
              .append(getNonDuplicateName(field, typePaths))
              .append(" extends IField")
              .append(" {}\n");
        }
      }

      // add methods
      if (!methods.isEmpty()) {
        for (Map.Entry<String, String> entry : methods.entries()) {
          String simpleName = entry.getKey();
          String normalized = entry.getValue();

          String methodName = methods.get(simpleName).size() == 1 ? simpleName : normalized;

          methodName =
              getNonDuplicateName(
                  methodName,
                  fields); // because fields and methods are both inners of the type, they can't
                           // duplicate

          sb.append(repeat("\t", indent))
              .append("public interface ")
              .append(getNonDuplicateName(methodName, typePaths))
              .append(" extends IMethod")
              .append(" {}\n");
        }
      }

      prevPaths = typePaths;
    }

    // close indention
    for (int j = prevPaths.size(); j >= 1; j--) {
      sb.append(repeat("\t", j)).append("}\n");
    }

    return sb.toString();
  }
示例#4
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.
  }