public ContextImpl(
     @NotNull JavaCompiler compiler,
     @NotNull DiagnosticOutputConsumer outConsumer,
     @NotNull OutputFileConsumer sink,
     CanceledStatus canceledStatus,
     boolean canUseOptimizedmanager) {
   myOutConsumer = outConsumer;
   myOutputFileSink = sink;
   myCanceledStatus = canceledStatus;
   StandardJavaFileManager stdManager = null;
   if (canUseOptimizedmanager) {
     final Class<StandardJavaFileManager> optimizedManagerClass =
         ClasspathBootstrap.getOptimizedFileManagerClass();
     if (optimizedManagerClass != null) {
       try {
         stdManager = optimizedManagerClass.newInstance();
       } catch (Throwable e) {
         if (SystemInfo.isWindows) {
           System.err.println(
               "Failed to load JPS optimized file manager for javac: " + e.getMessage());
         }
       }
     }
   }
   if (stdManager != null) {
     myStdManager = stdManager;
   } else {
     myStdManager = compiler.getStandardFileManager(outConsumer, Locale.US, null);
   }
 }
  /**
   * (Compiles and) loads all custom abilities in the given directory.
   *
   * @param classDir a directory
   */
  private static void loadClasses(File classDir) {
    // Grab the class loader
    ClassLoader loader = getLoader(classDir);
    if (loader == null) return;

    for (File file : classDir.listFiles()) {
      String filename = file.getName();

      // Only load .class files.
      int dot = filename.lastIndexOf(".class");
      if (dot < 0) continue;

      // Trim off the .class extension
      String name = filename.substring(0, file.getName().lastIndexOf("."));

      try {
        // Load the class
        Class<?> cls = loader.loadClass(name);

        // Verify that it's an Ability, then register it
        if (Ability.class.isAssignableFrom(cls)) {
          register(cls.asSubclass(Ability.class), true);
        }
      } catch (Exception e) {
      }
    }
  }
 /**
  * Get an instance of an ability by alias
  *
  * @param alias the alias of an ability
  * @return a new Ability object, or null
  */
 public static Ability getAbility(String alias) {
   try {
     Class<? extends Ability> cls = abilities.get(alias.toLowerCase().replaceAll("[-_.]", ""));
     return cls.newInstance();
   } catch (Exception e) {
     return null;
   }
 }
 private static List<JavaSourceTransformer> getSourceTransformers() {
   final Class<JavaSourceTransformer> transformerClass = JavaSourceTransformer.class;
   final ServiceLoader<JavaSourceTransformer> loader =
       ServiceLoader.load(transformerClass, transformerClass.getClassLoader());
   final List<JavaSourceTransformer> transformers = new ArrayList<JavaSourceTransformer>();
   for (JavaSourceTransformer t : loader) {
     transformers.add(t);
   }
   return transformers;
 }
Example #5
0
 private static void assertTestClassPresentByMetadata(
     @NotNull Class<?> outerClass, @NotNull File testDataDir) {
   for (Class<?> nestedClass : outerClass.getDeclaredClasses()) {
     TestMetadata testMetadata = nestedClass.getAnnotation(TestMetadata.class);
     if (testMetadata != null && testMetadata.value().equals(getFilePath(testDataDir))) {
       return;
     }
   }
   Assert.fail(
       "Test data directory missing from the generated test class: "
           + testDataDir
           + "\n"
           + PLEASE_REGENERATE_TESTS);
 }
Example #6
0
  public static void assertAllTestsPresentByMetadata(
      @NotNull Class<?> testCaseClass,
      @NotNull File testDataDir,
      @NotNull Pattern filenamePattern,
      boolean recursive,
      @NotNull String... excludeDirs) {
    TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
    Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
    String rootPath = testClassMetadata.value();
    File rootFile = new File(rootPath);

    Set<String> filePaths = collectPathsMetadata(testCaseClass);
    Set<String> exclude = SetsKt.setOf(excludeDirs);

    File[] files = testDataDir.listFiles();
    if (files != null) {
      for (File file : files) {
        if (file.isDirectory()) {
          if (recursive
              && containsTestData(file, filenamePattern)
              && !exclude.contains(file.getName())) {
            assertTestClassPresentByMetadata(testCaseClass, file);
          }
        } else if (filenamePattern.matcher(file.getName()).matches()) {
          assertFilePathPresent(file, rootFile, filePaths);
        }
      }
    }
  }
 static {
   try {
     Field freelistRef;
     try {
       // trying jdk 6
       freelistRef =
           Class.forName("com.sun.tools.javac.util.Name$Table").getDeclaredField("freelist");
     } catch (Exception e) {
       // trying jdk 7
       freelistRef =
           Class.forName("com.sun.tools.javac.util.SharedNameTable")
               .getDeclaredField("freelist");
     }
     freelistRef.setAccessible(true);
     freelistField = freelistRef;
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
Example #8
0
 private static Set<String> collectMethodsMetadata(Class<?> testCaseClass) {
   Set<String> filePaths = Sets.newHashSet();
   for (Method method : testCaseClass.getDeclaredMethods()) {
     TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
     if (testMetadata != null) {
       filePaths.add(testMetadata.value());
     }
   }
   return filePaths;
 }
  /**
   * Register an ability by its class object
   *
   * @param cls the ability class
   */
  private static void register(Class<? extends Ability> cls, boolean announce) {
    AbilityInfo info = cls.getAnnotation(AbilityInfo.class);
    if (info == null) return;

    // Map all the aliases
    for (String alias : info.aliases()) {
      abilities.put(alias, cls);
    }

    // Announce custom abilities
    if (announce) Messenger.info("Loaded custom ability '" + info.name() + "'");
  }
Example #10
0
  public static void assertAllTestsPresentInSingleGeneratedClass(
      @NotNull Class<?> testCaseClass,
      @NotNull File testDataDir,
      @NotNull final Pattern filenamePattern) {
    TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
    Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
    final File rootFile = new File(testClassMetadata.value());

    final Set<String> filePaths = collectPathsMetadata(testCaseClass);

    FileUtil.processFilesRecursively(
        testDataDir,
        new Processor<File>() {
          @Override
          public boolean process(File file) {
            if (file.isFile() && filenamePattern.matcher(file.getName()).matches()) {
              assertFilePathPresent(file, rootFile, filePaths);
            }

            return true;
          }
        });
  }