Example usage for org.eclipse.jdt.core IJavaProject getPath

List of usage examples for org.eclipse.jdt.core IJavaProject getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getPath.

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.eclipse.che.jdt.javaeditor.JavaReconciler.java

License:Open Source License

public ReconcileResult reconcile(IJavaProject javaProject, String fqn) throws JavaModelException {
    final ProblemRequestor requestor = new ProblemRequestor();
    WorkingCopyOwner wcOwner = new WorkingCopyOwner() {
        public IProblemRequestor getProblemRequestor(ICompilationUnit unit) {
            return requestor;
        }/*from w ww  .j  ava 2  s . c  o m*/

        @Override
        public IBuffer createBuffer(ICompilationUnit workingCopy) {
            //                return BufferManager.createBuffer(workingCopy);
            //                ?????
            return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy,
                    (IFile) workingCopy.getResource());
        }
    };
    List<HighlightedPosition> positions = null;
    ICompilationUnit compilationUnit = null;
    try {
        IType type = javaProject.findType(fqn);
        if (type == null) {
            return null;
        }
        if (type.isBinary()) {
            throw new IllegalArgumentException("Can't reconcile binary type: " + fqn);
        } else {
            compilationUnit = type.getCompilationUnit().getWorkingCopy(wcOwner, null);
        }
        requestor.reset();
        CompilationUnit unit = compilationUnit.reconcile(AST.JLS8, true, wcOwner, null);
        positions = semanticHighlighting.reconcileSemanticHighlight(unit);
        if (compilationUnit instanceof ClassFileWorkingCopy) {
            //we don't wont to show any errors from ".class" files
            requestor.reset();
        }

    } catch (JavaModelException e) {
        LOG.error("Can't reconcile class: " + fqn + " in project:" + javaProject.getPath().toOSString(), e);
        throw e;
    } finally {
        if (compilationUnit != null && compilationUnit.isWorkingCopy()) {
            try {
                //todo close buffer
                compilationUnit.getBuffer().close();
                compilationUnit.discardWorkingCopy();
            } catch (JavaModelException e) {
                //ignore
            }
        }
    }

    ReconcileResult result = DtoFactory.getInstance().createDto(ReconcileResult.class);
    result.setProblems(convertProblems(requestor.problems));
    result.setHighlightedPositions(positions);
    return result;
}

From source file:org.eclipse.che.jdt.search.JavaElementToDtoConverter.java

License:Open Source License

public List<JavaProject> getProjects() throws JavaModelException {
    Set<Object> objects = childrens.get(result);
    List<JavaProject> result = new ArrayList<>();
    if (objects == null) {
        return result;
    }// w  w w .j a v a 2 s . c  o  m
    for (Object object : objects) {
        JavaProject javaProject = DtoFactory.newDto(JavaProject.class);

        IJavaProject project = (IJavaProject) object;
        javaProject.setName(project.getElementName());
        String path = project.getPath().toOSString();
        javaProject.setPath(path);
        javaProject.setPackageFragmentRoots(getPackageFragmentRoots(object, path));
        result.add(javaProject);
    }
    return result;
}

From source file:org.eclipse.che.jdt.search.SearchManager.java

License:Open Source License

public FindUsagesResponse findUsage(IJavaProject javaProject, String fqn, int offset) throws SearchException {
    try {// w w w .ja v  a2  s  .  c om
        ICompilationUnit compilationUnit;
        IType type = javaProject.findType(fqn);
        if (type == null) {
            throw new SearchException("Can't find type: " + fqn);
        }
        if (type.isBinary()) {
            compilationUnit = type.getClassFile().getWorkingCopy(DefaultWorkingCopyOwner.PRIMARY, null);
            if (compilationUnit == null) {
                throw new SearchException("Can't find sources for: " + fqn + " type");
            }
        } else {
            compilationUnit = type.getCompilationUnit();
        }
        IJavaElement[] elements = compilationUnit.codeSelect(offset, 0);
        if (elements != null && elements.length == 1) {
            IJavaElement element = elements[0];
            if (isTypeValid(element, TYPES_FOR_FIND_USAGE)) {
                return performFindUsageSearch(element);
            } else {
                throw new SearchException("Find usage can't search for element: " + element.getElementName());
            }

        } else {
            throw new SearchException(
                    "Can't find element to search, try to move cursor to another place and invoke search again");
        }

    } catch (JavaModelException e) {
        LOG.error(e.getMessage(), e);
        throw new SearchException(String.format("Can't find project: %s or file for FQN: %s",
                javaProject.getPath().toOSString(), fqn), e);
    } catch (BadLocationException e) {
        LOG.error(e.getMessage(), e);
        throw new SearchException("Some error happened when formatting search result", e);
    }
}

