/**
   * Updates the content of <code>cu</code>, modifying the type name and/or package declaration as
   * necessary.
   *
   * @return an AST rewrite or null if no rewrite needed
   */
  private TextEdit updateContent(ICompilationUnit cu, PackageFragment dest, String newName)
      throws JavaModelException {
    String[] currPackageName = ((PackageFragment) cu.getParent()).names;
    String[] destPackageName = dest.names;
    if (Util.equalArraysOrNull(currPackageName, destPackageName) && newName == null) {
      return null; // nothing to change
    } else {
      // ensure cu is consistent (noop if already consistent)
      cu.makeConsistent(this.progressMonitor);

      // GROOVY start
      // don't use the ASTParser if not a Java compilation unit
      if (LanguageSupportFactory.isInterestingSourceFile(cu.getElementName())) {
        // ZALUUM
        // old return updateNonJavaContent(cu, destPackageName, currPackageName, newName);
        return LanguageSupportFactory.updateContent(cu, destPackageName, currPackageName, newName);
        // END ZALUUM
      }
      // GROOVY end

      this.parser.setSource(cu);
      CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
      AST ast = astCU.getAST();
      ASTRewrite rewrite = ASTRewrite.create(ast);
      updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite);
      updatePackageStatement(astCU, destPackageName, rewrite, cu);
      return rewrite.rewriteAST();
    }
  }
Пример #2
0
 @Override
 public String toString() {
   return "("
       + ModifiableMClass.class.getSimpleName()
       + ")"
       + jdtCu.getParent().getElementName()
       + "."
       + jdtCu.getElementName();
 }
 private boolean isVisible(TypeNameMatch curr, ICompilationUnit cu) {
   int flags = curr.getModifiers();
   if (Flags.isPrivate(flags)) {
     return false;
   }
   if (Flags.isPublic(flags) || Flags.isProtected(flags)) {
     return true;
   }
   return curr.getPackageName().equals(cu.getParent().getElementName());
 }
Пример #4
0
 /**
  * @since 2.3
  * @deprecated This method is not used anymore.
  */
 @Deprecated
 protected String getExpectedPrimaryTypeNameFor(ICompilationUnit cu) {
   String fileName = cu.getElementName();
   String typeName = fileName.substring(0, fileName.lastIndexOf('.'));
   IPackageFragment pkg = (IPackageFragment) cu.getParent();
   if (!pkg.isDefaultPackage()) {
     typeName = pkg.getElementName() + '.' + typeName;
   }
   return typeName;
 }
Пример #5
0
 @Override
 public void setName(String name) {
   try {
     IPackageFragment jdtPkg = (IPackageFragment) jdtCu.getParent();
     jdtCu.rename(name + ".java", false, MyMonitor.currentMonitor());
     jdtCu = jdtPkg.getCompilationUnit(name + ".java");
   } catch (JavaModelException e) {
     throw new MyRuntimeException("Rename " + this + " to " + name, e);
   }
 }
  public void testExtractInterfaceFromInterface2() throws Exception {
    String className = "A";
    String extendingInterfaceName = "I1";
    String newInterfaceName = "B";

    IType clas =
        getType(createCUfromTestFile(getPackageP(), getTopLevelTypeName(className)), className);
    ICompilationUnit cu = clas.getCompilationUnit();
    IPackageFragment pack = (IPackageFragment) cu.getParent();

    getType(
        createCUfromTestFile(getPackageP(), getTopLevelTypeName(extendingInterfaceName)),
        extendingInterfaceName);

    IPackageFragmentRoot root = RefactoringTestSetup.getDefaultSourceFolder();
    assertNotNull(root);
    IPackageFragment p2 = root.createPackageFragment("p2", true, null);
    getType(createCUfromTestFile(p2, getTopLevelTypeName("I2")), "I2");

    ExtractInterfaceProcessor processor =
        new ExtractInterfaceProcessor(
            clas, JavaPreferencesSettings.getCodeGenerationSettings(clas.getJavaProject()));
    Refactoring ref = new ProcessorBasedRefactoring(processor);

    processor.setTypeName(newInterfaceName);
    assertEquals(
        "interface name should be accepted",
        RefactoringStatus.OK,
        processor.checkTypeName(newInterfaceName).getSeverity());

    IMember[] extractableMembers = processor.getExtractableMembers();
    final IMember[] members = new IMember[extractableMembers.length - 1];
    List<IMember> list = new ArrayList<>();
    for (IMember iMember : extractableMembers) {
      if (!(iMember instanceof IField)) {
        list.add(iMember);
      }
    }
    processor.setExtractedMembers(list.toArray(members));
    processor.setReplace(true);
    processor.setAnnotations(false);
    RefactoringStatus performRefactoring = performRefactoring(ref);
    assertEquals("was supposed to pass", null, performRefactoring);
    assertEqualLines(
        "incorrect changes in " + className,
        getFileContents(getOutputTestFileName(className)),
        cu.getSource());

    ICompilationUnit interfaceCu = pack.getCompilationUnit(newInterfaceName + ".java");
    assertEqualLines(
        "incorrect interface created",
        getFileContents(getOutputTestFileName(newInterfaceName)),
        interfaceCu.getSource());
  }
