Search in sources :

Example 21 with IProblem

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

the class ShowTranslationHandler method execute.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands
	 * .ExecutionEvent)
	 */
public Object execute(final ExecutionEvent event) throws ExecutionException {
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        List list = ((IStructuredSelection) selection).toList();
        if (!list.isEmpty()) {
            if (list.get(0) instanceof IDOMNode) {
                final IDOMModel model = ((IDOMNode) list.get(0)).getModel();
                INodeAdapter adapter = model.getDocument().getAdapterFor(IJSPTranslation.class);
                if (adapter != null) {
                    Job opener = new UIJob("Opening JSP Java Translation") {

                        public IStatus runInUIThread(IProgressMonitor monitor) {
                            JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
                            final JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
                            // create an IEditorInput for the Java editor
                            final IStorageEditorInput input = new JSPTranslationEditorInput(model);
                            try {
                                IEditorPart editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), input, JavaUI.ID_CU_EDITOR, true);
                                // Now add the problems we found
                                if (editor instanceof ITextEditor) {
                                    IAnnotationModel annotationModel = ((ITextEditor) editor).getDocumentProvider().getAnnotationModel(input);
                                    translation.reconcileCompilationUnit();
                                    List problemsList = translation.getProblems();
                                    IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
                                    AnnotationTypeLookup lookup = new AnnotationTypeLookup();
                                    for (int i = 0; i < problems.length; i++) {
                                        if (problems[i] instanceof IJSPProblem)
                                            continue;
                                        int length = problems[i].getSourceEnd() - problems[i].getSourceStart() + 1;
                                        Position position = new Position(problems[i].getSourceStart(), length);
                                        Annotation annotation = null;
                                        String type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_INFO);
                                        if (problems[i].isError()) {
                                            type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_ERROR);
                                        } else if (problems[i].isWarning()) {
                                            type = lookup.getAnnotationType(IMarker.PROBLEM, IMarker.SEVERITY_WARNING);
                                        }
                                        annotation = new Annotation(type, false, problems[i].getMessage());
                                        if (annotation != null) {
                                            annotationModel.addAnnotation(annotation, position);
                                        }
                                    }
                                }
                            } catch (PartInitException e) {
                                e.printStackTrace();
                                Display.getCurrent().beep();
                            }
                            return Status.OK_STATUS;
                        }
                    };
                    opener.setSystem(false);
                    opener.setUser(true);
                    opener.schedule();
                }
            }
        }
    }
    return null;
}
Also used : INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IJSPProblem(org.eclipse.jst.jsp.core.internal.java.IJSPProblem) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) ISelection(org.eclipse.jface.viewers.ISelection) UIJob(org.eclipse.ui.progress.UIJob) List(java.util.List) PartInitException(org.eclipse.ui.PartInitException) Job(org.eclipse.core.runtime.jobs.Job) UIJob(org.eclipse.ui.progress.UIJob) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) Position(org.eclipse.jface.text.Position) IEditorPart(org.eclipse.ui.IEditorPart) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IProblem(org.eclipse.jdt.core.compiler.IProblem) Annotation(org.eclipse.jface.text.source.Annotation) AnnotationTypeLookup(org.eclipse.ui.texteditor.AnnotationTypeLookup) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)

Example 22 with IProblem

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

the class NewTypeWizardPage method removeUnusedImports.