From source file:org.eclipse.che.plugin.java.plain.server.projecttype.PlainJavaInitHandler.java

License:Open Source License

@Override
protected void initializeClasspath(IJavaProject javaProject) throws ServerException {
    IClasspathEntry[] projectClasspath;//from   www .j  a  va 2s .  co  m
    try {
        projectClasspath = javaProject.getRawClasspath();
    } catch (JavaModelException e) {
        LOG.warn("Can't get classpath for: " + javaProject.getProject().getFullPath().toOSString(), e);
        throw new ServerException(e);
    }

    //default classpath
    IClasspathEntry[] defaultClasspath = new IClasspathEntry[] {
            JavaCore.newSourceEntry(javaProject.getPath()) };
    if (!Arrays.equals(defaultClasspath, projectClasspath)) {
        //classpath is already initialized
        return;
    }

    RegisteredProject project = projectRegistryProvider.get().getProject(javaProject.getPath().toOSString());
    List<String> sourceFolders = project.getAttributes().get(Constants.SOURCE_FOLDER);
    List<String> library = project.getAttributes().get(LIBRARY_FOLDER);

    classpathBuilder.generateClasspath(javaProject, sourceFolders, library);
}

From source file:org.eclipse.che.plugin.java.server.JavaNavigation.java

License:Open Source License

public List<JavaProject> getAllProjectsAndPackages(boolean includePackages) throws JavaModelException {
    JavaModel javaModel = JavaModelManager.getJavaModelManager().getJavaModel();
    IJavaProject[] javaProjects = javaModel.getJavaProjects();
    List<JavaProject> result = new ArrayList<>();
    for (IJavaProject javaProject : javaProjects) {
        if (javaProject.exists()) {
            JavaProject project = DtoFactory.newDto(JavaProject.class);
            project.setName(javaProject.getElementName());
            project.setPath(javaProject.getPath().toOSString());
            project.setPackageFragmentRoots(toPackageRoots(javaProject, includePackages));
            result.add(project);/*from   w w  w.  j av a  2s .  c  o  m*/
        }
    }
    return result;
}

From source file:org.eclipse.che.plugin.java.server.JavaReconcileRequestHandler.java

License:Open Source License

private ReconcileResult getReconcileOperation(JavaClassInfo javaClassInfo) {
    IJavaProject javaProject = JAVA_MODEL.getJavaProject(javaClassInfo.getProjectPath());
    try {/*w  w  w .  j av  a 2  s . c om*/
        return reconciler.reconcile(javaProject, javaClassInfo.getFQN());
    } catch (JavaModelException e) {
        String error = format("Can't reconcile class: %s in project: %s, the reason is %s",
                javaClassInfo.getFQN(), javaProject.getPath().toOSString(), e.getLocalizedMessage());
        throw new JsonRpcException(500, error);
    }
}

From source file:org.eclipse.che.plugin.java.testing.AbstractJavaTestRunner.java

License:Open Source License

protected String getOutputDirectory(IJavaProject javaProject) {
    String path = workspacePath + javaProject.getPath() + TEST_OUTPUT_FOLDER;
    try {/* www.  jav  a  2s  . co m*/
        IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry iClasspathEntry : resolvedClasspath) {
            if (iClasspathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath outputLocation = iClasspathEntry.getOutputLocation();
                if (outputLocation == null) {
                    continue;
                }
                return workspacePath + outputLocation.removeLastSegments(1).append(TEST_OUTPUT_FOLDER);
            }
        }
    } catch (JavaModelException e) {
        return path;
    }
    return path;
}

From source file:org.eclipse.che.plugin.testing.junit.server.junit4.JUnit4TestRunner.java

