Search in sources :

Example 1 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem in project che by eclipse.

the class OrganizeImportsOperation method collectReferences.

// find type references in a compilation unit
private boolean collectReferences(CompilationUnit astRoot, List<SimpleName> typeReferences, List<SimpleName> staticReferences, Set<String> oldSingleImports, Set<String> oldDemandImports) {
    if (!fAllowSyntaxErrors) {
        IProblem[] problems = astRoot.getProblems();
        for (int i = 0; i < problems.length; i++) {
            IProblem curr = problems[i];
            if (curr.isError() && (curr.getID() & IProblem.Syntax) != 0) {
                fParsingError = problems[i];
                return false;
            }
        }
    }
    List<ImportDeclaration> imports = astRoot.imports();
    for (int i = 0; i < imports.size(); i++) {
        ImportDeclaration curr = imports.get(i);
        String id = ASTResolving.getFullName(curr.getName());
        if (curr.isOnDemand()) {
            oldDemandImports.add(id);
        } else {
            oldSingleImports.add(id);
        }
    }
    IJavaProject project = fCompilationUnit.getJavaProject();
    ImportReferencesCollector.collect(astRoot, project, null, typeReferences, staticReferences);
    return true;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 2 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem in project che by eclipse.

the class LinkedNodeFinder method findByProblems.

public static SimpleName[] findByProblems(ASTNode parent, SimpleName nameNode) {
    ArrayList<SimpleName> res = new ArrayList<SimpleName>();
    ASTNode astRoot = parent.getRoot();
    if (!(astRoot instanceof CompilationUnit)) {
        return null;
    }
    IProblem[] problems = ((CompilationUnit) astRoot).getProblems();
    int nameNodeKind = getNameNodeProblemKind(problems, nameNode);
    if (nameNodeKind == 0) {
        // no problem on node
        return null;
    }
    int bodyStart = parent.getStartPosition();
    int bodyEnd = bodyStart + parent.getLength();
    String name = nameNode.getIdentifier();
    for (int i = 0; i < problems.length; i++) {
        IProblem curr = problems[i];
        int probStart = curr.getSourceStart();
        int probEnd = curr.getSourceEnd() + 1;
        if (probStart > bodyStart && probEnd < bodyEnd) {
            int currKind = getProblemKind(curr);
            if ((nameNodeKind & currKind) != 0) {
                ASTNode node = NodeFinder.perform(parent, probStart, (probEnd - probStart));
                if (node instanceof SimpleName && name.equals(((SimpleName) node).getIdentifier())) {
                    res.add((SimpleName) node);
                }
            }
        }
    }
    return res.toArray(new SimpleName[res.size()]);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 3 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem in project che by eclipse.

the class RefactoringAnalyzeUtil method getIntroducedCompileProblems.

public static IProblem[] getIntroducedCompileProblems(CompilationUnit newCUNode, CompilationUnit oldCuNode) {
    Set<IProblem> subResult = new HashSet<IProblem>();
    Set<IProblem> oldProblems = getOldProblems(oldCuNode);
    IProblem[] newProblems = ASTNodes.getProblems(newCUNode, ASTNodes.INCLUDE_ALL_PARENTS, ASTNodes.PROBLEMS);
    for (int i = 0; i < newProblems.length; i++) {
        IProblem correspondingOld = findCorrespondingProblem(oldProblems, newProblems[i]);
        if (correspondingOld == null)
            subResult.add(newProblems[i]);
    }
    return subResult.toArray(new IProblem[subResult.size()]);
}
Also used : IProblem(org.eclipse.jdt.core.compiler.IProblem) HashSet(java.util.HashSet)

Example 4 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem 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 5 with IProblem

use of org.eclipse.jdt.core.compiler.IProblem in project che by eclipse.

the class SourceAnalyzer method checkActivation.

public RefactoringStatus checkActivation() throws JavaModelException {
    RefactoringStatus result = new RefactoringStatus();
    if (!fTypeRoot.isStructureKnown()) {
        result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_syntax_errors, JavaStatusContext.create(fTypeRoot));
        return result;
    }
    IProblem[] problems = ASTNodes.getProblems(fDeclaration, ASTNodes.NODE_ONLY, ASTNodes.ERROR);
    if (problems.length > 0) {
        result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_declaration_has_errors, JavaStatusContext.create(fTypeRoot, fDeclaration));
        return result;
    }
    final IMethodBinding declarationBinding = fDeclaration.resolveBinding();
    if (declarationBinding != null) {
        final int modifiers = declarationBinding.getModifiers();
        if (Modifier.isAbstract(modifiers)) {
            result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_abstract_methods, JavaStatusContext.create(fTypeRoot, fDeclaration));
            return result;
        } else if (Modifier.isNative(modifiers)) {
            result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_native_methods, JavaStatusContext.create(fTypeRoot, fDeclaration));
            return result;
        }
    } else {
        result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_methoddeclaration_has_errors, JavaStatusContext.create(fTypeRoot));
        return result;
    }
    ActivationAnalyzer analyzer = new ActivationAnalyzer();
    fDeclaration.accept(analyzer);
    result.merge(analyzer.status);
    if (!result.hasFatalError()) {
        List<SingleVariableDeclaration> parameters = fDeclaration.parameters();
        fParameters = new HashMap<IVariableBinding, ParameterData>(parameters.size() * 2);
        for (Iterator<SingleVariableDeclaration> iter = parameters.iterator(); iter.hasNext(); ) {
            SingleVariableDeclaration element = iter.next();
            IVariableBinding binding = element.resolveBinding();
            if (binding == null) {
                result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_declaration_has_errors, JavaStatusContext.create(fTypeRoot, fDeclaration));
                return result;
            }
            fParameters.put(binding, (ParameterData) element.getProperty(ParameterData.PROPERTY));
        }
        fNames = new HashMap<IBinding, NameData>();
        fImplicitReceivers = new ArrayList<Expression>(2);
        fTypeParameterReferences = new ArrayList<NameData>(0);
        fTypeParameterMapping = new HashMap<ITypeBinding, NameData>();
        ITypeBinding declaringType = declarationBinding.getDeclaringClass();
        if (declaringType == null) {
            result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_typedeclaration_has_errors, JavaStatusContext.create(fTypeRoot));
            return result;
        }
        ITypeBinding[] typeParameters = declaringType.getTypeParameters();
        for (int i = 0; i < typeParameters.length; i++) {
            NameData data = new NameData(typeParameters[i].getName());
            fTypeParameterReferences.add(data);
            fTypeParameterMapping.put(typeParameters[i], data);
        }
        fMethodTypeParameterReferences = new ArrayList<NameData>(0);
        fMethodTypeParameterMapping = new HashMap<ITypeBinding, NameData>();
        IMethodBinding method = declarationBinding;
        typeParameters = method.getTypeParameters();
        for (int i = 0; i < typeParameters.length; i++) {
            NameData data = new NameData(typeParameters[i].getName());
            fMethodTypeParameterReferences.add(data);
            fMethodTypeParameterMapping.put(typeParameters[i], data);
        }
    }
    if (fDeclaration.isVarargs()) {
        List<SingleVariableDeclaration> parameters = fDeclaration.parameters();
        VarargAnalyzer vAnalyzer = new VarargAnalyzer(parameters.get(parameters.size() - 1).getName().resolveBinding());
        fDeclaration.getBody().accept(vAnalyzer);
    }
    return result;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) IProblem(org.eclipse.jdt.core.compiler.IProblem) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Aggregations

IProblem (org.eclipse.jdt.core.compiler.IProblem)62 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)28 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)25 ArrayList (java.util.ArrayList)21 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)10 File (java.io.File)9 ASTParser (org.eclipse.jdt.core.dom.ASTParser)9 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)7 IProblemLocation (org.eclipse.jdt.ui.text.java.IProblemLocation)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 HashMap (java.util.HashMap)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 RefactoringStatusEntry (org.eclipse.ltk.core.refactoring.RefactoringStatusEntry)5 URI (java.net.URI)4 HashSet (java.util.HashSet)4 StringTokenizer (java.util.StringTokenizer)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4