示例#1
0
  @Nullable
  public static String getAnnotationProcessorsGenerationPath(Module module) {
    final CompilerConfiguration config = CompilerConfiguration.getInstance(module.getProject());

    final String sourceDirName = config.getGeneratedSourceDirName(module);
    if (sourceDirName != null && sourceDirName.length() > 0) {
      final String[] roots = ModuleRootManager.getInstance(module).getContentRootUrls();
      if (roots.length == 0) {
        return null;
      }
      if (roots.length > 1) {
        Arrays.sort(roots, URLS_COMPARATOR);
      }
      return VirtualFileManager.extractPath(roots[0]) + "/" + sourceDirName;
    }

    final CompilerProjectExtension extension =
        CompilerProjectExtension.getInstance(module.getProject());
    if (extension == null) {
      return null;
    }
    final String url = extension.getCompilerOutputUrl();
    if (url == null) {
      return null;
    }
    return VirtualFileManager.extractPath(url)
        + "/"
        + DEFAULT_GENERATED_DIR_NAME
        + "/"
        + module.getName().toLowerCase();
  }
 private boolean needTransformCopying(CompileScope compileScope) {
   final CompilerConfiguration configuration = CompilerConfiguration.getInstance(myProject);
   final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
   for (VirtualFile file :
       FilenameIndex.getVirtualFilesByName(
           myProject, AST_TRANSFORM_FILE_NAME, GlobalSearchScope.projectScope(myProject))) {
     if (compileScope.belongs(file.getUrl())
         && index.isInSource(file)
         && !configuration.isResourceFile(file)) {
       return true;
     }
   }
   return false;
 }
示例#3
0
  private void openConfigurationDialog() {
    final CompilerConfigurationImpl configuration =
        (CompilerConfigurationImpl) CompilerConfiguration.getInstance(myProject);
    final Collection<BackendCompiler> compilers = configuration.getRegisteredJavaCompilers();
    final BackendCompiler defaultCompiler = configuration.getDefaultCompiler();
    final JavaCompilersTab compilersTab =
        new JavaCompilersTab(myProject, compilers, defaultCompiler);

    ShowSettingsUtil.getInstance().editConfigurable(myProject, compilersTab);
  }
 public static Collection<Trinity<Artifact, PackagingElementPath, String>>
     findContainingArtifactsWithOutputPaths(
         @NotNull final VirtualFile file, @NotNull Project project) {
   final boolean isResourceFile = CompilerConfiguration.getInstance(project).isResourceFile(file);
   final List<Trinity<Artifact, PackagingElementPath, String>> artifacts =
       new ArrayList<Trinity<Artifact, PackagingElementPath, String>>();
   final PackagingElementResolvingContext context =
       ArtifactManager.getInstance(project).getResolvingContext();
   for (final Artifact artifact : ArtifactManager.getInstance(project).getArtifacts()) {
     processPackagingElements(
         artifact,
         null,
         new PackagingElementProcessor<PackagingElement<?>>() {
           @Override
           public boolean process(
               @NotNull PackagingElement<?> element, @NotNull PackagingElementPath path) {
             if (element instanceof FileOrDirectoryCopyPackagingElement<?>) {
               final VirtualFile root = ((FileOrDirectoryCopyPackagingElement) element).findFile();
               if (root != null && VfsUtil.isAncestor(root, file, false)) {
                 final String relativePath;
                 if (root.equals(file) && element instanceof FileCopyPackagingElement) {
                   relativePath = ((FileCopyPackagingElement) element).getOutputFileName();
                 } else {
                   relativePath = VfsUtilCore.getRelativePath(file, root, '/');
                 }
                 artifacts.add(Trinity.create(artifact, path, relativePath));
                 return false;
               }
             } else if (isResourceFile && element instanceof ModuleOutputPackagingElement) {
               final String relativePath =
                   getRelativePathInSources(file, (ModuleOutputPackagingElement) element, context);
               if (relativePath != null) {
                 artifacts.add(Trinity.create(artifact, path, relativePath));
                 return false;
               }
             }
             return true;
           }
         },
         context,
         true);
   }
   return artifacts;
 }
  private void excludeFromCompilationArchetypeResources() {
    VirtualFile directoryFile = myMavenProject.getDirectoryFile();

    VirtualFile archetypeResourcesDir =
        VfsUtil.findRelativeFile(directoryFile, "src", "main", "resources", "archetype-resources");

    if (archetypeResourcesDir != null) {
      Project project = myModule.getProject();

      CompilerConfigurationImpl compilerConfiguration =
          (CompilerConfigurationImpl) CompilerConfiguration.getInstance(project);

      if (!compilerConfiguration.isExcludedFromCompilation(archetypeResourcesDir)) {
        ExcludedEntriesConfiguration cfg = compilerConfiguration.getExcludedEntriesConfiguration();

        cfg.addExcludeEntryDescription(
            new ExcludeEntryDescription(archetypeResourcesDir, true, false, project));
      }
    }
  }