License:Open Source License

private ProcessHandler startTestProcess(IJavaProject javaProject, TestExecutionContext context) {
    JavaParameters parameters = new JavaParameters();
    parameters.setJavaExecutable(System.getProperty("java.home") + "/bin/java");
    parameters.setMainClassName(MAIN_CLASS_NAME);
    parameters.setWorkingDirectory(workspacePath + javaProject.getPath());

    List<String> classPath = new ArrayList<>();
    Set<String> projectClassPath = classpathProvider.getProjectClassPath(javaProject);
    classPath.addAll(projectClassPath);//  ww w  .j  a v a 2s .  c  o m
    classPath.add(ClasspathUtil.getJarPathForClass(CheJUnitCoreRunner.class));
    parameters.getClassPath().addAll(classPath);

    List<String> suite = findTests(context, javaProject, JavaTestAnnotations.JUNIT4X_TEST.getName(),
            JavaTestAnnotations.JUNIT4X_RUN_WITH.getName());
    for (String element : suite) {
        parameters.getParametersList().add(element);
    }
    if (context.isDebugModeEnable()) {
        generateDebuggerPort();
        parameters.getVmParameters().add("-Xdebug");
        parameters.getVmParameters()
                .add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=" + getDebugPort());
    }
    CommandLine command = parameters.createCommand();
    try {
        return new ProcessHandler(command.createProcess());
    } catch (ExecutionException e) {
        LOG.error("Can't run JUnit JVM", e);
    }

    return null;
}

From source file:org.eclipse.che.plugin.testing.testng.server.TestNGRunner.java

License:Open Source License

private ProcessHandler startTestProcess(IJavaProject javaProject, TestExecutionContext context) {
    File suiteFile = createSuite(context, javaProject);
    if (suiteFile == null) {
        throw new RuntimeException("Can't create TestNG suite xml file.");
    }//w ww.j av a 2  s.  co m

    JavaParameters parameters = new JavaParameters();
    parameters.setJavaExecutable(System.getProperty("java.home") + "/bin/java");
    parameters.setMainClassName("org.testng.CheTestNGLauncher");
    String outputDirectory = getOutputDirectory(javaProject);
    parameters.getParametersList().add("-d", outputDirectory);
    parameters.setWorkingDirectory(workspacePath + javaProject.getPath());
    List<String> classPath = new ArrayList<>();
    Set<String> projectClassPath = classpathProvider.getProjectClassPath(javaProject);
    classPath.addAll(projectClassPath);
    classPath.add(ClasspathUtil.getJarPathForClass(org.testng.CheTestNG.class));
    classPath.add(ClasspathUtil.getJarPathForClass(JCommander.class));
    parameters.getClassPath().addAll(classPath);

    parameters.getParametersList().add("-suiteFile", suiteFile.getAbsolutePath());
    if (context.isDebugModeEnable()) {
        generateDebuggerPort();
        parameters.getVmParameters().add("-Xdebug");
        parameters.getVmParameters()
                .add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=" + getDebugPort());
    }
    CommandLine command = parameters.createCommand();
    try {
        return new ProcessHandler(command.createProcess());
    } catch (ExecutionException e) {
        LOG.error("Can't run TestNG JVM", e);
    }

    return null;
}

From source file:org.eclipse.che.plugin.testing.testng.server.TestNGRunner.java

License:Open Source License

private File createSuite(TestExecutionContext context, IJavaProject javaProject) {
    String filePath = context.getFilePath();
    if (!isNullOrEmpty(filePath) && filePath.endsWith(".xml")) {
        String path = filePath.substring(javaProject.getPath().toString().length(), filePath.length());
        IFile file = javaProject.getProject().getFile(path);
        return suiteUtil.writeSuite(System.getProperty(JAVA_IO_TMPDIR), file);
    }/*from  ww  w. j  av  a2 s .com*/
    List<String> testSuite = findTests(context, javaProject, JavaTestAnnotations.TESTNG_TEST.getName(), "");

    Map<String, List<String>> classes = buildTestNgSuite(testSuite, context);

    return suiteUtil.writeSuite(System.getProperty(JAVA_IO_TMPDIR), javaProject.getElementName(), classes);
}