Search in sources :

Example 21 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class DeltaProcessingState method getOldJavaProjecNames.

/*
	 * Workaround for bug 15168 circular errors not reported
	 * Returns the list of java projects before resource delta processing
	 * has started.
	 */
public synchronized HashSet getOldJavaProjecNames() {
    if (this.javaProjectNamesCache == null) {
        HashSet result = new HashSet();
        IJavaProject[] projects;
        try {
            projects = manager.getJavaModel().getJavaProjects();
        } catch (JavaModelException e) {
            return this.javaProjectNamesCache;
        }
        for (int i = 0, length = projects.length; i < length; i++) {
            IJavaProject project = projects[i];
            result.add(project.getElementName());
        }
        return this.javaProjectNamesCache = result;
    }
    return this.javaProjectNamesCache;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashSet(java.util.HashSet)

Example 22 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class JavadocContentAccess2 method createAST.

private static CompilationUnit createAST(IJavaElement element, String cuSource) {
    Assert.isNotNull(element);
    CheASTParser parser = CheASTParser.newParser(AST.JLS8);
    IJavaProject javaProject = element.getJavaProject();
    parser.setProject(javaProject);
    Map<String, String> options = javaProject.getOptions(true);
    options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
    JavaCore.ENABLED);
    parser.setCompilerOptions(options);
    parser.setSource(cuSource.toCharArray());
    return (CompilationUnit) parser.createAST(null);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaProject(org.eclipse.jdt.core.IJavaProject) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser)

Example 23 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class InferTypeArgumentsRefactoring method checkFinalConditions.

