Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to find position in the java file of an element
How to find position in the java file of an element [message #1019691] Sat, 16 March 2013 07:20 Go to next message
Stave Escura is currently offline Stave Escura
Messages: 6
Registered: March 2013
Junior Member
I am an Eclipse newbie, so I appreciate any help you can give.

Big Picture: I want to add annotationmarkers to IJavaElements that match some condition (think static code analysis).

Current state: I have learnt how to add annotations in my own text editor. I calculate the start and end position by reading the resource in as a file, and finding the start and end points of strings in the text representation of the source. The following code adds the annotations exactly where I want them
	IWorkbench workbench = PlatformUI.getWorkbench();
	IEditorPart activeWorkbenchWindow = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
	if (activeWorkbenchWindow instanceof AbstractDecoratedTextEditor) {
		AbstractDecoratedTextEditor editor = (AbstractDecoratedTextEditor) activeWorkbenchWindow;
		IAnnotationModel annotationModel = ((AbstractDecoratedTextEditor) activeWorkbenchWindow).getDocumentProvider().getAnnotationModel(editor.getEditorInput());
		IMarker marker = resource.createMarker(IMarker.PROBLEM); //later on this won't be a PROBLEM
		marker.setAttribute(IMarker.MESSAGE, "Some Text"); //just to see it in the tooltip
		MarkerAnnotation annotation = new MarkerAnnotation(marker);
		Position position = new Position(...startPosition..., ...lengt...);	
		annotationModel.addAnnotation(annotation, position);


Current problem: I am now trying to add the annotations to the Java Editor. The same code doesn't actually work: the correct number of annotation do appear, but they don't coincide with the text representation. As an example if I have text in the Editor with the string "asd" repeated several times, and I add annotations around the "asd"s with startPosition the indexOf asd in the text representation of the javafile, then the first annotation appears to the right of the first appearance of "asd", and all the rest appear superimposed over the first. This is the same code that works in my homebrew editor.

I have two questions that I would like to ask

  • How do I find the position for a piece of text in the document backing the java model. For example if the strings "asd" occurs somewhere in the resource, how do I find its position
  • Given a IJavaElement how do I find its position so that I can annotate it


Once again thanks for all the help
Re: How to find position in the java file of an element [message #1023862 is a reply to message #1019691] Mon, 25 March 2013 06:26 Go to previous message
Stave Escura is currently offline Stave Escura
Messages: 6
Registered: March 2013
Junior Member
OK well I have found the answer to the questions, so recording them here in case other people have the same issue.

Question1: Given a IJavaElement how do I find its position so that I can annotate it?

To find the location, I have to make the java element more specific. I found it helper to have a visitor pattern:

public static <T> T visitJavaElement(IJavaElement element, IJavaElementVisitor<T> visitor) {
		if (element instanceof JavaProject)
			return visitor.from((JavaProject) element);
		if (element instanceof JavaModel)
			return visitor.from((JavaModel) element);
		else if (element instanceof PackageFragmentRoot)
			return visitor.from((PackageFragmentRoot) element);
		else if (element instanceof PackageFragment)
			return visitor.from((PackageFragment) element);
		else if (element instanceof org.eclipse.jdt.internal.core.CompilationUnit)
			return visitor.from((org.eclipse.jdt.internal.core.CompilationUnit) element);
		else if (element instanceof SourceType)
			return visitor.from((SourceType) element);
		else if (element instanceof BinaryType)
			return visitor.from((BinaryType) element);
		else if (element instanceof BinaryMember)
			return visitor.from((BinaryMember) element);
		else if (element instanceof SourceMethod)
			return visitor.from((SourceMethod) element);
		else if (element instanceof ClassFile)
			return visitor.from((ClassFile) element);
...etc
	}


Then to find the start position (for example) I can
visitJavaElement(new IJavaElementVisitor<Integer>(){
   			@Override
			public String from(SourceType sourceType) {
				return sourceType.getNameRange().getOffset();
			}

			@Override
			public String from(SourceMethod method) {
				return method.getNameRange().getOffset();

			}
			...etc
});


Question2: How do I find the position for a piece of text in the document backing the java model. For example if the strings "asd" occurs somewhere in the resource, how do I find its position
In the following code offset is the index of asd, and length is it's length
	public static CompilationUnit findCompilationUnitFrom(ITypeRoot compilationUnit) {
		CompilationUnit root = SharedASTProvider.getAST(compilationUnit, SharedASTProvider.WAIT_NO, null);
		return root;
	}

	public static ASTNode findAstNodeFor(ASTNode root, int offset, int length) {
		ASTNode node = NodeFinder.perform(root, offset, length);
		return node;
	}



Previous Topic:Autocompletition problem
Next Topic:Cannot update plugin (Hibernate) because of "conflicting dependency"
Goto Forum:
  


Current Time: Thu Apr 17 03:36:50 EDT 2014

Powered by FUDForum. Page generated in 0.01708 seconds