コード例 #1
0
  /**
   * Add a Java Nature to a project when creating
   *
   * @param project
   * @throws CoreException
   */
  private static void addJavaNature(final IProject project) throws CoreException {
    if (!project.hasNature(JavaCore.NATURE_ID)) {
      final IProjectDescription description = project.getDescription();
      final String[] prevNatures = description.getNatureIds();
      final String[] newNatures = new String[prevNatures.length + 1];
      System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
      newNatures[prevNatures.length] = JavaCore.NATURE_ID;
      description.setNatureIds(newNatures);
      project.setDescription(description, null);
      IFolder sourceFolder = project.getFolder("/src");
      sourceFolder.create(true, true, null);

      IJavaProject javaProject = JavaCore.create(project);
      javaProject.setRawClasspath(
          new IClasspathEntry[] {
            JavaCore.newSourceEntry(sourceFolder.getFullPath()),
            JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER"))
          },
          null);

      @SuppressWarnings("unchecked")
      Hashtable<String, String> javaOptions = JavaCore.getOptions();
      javaOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
      javaOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
      javaOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
      javaProject.setOptions(javaOptions);
    }
  }
コード例 #2
0
  /** Test custom encoding - using getOptions() */
  public void test05() throws CoreException {
    try {
      IJavaProject projectA =
          this.createJavaProject(
              "A",
              new String[] {}, // source folders
              new String[] {}, // lib folders
              new String[] {}, // projects
              "");
      IJavaProject projectB =
          this.createJavaProject(
              "B",
              new String[] {}, // source folders
              new String[] {}, // lib folders
              new String[] {}, // projects
              "");

      String globalEncoding = JavaCore.getOption(JavaCore.CORE_ENCODING);

      Hashtable options = new Hashtable();
      options.put(JavaCore.CORE_ENCODING, "custom");
      projectA.setOptions(options);

      // check project A custom options
      assertEquals(
          "projA:unexpected custom encoding",
          "custom",
          projectA.getOptions(true).get(JavaCore.CORE_ENCODING));

      // check project B custom options	(should be none, indicating it sees global ones only)
      assertEquals(
          "projB:unexpected custom encoding",
          globalEncoding,
          projectB.getOptions(true).get(JavaCore.CORE_ENCODING));

      // flush custom options - project A should revert to global ones
      projectA.setOptions(null);
      assertEquals(
          "projA:unexpected reverted encoding",
          globalEncoding,
          projectA.getOptions(true).get(JavaCore.CORE_ENCODING));

    } finally {
      this.deleteProject("A");
      this.deleteProject("B");
    }
  }
コード例 #3
0
  public static IPackageFragmentRoot addRTJar13(IJavaProject jproject) throws CoreException {
    IPath[] rtJarPath = findRtJar(RT_STUBS_13);

    Map options = jproject.getOptions(false);
    JavaProjectHelper.set13CompilerOptions(options);
    jproject.setOptions(options);

    return addLibrary(jproject, rtJarPath[0], rtJarPath[1], rtJarPath[2]);
  }
コード例 #4
0
ファイル: JavaUtils.java プロジェクト: alvaroc1/eclim
  /**
   * Sets the java version for which all source is to be compatable with.
   *
   * @param project The java project.
   * @param version The java version.
   */
  @SuppressWarnings("unchecked")
  public static void setCompilerSourceCompliance(IJavaProject project, String version)
      throws Exception {
    // using project.setOption(String,String) doesn't save the setting.
    Map<String, String> options = project.getOptions(false);
    options.put(JavaCore.COMPILER_SOURCE, version);
    options.put(JavaCore.COMPILER_COMPLIANCE, version);
    options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, version);

    project.setOptions(options);
  }
コード例 #5
0
 public static void makeJava5Compliant(IJavaProject javaProject) {
   @SuppressWarnings("unchecked")
   Map<String, String> options = javaProject.getOptions(false);
   options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
   options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
   options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
   options.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
   options.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR);
   options.put(JavaCore.COMPILER_CODEGEN_INLINE_JSR_BYTECODE, JavaCore.ENABLED);
   options.put(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, JavaCore.GENERATE);
   options.put(JavaCore.COMPILER_LINE_NUMBER_ATTR, JavaCore.GENERATE);
   options.put(JavaCore.COMPILER_SOURCE_FILE_ATTR, JavaCore.GENERATE);
   options.put(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL, JavaCore.PRESERVE);
   javaProject.setOptions(options);
 }
コード例 #6
0
 /**
  * Sets the compiler options to 1.4 for the given project.
  *
  * @param project the java project
  */
 public static void set14CompilerOptions(IJavaProject project) {
   Map options = project.getOptions(false);
   JavaProjectHelper.set14CompilerOptions(options);
   project.setOptions(options);
 }