/*
	 * @see org.eclipse.ltk.core.refactoring.Refactoring#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public RefactoringStatus checkFinalConditions(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    HashMap<IJavaProject, ArrayList<IJavaElement>> projectsToElements = getJavaElementsPerProject(fElements);
    //$NON-NLS-1$
    pm.beginTask("", projectsToElements.size() + 2);
    final RefactoringStatus result = new RefactoringStatus();
    try {
        fTCModel = new InferTypeArgumentsTCModel();
        final InferTypeArgumentsConstraintCreator unitCollector = new InferTypeArgumentsConstraintCreator(fTCModel, fAssumeCloneReturnsSameType);
        for (Iterator<Entry<IJavaProject, ArrayList<IJavaElement>>> iter = projectsToElements.entrySet().iterator(); iter.hasNext(); ) {
            Entry<IJavaProject, ArrayList<IJavaElement>> entry = iter.next();
            IJavaProject project = entry.getKey();
            ArrayList<IJavaElement> javaElementsList = entry.getValue();
            IJavaElement[] javaElements = javaElementsList.toArray(new IJavaElement[javaElementsList.size()]);
            List<ICompilationUnit> cus = Arrays.asList(JavaModelUtil.getAllCompilationUnits(javaElements));
            int batchSize = 150;
            int batches = ((cus.size() - 1) / batchSize) + 1;
            SubProgressMonitor projectMonitor = new SubProgressMonitor(pm, 1);
            //$NON-NLS-1$
            projectMonitor.beginTask("", batches);
            projectMonitor.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_building);
            for (int i = 0; i < batches; i++) {
                List<ICompilationUnit> batch = cus.subList(i * batchSize, Math.min(cus.size(), (i + 1) * batchSize));
                ICompilationUnit[] batchCus = batch.toArray(new ICompilationUnit[batch.size()]);
                final SubProgressMonitor batchMonitor = new SubProgressMonitor(projectMonitor, 1);
                batchMonitor.subTask(RefactoringCoreMessages.InferTypeArgumentsRefactoring_calculating_dependencies);
                ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
                parser.setProject(project);
                parser.setCompilerOptions(RefactoringASTParser.getCompilerOptions(project));
                parser.setResolveBindings(true);
                parser.createASTs(batchCus, new String[0], new ASTRequestor() {

                    @Override
                    public void acceptAST(final ICompilationUnit source, final CompilationUnit ast) {
                        batchMonitor.subTask(BasicElementLabels.getFileName(source));
                        SafeRunner.run(new ISafeRunnable() {

                            public void run() throws Exception {
                                IProblem[] problems = ast.getProblems();
                                for (int p = 0; p < problems.length; p++) {
                                    if (problems[p].isError()) {
                                        String cuName = JavaElementLabels.getElementLabel(source, JavaElementLabels.CU_QUALIFIED);
                                        String msg = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_error_in_cu_skipped, new Object[] { cuName });
                                        result.addError(msg, JavaStatusContext.create(source, SourceRangeFactory.create(problems[p])));
                                        return;
                                    }
                                }
                                ast.accept(unitCollector);
                            }

                            public void handleException(Throwable exception) {
                                String cuName = JavaElementLabels.getElementLabel(source, JavaElementLabels.CU_QUALIFIED);
                                String msg = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_internal_error, new Object[] { cuName });
                                JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, msg, null));
                                String msg2 = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_error_skipped, new Object[] { cuName });
                                result.addError(msg2, JavaStatusContext.create(source));
                            }
                        });
                        fTCModel.newCu();
                    }

                    @Override
                    public void acceptBinding(String bindingKey, IBinding binding) {
                    //do nothing
                    }
                }, batchMonitor);
            }
            projectMonitor.done();
            fTCModel.newCu();
        }
        //			Display.getDefault().syncExec(new Runnable() {
        //				public void run() {
        //					MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Debugging...", "after constraint gen");
        //				}
        //			});
        pm.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_solving);
        InferTypeArgumentsConstraintsSolver solver = new InferTypeArgumentsConstraintsSolver(fTCModel);
        InferTypeArgumentsUpdate updates = solver.solveConstraints(new SubProgressMonitor(pm, 1));
        //free caches
        solver = null;
        fChangeManager = new TextChangeManager();
        rewriteDeclarations(updates, new SubProgressMonitor(pm, 1));
        IFile[] filesToModify = ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits());
        result.merge(Checks.validateModifiesFiles(filesToModify, getValidationContext()));
        return result;
    } finally {
        pm.done();
        clearGlobalState();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Entry(java.util.Map.Entry) ASTRequestor(org.eclipse.jdt.core.dom.ASTRequestor) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IProblem(org.eclipse.jdt.core.compiler.IProblem) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 24 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class InferTypeArgumentsRefactoring method getJavaElementsPerProject.

private HashMap<IJavaProject, ArrayList<IJavaElement>> getJavaElementsPerProject(IJavaElement[] elements) {
    HashMap<IJavaProject, ArrayList<IJavaElement>> result = new HashMap<IJavaProject, ArrayList<IJavaElement>>();
    for (int i = 0; i < elements.length; i++) {
        IJavaElement element = elements[i];
        IJavaProject javaProject = element.getJavaProject();
        ArrayList<IJavaElement> javaElements = result.get(javaProject);
        if (javaElements == null) {
            javaElements = new ArrayList<IJavaElement>();
            result.put(javaProject, javaElements);
        }
        javaElements.add(element);
    }
    return result;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 25 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class InferTypeArgumentsRefactoring method createChange.

/*
	 * @see org.eclipse.ltk.core.refactoring.Refactoring#createChange(org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    //$NON-NLS-1$
    pm.beginTask("", 1);
    try {
        DynamicValidationStateChange result = new DynamicValidationStateChange(RefactoringCoreMessages.InferTypeArgumentsRefactoring_name, fChangeManager.getAllChanges()) {

            @Override
            public final ChangeDescriptor getDescriptor() {
                final Map<String, String> arguments = new HashMap<String, String>();
                final IJavaProject project = getSingleProject();
                final String description = RefactoringCoreMessages.InferTypeArgumentsRefactoring_descriptor_description;
                final String header = project != null ? Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_descriptor_description_project, BasicElementLabels.getJavaElementName(project.getElementName())) : RefactoringCoreMessages.InferTypeArgumentsRefactoring_descriptor_description;
                final String name = project != null ? project.getElementName() : null;
                final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(name, this, header);
                final String[] settings = new String[fElements.length];
                for (int index = 0; index < settings.length; index++) settings[index] = JavaElementLabels.getTextLabel(fElements[index], JavaElementLabels.ALL_FULLY_QUALIFIED);
                comment.addSetting(JDTRefactoringDescriptorComment.createCompositeSetting(RefactoringCoreMessages.InferTypeArgumentsRefactoring_original_elements, settings));
                if (fAssumeCloneReturnsSameType)
                    comment.addSetting(RefactoringCoreMessages.InferTypeArgumentsRefactoring_assume_clone);
                if (fLeaveUnconstrainedRaw)
                    comment.addSetting(RefactoringCoreMessages.InferTypeArgumentsRefactoring_leave_unconstrained);
                final InferTypeArgumentsDescriptor descriptor = RefactoringSignatureDescriptorFactory.createInferTypeArgumentsDescriptor(name, description, comment.asString(), arguments, RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE);
                for (int index = 0; index < fElements.length; index++) arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + (index + 1), JavaRefactoringDescriptorUtil.elementToHandle(name, fElements[index]));
                arguments.put(ATTRIBUTE_CLONE, Boolean.valueOf(fAssumeCloneReturnsSameType).toString());
                arguments.put(ATTRIBUTE_LEAVE, Boolean.valueOf(fLeaveUnconstrainedRaw).toString());
                return new RefactoringChangeDescriptor(descriptor);
            }
        };
        return result;
    } finally {
        pm.done();
    }
}
Also used : InferTypeArgumentsDescriptor(org.eclipse.jdt.core.refactoring.descriptors.InferTypeArgumentsDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor)

Aggregations

IJavaProject (org.eclipse.jdt.core.IJavaProject)733 IProject (org.eclipse.core.resources.IProject)177 IFile (org.eclipse.core.resources.IFile)149 Test (org.junit.Test)142 CoreException (org.eclipse.core.runtime.CoreException)125 JavaModelException (org.eclipse.jdt.core.JavaModelException)124 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)122 IPath (org.eclipse.core.runtime.IPath)110 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)102 ArrayList (java.util.ArrayList)87 IFolder (org.eclipse.core.resources.IFolder)78 IResource (org.eclipse.core.resources.IResource)75 IJavaElement (org.eclipse.jdt.core.IJavaElement)62 IType (org.eclipse.jdt.core.IType)62 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)60 Path (org.eclipse.core.runtime.Path)57 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)55 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)41 StringInputStream (org.eclipse.xtext.util.StringInputStream)39 HashMap (java.util.HashMap)38