Пример #1
0
  /**
   * Create rename refactoring session.
   *
   * @param settings rename settings
   * @return the rename refactoring session
   * @throws CoreException when RenameSupport can't be created
   * @throws RefactoringException when Java element was not found
   */
  @POST
  @Path("rename/create")
  @Produces("application/json")
  @Consumes("application/json")
  public RenameRefactoringSession createRenameRefactoring(CreateRenameRefactoring settings)
      throws CoreException, RefactoringException {
    IJavaProject javaProject = model.getJavaProject(settings.getProjectPath());
    IJavaElement elementToRename;
    ICompilationUnit cu = null;
    switch (settings.getType()) {
      case COMPILATION_UNIT:
        elementToRename = javaProject.findType(settings.getPath()).getCompilationUnit();
        break;
      case PACKAGE:
        elementToRename =
            javaProject.findPackageFragment(new org.eclipse.core.runtime.Path(settings.getPath()));
        break;
      case JAVA_ELEMENT:
        cu = javaProject.findType(settings.getPath()).getCompilationUnit();
        elementToRename = getSelectionElement(cu, settings.getOffset());
        break;
      default:
        elementToRename = null;
    }
    if (elementToRename == null) {
      throw new RefactoringException("Can't find java element to rename.");
    }

    return manager.createRenameRefactoring(
        elementToRename, cu, settings.getOffset(), settings.isRefactorLightweight());
  }
  @Test
  public void test0d() {

    try {
      IWorkspace workspace = ResourcesPlugin.getWorkspace();
      IWorkspaceRoot root = workspace.getRoot();
      IProject project = root.getProject("TestProject");
      IJavaProject javaProject = JavaCore.create(project);

      IPackageFragment mypackage =
          javaProject.findPackageFragment(new Path("/TestProject/src/be/ac/ua/test/pack"));
      if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
        ICompilationUnit cu = mypackage.getCompilationUnits()[0];
        IType[] types = cu.getAllTypes();
        ClassRecorder recorder = new ClassRecorder(types[0]);

        Field uniqueNameField = ClassRecorder.class.getDeclaredField("uniqueName");
        uniqueNameField.setAccessible(true);
        String fieldValue = (String) uniqueNameField.get(recorder);
        assertEquals(fieldValue, packname + "." + classname);

        /*Field parentField = ClassRecorder.class.getDeclaredField("parent");
        parentField.setAccessible(true);
        FamixEntity fieldValue2 = (FamixEntity)parentField.get(recorder);
        assertEquals(packname, fieldValue2.getUniqueName());*/
      }
    } catch (JavaModelException e) {
      e.printStackTrace();
      fail("Unexpected exception");
    } catch (SecurityException e) {
      e.printStackTrace();
      fail("Unexpected exception");
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
      fail("Unexpected exception");
    } catch (NoSuchFieldException e) {
      e.printStackTrace();
      fail("Unexpected exception");
    } catch (IllegalAccessException e) {
      e.printStackTrace();
      fail("Unexpected exception");
    }
  }
  public IPackageFragment getBasePackageFragment() {

    IPackageFragment packageFragment = null;
    if (!projectText.equals("")) { // $NON-NLS-1$

      try {
        IJavaProject project = JavaCore.create(Util.getProject(projectText));
        IPath moduleXmlPath = new Path(moduleText);
        IPath packageFragmentPath =
            new Path("/")
                .append(projectText)
                .append(moduleXmlPath.removeLastSegments(1)); // $NON-NLS-1$
        packageFragment = project.findPackageFragment(packageFragmentPath);
      } catch (JavaModelException e) {
        Activator.logException(e);
      }
    }

    return packageFragment;
  }
Пример #4
0
  /**
   * Create move refactoring session.
   *
   * @param cmr move settings, contains resource paths to move.
   * @return refactoring session id.
   * @throws JavaModelException when JavaModel has a failure
   * @throws RefactoringException when impossible to create move refactoring session
   */
  @POST
  @Path("move/create")
  @Consumes("application/json")
  @Produces("text/plain")
  public String createMoveRefactoring(CreateMoveRefactoring cmr)
      throws JavaModelException, RefactoringException {
    IJavaProject javaProject = model.getJavaProject(cmr.getProjectPath());
    IJavaElement[] javaElements;
    try {
      Function<ElementToMove, IJavaElement> map =
          javaElement -> {
            try {
              if (javaElement.isPack()) {
                return javaProject.findPackageFragment(
                    new org.eclipse.core.runtime.Path(javaElement.getPath()));
              } else {
                return javaProject.findType(javaElement.getPath()).getCompilationUnit();
              }
            } catch (JavaModelException e) {
              throw new IllegalArgumentException(e);
            }
          };
      javaElements = cmr.getElements().stream().map(map).toArray(IJavaElement[]::new);

    } catch (IllegalArgumentException e) {
      if (e.getCause() instanceof JavaModelException) {
        throw (JavaModelException) e.getCause();
      } else {
        throw e;
      }
    }
    if (RefactoringAvailabilityTester.isMoveAvailable(new IResource[0], javaElements)) {
      return manager.createMoveRefactoringSession(javaElements);
    }

    throw new RefactoringException("Can't create move refactoring.");
  }