コード例 #7
0
  public void testASTRewriteExample() throws Exception {
    // create a new project
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("Test");
    project.create(null);
    project.open(null);
    try {
      // set the Java nature and Java build path
      IProjectDescription description = project.getDescription();
      description.setNatureIds(new String[] {JavaCore.NATURE_ID});
      project.setDescription(description, null);

      IJavaProject javaProject = JavaCore.create(project);

      // build path is: project as source folder and JRE container
      IClasspathEntry[] cpentry =
          new IClasspathEntry[] {
            JavaCore.newSourceEntry(javaProject.getPath()),
            JavaRuntime.getDefaultJREContainerEntry()
          };
      javaProject.setRawClasspath(cpentry, javaProject.getPath(), null);
      Map<String, String> options = new HashMap<>();
      options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE);
      options.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4");
      javaProject.setOptions(options);

      // create a test file
      IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(project);
      IPackageFragment pack1 = root.createPackageFragment("test1", false, null);
      StringBuffer buf = new StringBuffer();
      buf.append("package test1;\n");
      buf.append("public class E {\n");
      buf.append("    public void foo(int i) {\n");
      buf.append("        while (--i > 0) {\n");
      buf.append("            System.beep();\n");
      buf.append("        }\n");
      buf.append("    }\n");
      buf.append("}\n");
      ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

      // create an AST
      ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
      parser.setSource(cu);
      parser.setResolveBindings(false);
      CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
      AST ast = astRoot.getAST();

      // create the descriptive ast rewriter
      ASTRewrite rewrite = ASTRewrite.create(ast);

      // get the block node that contains the statements in the method body
      TypeDeclaration typeDecl = (TypeDeclaration) astRoot.types().get(0);
      MethodDeclaration methodDecl = typeDecl.getMethods()[0];
      Block block = methodDecl.getBody();

      // create new statements to insert
      MethodInvocation newInv1 = ast.newMethodInvocation();
      newInv1.setName(ast.newSimpleName("bar1"));
      Statement newStatement1 = ast.newExpressionStatement(newInv1);

      MethodInvocation newInv2 = ast.newMethodInvocation();
      newInv2.setName(ast.newSimpleName("bar2"));
      Statement newStatement2 = ast.newExpressionStatement(newInv2);

      // describe that the first node is inserted as first statement in block, the other one as last
      // statement
      // note: AST is not modified by this
      ListRewrite listRewrite = rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
      listRewrite.insertFirst(newStatement1, null);
      listRewrite.insertLast(newStatement2, null);

      // evaluate the text edits corresponding to the described changes. AST and CU still
      // unmodified.
      TextEdit res = rewrite.rewriteAST();

      // apply the text edits to the compilation unit
      Document document = new Document(cu.getSource());
      res.apply(document);
      cu.getBuffer().setContents(document.get());

      // test result
      String preview = cu.getSource();

      buf = new StringBuffer();
      buf.append("package test1;\n");
      buf.append("public class E {\n");
      buf.append("    public void foo(int i) {\n");
      buf.append("        bar1();\n");
      buf.append("        while (--i > 0) {\n");
      buf.append("            System.beep();\n");
      buf.append("        }\n");
      buf.append("        bar2();\n");
      buf.append("    }\n");
      buf.append("}\n");
      assertEquals(preview, buf.toString());
    } finally {
      project.delete(true, null);
    }
  }
