Ejemplo n.º 1
0
  /**
   * Compile hierarchies starting with each of the 'types' and return a ClassLoader that can be used
   * to load the compiled classes.
   */
  public ClassLoader compile(Type... types) {
    ClassFilePreprocessor[] cfps = this.postprocessors.toArray(new ClassFilePreprocessor[0]);

    DirectedClassLoader dcl = new DirectedClassLoader(cfps);

    for (Type t : types) {
      for (Map.Entry<String, File> each : compileHierarchy(t).entrySet()) {
        dcl.setLocationFor(each.getKey(), each.getValue());
      }
    }
    return dcl;
  }
Ejemplo n.º 2
0
  /**
   * Compiles a hierarchy, starting at 'type' and return a mapping of the name to the location where
   * the classfile for that type resides.
   */
  private Map<String, File> compileHierarchy(Type type) {
    HashMap<String, File> outputDirs = new HashMap<>();

    File outDir = compileOne(type);
    outputDirs.put(type.getName(), outDir);

    Class superClass = type.getSuperclass();
    if (superClass != null) {
      for (Map.Entry<String, File> each : compileHierarchy(superClass).entrySet()) {
        outputDirs.put(each.getKey(), each.getValue());
      }
    }
    for (Extends ext : type.getSupertypes()) {
      Type iface = ext.getType();
      for (Map.Entry<String, File> each : compileHierarchy(iface).entrySet()) {
        outputDirs.put(each.getKey(), each.getValue());
      }
    }

    return outputDirs;
  }
Ejemplo n.º 3
0
 // Removes all of the elements in the cache and deletes the associated
 // output directories.  This may not actually empty the cache if there
 // are concurrent users of it.
 public static void purgeCache() {
   for (Map.Entry<String, File> entry : cache.entrySet()) {
     cache.remove(entry.getKey());
     deleteDir(entry.getValue());
   }
 }
  public static void addCompilationOptions(
      List<String> options,
      CompileContext context,
      ModuleChunk chunk,
      @Nullable ProcessorConfigProfile profile) {
    if (!isEncodingSet(options)) {
      final CompilerEncodingConfiguration config =
          context.getProjectDescriptor().getEncodingConfiguration();
      final String encoding = config.getPreferredModuleChunkEncoding(chunk);
      if (config.getAllModuleChunkEncodings(chunk).size() > 1) {
        final StringBuilder msgBuilder = new StringBuilder();
        msgBuilder.append("Multiple encodings set for module chunk ").append(chunk.getName());
        if (encoding != null) {
          msgBuilder.append("\n\"").append(encoding).append("\" will be used by compiler");
        }
        context.processMessage(
            new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.INFO, msgBuilder.toString()));
      }
      if (!StringUtil.isEmpty(encoding)) {
        options.add("-encoding");
        options.add(encoding);
      }
    }

    final String langLevel = getLanguageLevel(chunk.getModules().iterator().next());
    if (!StringUtil.isEmpty(langLevel)) {
      options.add("-source");
      options.add(langLevel);
    }

    JpsJavaCompilerConfiguration compilerConfiguration =
        JpsJavaExtensionService.getInstance()
            .getOrCreateCompilerConfiguration(context.getProjectDescriptor().getProject());
    String bytecodeTarget = null;
    int chunkSdkVersion = -1;
    for (JpsModule module : chunk.getModules()) {
      final JpsSdk<JpsDummyElement> sdk = module.getSdk(JpsJavaSdkType.INSTANCE);
      if (sdk != null) {
        final int moduleSdkVersion = convertToNumber(sdk.getVersionString());
        if (moduleSdkVersion != 0 /*could determine the version*/
            && (chunkSdkVersion < 0 || chunkSdkVersion > moduleSdkVersion)) {
          chunkSdkVersion = moduleSdkVersion;
        }
      }

      final String moduleTarget = compilerConfiguration.getByteCodeTargetLevel(module.getName());
      if (moduleTarget == null) {
        continue;
      }
      if (bytecodeTarget == null) {
        bytecodeTarget = moduleTarget;
      } else {
        if (moduleTarget.compareTo(bytecodeTarget) < 0) {
          bytecodeTarget =
              moduleTarget; // use the lower possible target among modules that form the chunk
        }
      }
    }
    if (bytecodeTarget != null) {
      options.add("-target");
      options.add(bytecodeTarget);
    } else {
      if (chunkSdkVersion > 0 && getCompilerSdkVersion(context) > chunkSdkVersion) {
        // force lower bytecode target level to match the version of sdk assigned to this chunk
        options.add("-target");
        options.add("1." + chunkSdkVersion);
      }
    }

    if (profile != null && profile.isEnabled()) {
      // configuring annotation processing
      if (!profile.isObtainProcessorsFromClasspath()) {
        final String processorsPath = profile.getProcessorPath();
        options.add("-processorpath");
        options.add(
            processorsPath == null ? "" : FileUtil.toSystemDependentName(processorsPath.trim()));
      }

      final Set<String> processors = profile.getProcessors();
      if (!processors.isEmpty()) {
        options.add("-processor");
        options.add(StringUtil.join(processors, ","));
      }

      for (Map.Entry<String, String> optionEntry : profile.getProcessorOptions().entrySet()) {
        options.add("-A" + optionEntry.getKey() + "=" + optionEntry.getValue());
      }

      final File srcOutput =
          ProjectPaths.getAnnotationProcessorGeneratedSourcesOutputDir(
              chunk.getModules().iterator().next(), chunk.containsTests(), profile);
      if (srcOutput != null) {
        srcOutput.mkdirs();
        options.add("-s");
        options.add(srcOutput.getPath());
      }
    } else {
      options.add("-proc:none");
    }
  }