示例#6
0
  public JavaCompilersTab(
      final Project project,
      Collection<BackendCompiler> compilers,
      BackendCompiler defaultCompiler) {
    myDefaultCompiler = defaultCompiler;
    myCompilerConfiguration =
        (CompilerConfigurationImpl) CompilerConfiguration.getInstance(project);
    myConfigurables = new ArrayList<Configurable>(compilers.size());

    myCardLayout = new CardLayout();
    myContentPanel.setLayout(myCardLayout);

    for (BackendCompiler compiler : compilers) {
      Configurable configurable = compiler.createConfigurable();
      myConfigurables.add(configurable);

      myContentPanel.add(configurable.createComponent(), compiler.getId());
    }
    myCompiler.setModel(new DefaultComboBoxModel(new Vector(compilers)));
    myCompiler.setRenderer(
        new DefaultListCellRenderer() {
          public Component getListCellRendererComponent(
              JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel component =
                (JLabel)
                    super.getListCellRendererComponent(
                        list, value, index, isSelected, cellHasFocus);
            component.setText(((BackendCompiler) value).getPresentableName());
            return component;
          }
        });
    myCompiler.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            BackendCompiler compiler = (BackendCompiler) myCompiler.getSelectedItem();
            if (compiler == null) return;
            selectCompiler(compiler);
          }
        });
  }
  public boolean validateConfiguration(CompileScope compileScope) {
    VirtualFile[] files = compileScope.getFiles(GroovyFileType.GROOVY_FILE_TYPE, true);
    if (files.length == 0) return true;

    final Set<String> scriptExtensions = GroovyFileTypeLoader.getCustomGroovyScriptExtensions();

    final CompilerManager compilerManager = CompilerManager.getInstance(myProject);
    Set<Module> modules = new HashSet<Module>();
    for (VirtualFile file : files) {
      if (scriptExtensions.contains(file.getExtension())
          || compilerManager.isExcludedFromCompilation(file)
          || CompilerConfiguration.getInstance(myProject).isResourceFile(file)) {
        continue;
      }

      ProjectRootManager rootManager = ProjectRootManager.getInstance(myProject);
      Module module = rootManager.getFileIndex().getModuleForFile(file);
      if (module != null) {
        modules.add(module);
      }
    }

    Set<Module> nojdkModules = new HashSet<Module>();
    for (Module module : modules) {
      if (!GroovyUtils.isAcceptableModuleType(ModuleType.get(module))) continue;
      final Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
      if (sdk == null || !(sdk.getSdkType() instanceof JavaSdkType)) {
        nojdkModules.add(module);
        continue;
      }

      if (!LibrariesUtil.hasGroovySdk(module)) {
        if (!GroovyConfigUtils.getInstance().tryToSetUpGroovyFacetOntheFly(module)) {
          Messages.showErrorDialog(
              myProject,
              GroovyBundle.message("cannot.compile.groovy.files.no.facet", module.getName()),
              GroovyBundle.message("cannot.compile"));
          ModulesConfigurator.showDialog(
              module.getProject(), module.getName(), ClasspathEditor.NAME);
          return false;
        }
      }
    }

    if (!nojdkModules.isEmpty()) {
      final Module[] noJdkArray = nojdkModules.toArray(new Module[nojdkModules.size()]);
      if (noJdkArray.length == 1) {
        Messages.showErrorDialog(
            myProject,
            GroovyBundle.message("cannot.compile.groovy.files.no.sdk", noJdkArray[0].getName()),
            GroovyBundle.message("cannot.compile"));
      } else {
        StringBuilder modulesList = new StringBuilder();
        for (int i = 0; i < noJdkArray.length; i++) {
          if (i > 0) modulesList.append(", ");
          modulesList.append(noJdkArray[i].getName());
        }
        Messages.showErrorDialog(
            myProject,
            GroovyBundle.message("cannot.compile.groovy.files.no.sdk.mult", modulesList.toString()),
            GroovyBundle.message("cannot.compile"));
      }
      return false;
    }

    final GroovyCompilerConfiguration configuration =
        GroovyCompilerConfiguration.getInstance(myProject);
    if (!configuration.transformsOk && needTransformCopying(compileScope)) {
      final int result =
          Messages.showYesNoDialog(
              myProject,
              "You seem to have global Groovy AST transformations defined in your project,\n"
                  + "but they won't be applied to your code because they are not marked as compiler resources.\n"
                  + "Do you want to add them to compiler resource list?\n"
                  + "(you can do it yourself later in Settings | Compiler | Resource patterns)",
              "AST Transformations found",
              GroovyIcons.GROOVY_ICON_32x32);
      if (result == 0) {
        CompilerConfiguration.getInstance(myProject)
            .addResourceFilePattern(AST_TRANSFORM_FILE_NAME);
      } else {
        configuration.transformsOk = true;
      }
    }

    return true;
  }
  private void configAnnotationProcessors() {
    if (Boolean.parseBoolean(System.getProperty("idea.maven.keep.annotation.processors"))) return;

    Sdk sdk = ModuleRootManager.getInstance(myModule).getSdk();
    if (sdk != null) {
      String versionString = sdk.getVersionString();
      if (versionString != null) {
        if (versionString.contains("1.5")
            || versionString.contains("1.4")
            || versionString.contains("1.3")
            || versionString.contains("1.2")) {
          return;
        }
      }
    }

    CompilerConfigurationImpl compilerConfiguration =
        (CompilerConfigurationImpl) CompilerConfiguration.getInstance(myModule.getProject());

    ProcessorConfigProfile currentProfile =
        compilerConfiguration.getAnnotationProcessingConfiguration(myModule);

    String moduleProfileName = PROFILE_PREFIX + myModule.getName();

    if (currentProfile != compilerConfiguration.getDefaultProcessorProfile()
        && !MAVEN_DEFAULT_ANNOTATION_PROFILE.equals(currentProfile.getName())
        && !moduleProfileName.equals(currentProfile.getName())) {
      return;
    }

    ProcessorConfigProfile moduleProfile =
        compilerConfiguration.findModuleProcessorProfile(moduleProfileName);

    ProcessorConfigProfile defaultMavenProfile =
        compilerConfiguration.findModuleProcessorProfile(MAVEN_DEFAULT_ANNOTATION_PROFILE);

    if (shouldEnableAnnotationProcessors()) {
      String annotationProcessorDirectory = getRelativeAnnotationProcessorDirectory(false);
      if (annotationProcessorDirectory == null) {
        annotationProcessorDirectory = DEFAULT_ANNOTATION_PATH_OUTPUT;
      }

      String testAnnotationProcessorDirectory = getRelativeAnnotationProcessorDirectory(true);
      if (testAnnotationProcessorDirectory == null) {
        testAnnotationProcessorDirectory = DEFAULT_TEST_ANNOTATION_OUTPUT;
      }

      Map<String, String> options = myMavenProject.getAnnotationProcessorOptions();

      List<String> processors = myMavenProject.getDeclaredAnnotationProcessors();

      if (processors == null
          && options.isEmpty()
          && DEFAULT_ANNOTATION_PATH_OUTPUT.equals(annotationProcessorDirectory.replace('\\', '/'))
          && DEFAULT_TEST_ANNOTATION_OUTPUT.equals(
              testAnnotationProcessorDirectory.replace('\\', '/'))) {
        if (moduleProfile != null) {
          compilerConfiguration.removeModuleProcessorProfile(moduleProfile);
        }

        if (defaultMavenProfile == null) {
          defaultMavenProfile = new ProcessorConfigProfileImpl(MAVEN_DEFAULT_ANNOTATION_PROFILE);
          defaultMavenProfile.setEnabled(true);
          defaultMavenProfile.setOutputRelativeToContentRoot(true);
          defaultMavenProfile.setObtainProcessorsFromClasspath(true);
          defaultMavenProfile.setGeneratedSourcesDirectoryName(
              DEFAULT_ANNOTATION_PATH_OUTPUT, false);
          defaultMavenProfile.setGeneratedSourcesDirectoryName(
              DEFAULT_TEST_ANNOTATION_OUTPUT, true);
          compilerConfiguration.addModuleProcessorProfile(defaultMavenProfile);
        }

        defaultMavenProfile.addModuleName(myModule.getName());
      } else {
        if (defaultMavenProfile != null) {
          defaultMavenProfile.removeModuleName(myModule.getName());

          if (defaultMavenProfile.getModuleNames().isEmpty()) {
            compilerConfiguration.removeModuleProcessorProfile(defaultMavenProfile);
          }
        }

        if (moduleProfile == null) {
          moduleProfile = new ProcessorConfigProfileImpl(moduleProfileName);
          moduleProfile.setOutputRelativeToContentRoot(true);
          moduleProfile.setEnabled(true);
          moduleProfile.setObtainProcessorsFromClasspath(true);
          moduleProfile.addModuleName(myModule.getName());
          compilerConfiguration.addModuleProcessorProfile(moduleProfile);
        }

        moduleProfile.setGeneratedSourcesDirectoryName(annotationProcessorDirectory, false);
        moduleProfile.setGeneratedSourcesDirectoryName(testAnnotationProcessorDirectory, true);

        moduleProfile.clearProcessorOptions();
        for (Map.Entry<String, String> entry : options.entrySet()) {
          moduleProfile.setOption(entry.getKey(), entry.getValue());
        }

        moduleProfile.clearProcessors();

        if (processors != null) {
          for (String processor : processors) {
            moduleProfile.addProcessor(processor);
          }
        }
      }
    } else {
      if (defaultMavenProfile != null) {
        defaultMavenProfile.removeModuleName(myModule.getName());

        if (defaultMavenProfile.getModuleNames().isEmpty()) {
          compilerConfiguration.removeModuleProcessorProfile(defaultMavenProfile);
        }
      }

      if (moduleProfile != null) {
        compilerConfiguration.removeModuleProcessorProfile(moduleProfile);
      }
    }
  }