Пример #7
0
 @Override
 public void setMPackage(ModifiableMPackage destMPackage) {
   try {
     String destPackageName = destMPackage.getName();
     IPackageFragmentRoot srcRoot = (IPackageFragmentRoot) jdtCu.getParent().getParent();
     IPackageFragment jdtDestPackage = srcRoot.getPackageFragment(destPackageName);
     jdtCu.move(jdtDestPackage, null, null, false, MyMonitor.currentMonitor());
     jdtCu = jdtDestPackage.getCompilationUnit(jdtCu.getElementName());
   } catch (JavaModelException e) {
     throw new MyRuntimeException("Move " + this + " to " + destMPackage, e);
   }
 }
 /**
  * Updates the content of <code>cu</code>, modifying the type name and/or package declaration as
  * necessary.
  *
  * @return an AST rewrite or null if no rewrite needed
  */
 private TextEdit updateContent(ICompilationUnit cu, PackageFragment dest, String newName)
     throws JavaModelException {
   String[] currPackageName = ((PackageFragment) cu.getParent()).names;
   String[] destPackageName = dest.names;
   if (Util.equalArraysOrNull(currPackageName, destPackageName) && newName == null) {
     return null; // nothing to change
   } else {
     // ensure cu is consistent (noop if already consistent)
     cu.makeConsistent(this.progressMonitor);
     this.parser.setSource(cu);
     CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
     AST ast = astCU.getAST();
     ASTRewrite rewrite = ASTRewrite.create(ast);
     updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite);
     updatePackageStatement(astCU, destPackageName, rewrite, cu);
     return rewrite.rewriteAST();
   }
 }
  /*
   * Ensures that searching takes the primary owner's working copies and the given working copies into account.
   * (regression test for bug 43300 SearchEngine(IWorkingCopy[] workingCopies) not backward compatible)
   */
  public void testSearch4() throws CoreException {
    ICompilationUnit primaryWorkingCopy = null;
    try {
      createFolder("P/p");
      createFile("/P/p/Y.java", "");
      primaryWorkingCopy = getCompilationUnit("P/p/Y.java");
      primaryWorkingCopy.becomeWorkingCopy(null);

      // create type Y in working copy
      primaryWorkingCopy.getBuffer().setContents("package p;\n" + "public class Y {\n" + "}");
      primaryWorkingCopy.makeConsistent(null);

      // create new working copy on X.java and add type X
      this.workingCopy = getCompilationUnit("P/p/X.java").getWorkingCopy(null);
      this.workingCopy.getBuffer().setContents("package p;\n" + "public class X {\n" + "}");
      this.workingCopy.makeConsistent(null);

      JavaSearchTests.JavaSearchResultCollector resultCollector =
          new JavaSearchTests.JavaSearchResultCollector();
      IJavaSearchScope scope =
          SearchEngine.createJavaSearchScope(new IJavaElement[] {primaryWorkingCopy.getParent()});
      SearchPattern pattern =
          SearchPattern.createPattern(
              "*",
              IJavaSearchConstants.TYPE,
              IJavaSearchConstants.DECLARATIONS,
              SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);
      new SearchEngine(new ICompilationUnit[] {this.workingCopy})
          .search(
              pattern,
              new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
              scope,
              resultCollector,
              null);
      assertEquals("p/X.java p.X [X]\n" + "p/Y.java p.Y [Y]", resultCollector.toString());

    } finally {
      if (primaryWorkingCopy != null) {
        primaryWorkingCopy.discardWorkingCopy();
      }
      deleteFile("/P/p/Y.java");
    }
  }
  private void helper1(
      int startLine,
      int startColumn,
      int endLine,
      int endColumn,
      boolean makeFinal,
      boolean makeStatic,
      String className,
      int visibility)
      throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), true, true);
    ISourceRange selection =
        TextRangeUtil.getSelection(cu, startLine, startColumn, endLine, endColumn);
    ConvertAnonymousToNestedRefactoring ref =
        new ConvertAnonymousToNestedRefactoring(cu, selection.getOffset(), selection.getLength());

    RefactoringStatus preconditionResult = ref.checkInitialConditions(new NullProgressMonitor());
    if (preconditionResult.isOK()) preconditionResult = null;
    assertEquals("activation was supposed to be successful", null, preconditionResult);

    ref.setClassName(className);
    ref.setDeclareFinal(makeFinal);
    ref.setDeclareStatic(makeStatic);
    ref.setVisibility(visibility);

    if (preconditionResult == null)
      preconditionResult = ref.checkFinalConditions(new NullProgressMonitor());
    else preconditionResult.merge(ref.checkFinalConditions(new NullProgressMonitor()));
    if (preconditionResult.isOK()) preconditionResult = null;
    assertEquals("precondition was supposed to pass", null, preconditionResult);

    performChange(ref, false);

    IPackageFragment pack = (IPackageFragment) cu.getParent();
    String newCuName = getSimpleTestFileName(true, true);
    ICompilationUnit newcu = pack.getCompilationUnit(newCuName);
    assertTrue(newCuName + " does not exist", newcu.exists());
    assertEqualLines(getFileContents(getTestFileName(true, false)), newcu.getSource());
  }
Пример #11
0
 public AssistCompilationUnit(
     ICompilationUnit compilationUnit, WorkingCopyOwner owner, Map bindingCache, Map infoCache) {
   super((PackageFragment) compilationUnit.getParent(), compilationUnit.getElementName(), owner);
   this.bindingCache = bindingCache;
   this.infoCache = infoCache;
 }
Пример #12
0
 @Override
 public ModifiableMPackage getMPackage() {
   return new MPackageJDT(
       ((IPackageFragment) jdtCu.getParent())
           .getCompilationUnit(MPackage.PACKAGE_INFO_CLASS + ".java"));
 }