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;
}
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);
}
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()]);
}
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();
}
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;
}
Aggregations