Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Resolve generic type in subclass?
Resolve generic type in subclass? [message #876443] Thu, 24 May 2012 11:52 Go to next message
Eclipse UserFriend
Here is a scenario:

public class Super<T> {
protected T id;
}

public class Sub extends Super<Long> {}


Is there a way to evaluate the type of the field "id" within the scope of the class "Sub" using available API? I'm currently using JDT dom API, but other public API is acceptable.
Re: Resolve generic type in subclass? [message #877438 is a reply to message #876443] Sat, 26 May 2012 14:25 Go to previous messageGo to next message
Eclipse UserFriend
The pure dom AST doesn't have the information about type substitutions, but if you request the binding (TypeDeclaration#resolveBinding()) you'll enter the land of resolved type information.

Here's a passing unit test:
	public void testResolvedField() throws JavaModelException {
		this.workingCopy = getWorkingCopy("/Converter15/src/X.java", true/*resolve*/);
		String contents =
				"public class X<T> {\n" +
				"    T f;\n" +
				"}\n" +
				"class Sub extends X<String> {}\n" +
				"class Sub2 extends X<Number> {}\n";
		ASTNode node = buildAST(
				contents,
				this.workingCopy,
				false);
		assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
		CompilationUnit unit = (CompilationUnit) node;
		assertProblemsSize(unit, 0);
		TypeDeclaration type = (TypeDeclaration) unit.types().get(1);
		ITypeBinding typeBinding = type.resolveBinding();
		ITypeBinding superType = typeBinding.getSuperclass();
		IVariableBinding[] fields = superType.getDeclaredFields();
		assertEquals(fields[0].getType().getName(), "String");
		
		type = (TypeDeclaration) unit.types().get(2);
		typeBinding = type.resolveBinding();
		superType = typeBinding.getSuperclass();
		fields = superType.getDeclaredFields();
		assertEquals(fields[0].getType().getName(), "Number");
	}

HTH,
Stephan

[Updated on: Sat, 26 May 2012 14:25] by Moderator

Re: Resolve generic type in subclass? [message #879057 is a reply to message #877438] Wed, 30 May 2012 08:58 Go to previous messageGo to next message
Eclipse UserFriend
Yep, that helps a bunch. Thanks!
Re: Resolve generic type in subclass? [message #885110 is a reply to message #876443] Tue, 12 June 2012 09:35 Go to previous messageGo to next message
Eclipse UserFriend
Is there any way to do this for binary types?
Re: Resolve generic type in subclass? [message #885149 is a reply to message #885110] Tue, 12 June 2012 10:35 Go to previous messageGo to next message
Eclipse UserFriend
You should be able to get the bindings using ASTParser#createBindings() and then you could start navigating from these bindings.
Re: Resolve generic type in subclass? [message #886721 is a reply to message #885149] Fri, 15 June 2012 10:21 Go to previous messageGo to next message
Eclipse UserFriend
Thanks, that helped a lot.

One question:

Should ASTParser#createBindings() for a BinaryMethod be an IMethodBinding? I'm getting an ITypeBinding instead.
Re: Resolve generic type in subclass? [message #886739 is a reply to message #886721] Fri, 15 June 2012 10:50 Go to previous messageGo to next message
Eclipse UserFriend
Could this be https://bugs.eclipse.org/bugs/show_bug.cgi?id=381503 ?
Re: Resolve generic type in subclass? [message #886749 is a reply to message #886739] Fri, 15 June 2012 11:11 Go to previous message
Eclipse UserFriend
Yeah, that looks to be right actually. Didn't notice they were all constructors, but yes.
Previous Topic:Improvement for 'find references'
Next Topic:Java Perspective disappeared
Goto Forum:
  


Current Time: Tue Jul 22 18:16:11 EDT 2025

Powered by FUDForum. Page generated in 0.03737 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top