private void removeUnusedImports(ICompilationUnit cu, Set<String> existingImports, boolean needsSave) throws CoreException {
    ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    parser.setSource(cu);
    parser.setResolveBindings(true);
    CompilationUnit root = (CompilationUnit) parser.createAST(null);
    if (root.getProblems().length == 0) {
        return;
    }
    @SuppressWarnings("unchecked") List<ImportDeclaration> importsDecls = root.imports();
    if (importsDecls.isEmpty()) {
        return;
    }
    ImportsManager imports = new ImportsManager(root);
    int importsEnd = ASTNodes.getExclusiveEnd(importsDecls.get(importsDecls.size() - 1));
    IProblem[] problems = root.getProblems();
    for (int i = 0; i < problems.length; i++) {
        IProblem curr = problems[i];
        if (curr.getSourceEnd() < importsEnd) {
            int id = curr.getID();
            if (id == IProblem.UnusedImport || id == IProblem.NotVisibleType) {
                // not visible problems hide unused
                // -> remove both
                int pos = curr.getSourceStart();
                for (int k = 0; k < importsDecls.size(); k++) {
                    ImportDeclaration decl = importsDecls.get(k);
                    if (decl.getStartPosition() <= pos && pos < decl.getStartPosition() + decl.getLength()) {
                        if (existingImports.isEmpty() || !existingImports.contains(ASTNodes.asString(decl))) {
                            String name = decl.getName().getFullyQualifiedName();
                            if (decl.isOnDemand()) {
                                // $NON-NLS-1$
                                name += ".*";
                            }
                            if (decl.isStatic()) {
                                imports.removeStaticImport(name);
                            } else {
                                imports.removeImport(name);
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
    imports.create(needsSave, null);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 23 with IProblem

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

the class ASTNodes method getProblems.

public static IProblem[] getProblems(ASTNode node, int scope, int severity) {
    ASTNode root = node.getRoot();
    if (!(root instanceof CompilationUnit))
        return EMPTY_PROBLEMS;
    IProblem[] problems = ((CompilationUnit) root).getProblems();
    if (root == node)
        return problems;
    final int iterations = computeIterations(scope);
    List<IProblem> result = new ArrayList<IProblem>(5);
    for (int i = 0; i < problems.length; i++) {
        IProblem problem = problems[i];
        boolean consider = false;
        if ((severity & PROBLEMS) == PROBLEMS)
            consider = true;
        else if ((severity & WARNING) != 0)
            consider = problem.isWarning();
        else if ((severity & ERROR) != 0)
            consider = problem.isError();
        if (consider) {
            ASTNode temp = node;
            int count = iterations;
            do {
                int nodeOffset = temp.getStartPosition();
                int problemOffset = problem.getSourceStart();
                if (nodeOffset <= problemOffset && problemOffset < nodeOffset + temp.getLength()) {
                    result.add(problem);
                    count = 0;
                } else {
                    count--;
                }
            } while ((temp = temp.getParent()) != null && count > 0);
        }
    }
    return result.toArray(new IProblem[result.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 24 with IProblem

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

the class LiveEditProblemRequestor method toJSON.

private String toJSON(IProblem[] problems) {
    StringBuilder result = new StringBuilder();
    boolean flag = false;
    result.append("[");
    for (IProblem problem : problems) {
        if (flag) {
            result.append(",");
        }
        result.append("{");
        result.append("\"id\":" + problem.getID());
        result.append(",\"author\":" + JDTComponent.JDT_SERVICE_ID);
        result.append(",\"description\":" + JSONObject.quote(problem.getMessage()));
        result.append(",\"line\":" + problem.getSourceLineNumber());
        result.append(",\"severity\":\"" + (problem.isError() ? "error" : "warning") + "\"");
        result.append(",\"start\":" + problem.getSourceStart());
        int end = problem.getSourceEnd() + 1;
        result.append(",\"end\":" + end);
        result.append("}");
        flag = true;
    }
    result.append("]");
    return result.toString();
}
Also used : IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 25 with IProblem

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

the class QuickAssistService method getProblemLocations.

public IProblemLocation getProblemLocations(ICompilationUnit liveEditUnit, int problemID, int offset, int length) {
    final ASTParser parser = ASTParser.newParser(AST.JLS4);
    // Parse the class as a compilation unit.
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(liveEditUnit);
    parser.setResolveBindings(true);
    // Return the compiled class as a compilation unit
    final CompilationUnit unit = (CompilationUnit) parser.createAST(null);
    IProblem[] problems = unit.getProblems();
    if (problemID > 0) {
        return filterOutProblems(problems, problemID);
    } else {
        IProblemLocation[] locations = convertProblems(unit.getProblems());
        for (IProblemLocation iProblemLocation : locations) {
            if (offset >= iProblemLocation.getOffset() && offset <= (iProblemLocation.getOffset() + iProblemLocation.getLength())) {
                return iProblemLocation;
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IProblem(org.eclipse.jdt.core.compiler.IProblem)

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