コード例 #8
0
  /** Test persistence of project custom options */
  public void test01() throws CoreException {
    try {
      IJavaProject projectA =
          this.createJavaProject(
              "A",
              new String[] {}, // source folders
              new String[] {}, // lib folders
              new String[] {}, // projects
              "");
      IJavaProject projectB =
          this.createJavaProject(
              "B",
              new String[] {}, // source folders
              new String[] {}, // lib folders
              new String[] {}, // projects
              "");

      Hashtable options = new Hashtable();
      options.put(JavaCore.COMPILER_PB_DEPRECATION_IN_DEPRECATED_CODE, JavaCore.DISABLED);
      options.put(JavaCore.COMPILER_COMPLIANCE, "8.0");
      options.put(JavaCore.COMPILER_PB_HIDDEN_CATCH_BLOCK, JavaCore.ERROR);
      JavaCore.setOptions(options);

      options.clear();
      options.put(JavaCore.COMPILER_PB_DEPRECATION_IN_DEPRECATED_CODE, JavaCore.ENABLED);
      options.put(JavaCore.COMPILER_COMPLIANCE, "10.0");
      projectA.setOptions(options);

      // check project A custom options
      assertEquals(
          "projA:unexpected custom value for deprecation option",
          JavaCore.ENABLED,
          projectA.getOption(JavaCore.COMPILER_PB_DEPRECATION_IN_DEPRECATED_CODE, true));
      assertEquals(
          "projA:unexpected custom value for compliance option",
          "10.0",
          projectA.getOption(JavaCore.COMPILER_COMPLIANCE, true));
      assertEquals(
          "projA:unexpected inherited value1 for hidden-catch option",
          JavaCore.ERROR,
          projectA.getOption(JavaCore.COMPILER_PB_HIDDEN_CATCH_BLOCK, true));

      // check project B custom options	(should be none, indicating it sees global ones only)
      assertEquals(
          "projB:unexpected custom value for deprecation option",
          JavaCore.DISABLED,
          projectB.getOption(JavaCore.COMPILER_PB_DEPRECATION_IN_DEPRECATED_CODE, true));
      assertEquals(
          "projB:unexpected custom value for compliance option",
          "8.0",
          projectB.getOption(JavaCore.COMPILER_COMPLIANCE, true));
      assertEquals(
          "projB:unexpected inherited value for hidden-catch option",
          JavaCore.ERROR,
          projectB.getOption(JavaCore.COMPILER_PB_HIDDEN_CATCH_BLOCK, true));

      // flush custom options - project A should revert to global ones
      projectA.setOptions(null);
      assertEquals(
          "projA:unexpected reverted value for deprecation option",
          JavaCore.DISABLED,
          projectA.getOption(JavaCore.COMPILER_PB_DEPRECATION_IN_DEPRECATED_CODE, true));
      assertEquals(
          "projA:unexpected reverted value for compliance option",
          "8.0",
          projectA.getOption(JavaCore.COMPILER_COMPLIANCE, true));
      assertEquals(
          "projA:unexpected inherited value2 for hidden-catch option",
          JavaCore.ERROR,
          projectA.getOption(JavaCore.COMPILER_PB_HIDDEN_CATCH_BLOCK, true));

    } finally {
      this.deleteProject("A");
      this.deleteProject("B");
    }
  }
コード例 #9
0
  /** Empty custom option must not be ignored http://bugs.eclipse.org/bugs/show_bug.cgi?id=26251 */
  public void test08() throws CoreException {
    try {
      IJavaProject projectA =
          this.createJavaProject(
              "A",
              new String[] {}, // source folders
              new String[] {}, // lib folders
              new String[] {}, // projects
              "");

      Hashtable options = new Hashtable();
      options.put(JavaCore.COMPILER_TASK_TAGS, "TODO:");
      JavaCore.setOptions(options);

      // check project A custom options
      assertEquals(
          "1#projA:unexpected custom value for task tags option",
          null,
          projectA.getOption(JavaCore.COMPILER_TASK_TAGS, false));
      assertEquals(
          "1#projA:unexpected custom value for inherited task tags option",
          "TODO:",
          projectA.getOption(JavaCore.COMPILER_TASK_TAGS, true));
      assertEquals(
          "1#workspace:unexpected custom value for task tags option",
          "TODO:",
          JavaCore.getOption(JavaCore.COMPILER_TASK_TAGS));

      // change custom options to have one less
      options.clear();
      options.put(JavaCore.COMPILER_TASK_TAGS, "");
      projectA.setOptions(options);
      assertEquals(
          "2#projA:unexpected custom value for task tags option",
          "",
          projectA.getOption(JavaCore.COMPILER_TASK_TAGS, false));
      assertEquals(
          "2#projA:unexpected custom value for inherited task tags option",
          "",
          projectA.getOption(JavaCore.COMPILER_TASK_TAGS, true));
      assertEquals(
          "2#workspace:unexpected custom value for task tags option",
          "TODO:",
          JavaCore.getOption(JavaCore.COMPILER_TASK_TAGS));

      // change custom options to have one less
      options.clear();
      options.put(JavaCore.COMPILER_TASK_TAGS, "@TODO");
      JavaCore.setOptions(options);
      assertEquals(
          "3#projA:unexpected custom value for task tags option",
          "",
          projectA.getOption(JavaCore.COMPILER_TASK_TAGS, false));
      assertEquals(
          "3#projA:unexpected custom value for inherited task tags option",
          "",
          projectA.getOption(JavaCore.COMPILER_TASK_TAGS, true));
      assertEquals(
          "3#workspace:unexpected custom value for task tags option",
          "@TODO",
          JavaCore.getOption(JavaCore.COMPILER_TASK_TAGS));

    } finally {
      this.deleteProject("A");
    }
  }