예제 #1
0
 private void addSystemLibraries() throws JavaModelException {
   IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
   IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
   System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
   newEntries[oldEntries.length] = JavaRuntime.getDefaultJREContainerEntry();
   javaProject.setRawClasspath(newEntries, null);
 }
  private IClasspathEntry[] createClasspathEntries(IJavaProject javaProject) {

    IPath srcPath1 = javaProject.getPath().append("src/main/java");
    IPath srcPath2 = javaProject.getPath().append("src/main/resources");
    IPath srcPath3 = javaProject.getPath().append("src/test/java");
    IPath srcPath4 = javaProject.getPath().append("src/test/resources");

    IPath[] javaPath = new IPath[] {new Path("**/*.java")};
    IPath testOutputLocation = javaProject.getPath().append("target/test-classes");

    IClasspathEntry[] entries = {
      JavaCore.newSourceEntry(srcPath1, javaPath, null, null),
      JavaCore.newSourceEntry(srcPath2, javaPath),
      JavaCore.newSourceEntry(srcPath3, javaPath, null, testOutputLocation),
      JavaCore.newSourceEntry(srcPath4, javaPath, testOutputLocation),
      JavaRuntime.getDefaultJREContainerEntry()
    };

    return entries;
  }
예제 #3
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);
    }
  }
예제 #4
0
 /**
  * @param javaProject
  * @throws JavaModelException
  */
 private void addJREContainerToProject(IJavaProject javaProject) throws JavaModelException {
   addToClasspath(javaProject, JavaRuntime.getDefaultJREContainerEntry());
 }
예제 #5
0
  /**
   * Adds the java nature to a given project
   *
   * @return the java project (nature) that has been set.
   */
  protected IJavaProject configureAsJavaProject(IProject project, IProgressMonitor monitor)
      throws CoreException {
    IProjectDescription description = project.getDescription();
    String[] natures = description.getNatureIds();
    String[] newNatures = new String[natures.length + 1];
    System.arraycopy(natures, 0, newNatures, 0, natures.length);
    newNatures[natures.length] = JavaCore.NATURE_ID;
    description.setNatureIds(newNatures);
    project.setDescription(description, monitor);

    IFolder srcFolder = project.getFolder(new Path("src"));
    srcFolder.create(false, true, monitor);

    IJavaProject javaProject = JavaCore.create(project);

    javaProject.setRawClasspath(
        new IClasspathEntry[] {JavaCore.newSourceEntry(srcFolder.getFullPath())}, monitor);

    Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>();
    entries.addAll(Arrays.asList(javaProject.getRawClasspath()));
    entries.add(JavaRuntime.getDefaultJREContainerEntry());
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);

    // create src/javamod1/javamod2
    IFolder javaMod1Folder = srcFolder.getFolder("javamod1");
    javaMod1Folder.create(true, true, monitor);

    IFolder javaMod2Folder = javaMod1Folder.getFolder("javamod2");
    javaMod2Folder.create(true, true, monitor);

    // create src/JavaDefault.java
    IFile javaClassFile = srcFolder.getFile("JavaDefault.java");

    String javaClassContents =
        "public class JavaDefault {\n"
            + // default package
            "   private int testJavaDefault(String[] args) {\n"
            + "       return 0;\n"
            + "   }\n"
            + "}\n";

    javaClassFile.create(new ByteArrayInputStream(javaClassContents.getBytes()), true, monitor);

    // create src/javamod1/JavaClass.java
    javaClassFile = javaMod1Folder.getFile("JavaClass.java");

    javaClassContents =
        "package javamod1;\n"
            + "public class JavaClass {\n"
            + "   \n"
            + "   public static int JAVA_CLASS_CONSTANT = 1;\n"
            + "   \n"
            + "   public static void main(String[] args) {\n"
            + "       new JavaClass().testJavaClass(new int[0]);\n"
            + "   }\n"
            + "   private int testJavaClass(int[] args) {\n"
            + "       return 0;\n"
            + "   }\n"
            + "}\n";

    javaClassFile.create(new ByteArrayInputStream(javaClassContents.getBytes()), true, monitor);

    // create src/javamod1/javamod2/JavaClass2.java
    javaClassFile = javaMod2Folder.getFile("JavaClass2.java");

    javaClassContents =
        "package javamod1.javamod2;\n"
            + "public class JavaClass2 {\n"
            + "   \n"
            + "   public static int JAVA_CLASS_CONSTANT_2 = 1;\n"
            + "   \n"
            + "   public JavaClass2(int i){};\n"
            + "   \n"
            + "   public static void main(String[] args) {\n"
            + "       new JavaClass2(1).testJavaClass2(new int[0]);\n"
            + "   }\n"
            + "   private int testJavaClass2(int[] args) {\n"
            + "       return 0;\n"
            + "   }\n"
            + "}\n";

    javaClassFile.create(new ByteArrayInputStream(javaClassContents.getBytes()), true, monitor);

    project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
    return javaProject;
  }
예제 #6
0
 public static IClasspathEntry getJreVariableEntry() {
   return JavaRuntime.getDefaultJREContainerEntry();
 }