Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Resolve generic type in subclass?
Resolve generic type in subclass? [message #876443] Thu, 24 May 2012 15:52 Go to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 200
Registered: July 2009
Senior Member
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 18:25 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1035
Registered: July 2009
Senior Member
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 18:25]

Report message to a moderator

Re: Resolve generic type in subclass? [message #879057 is a reply to message #877438] Wed, 30 May 2012 12:58 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 200
Registered: July 2009
Senior Member
Yep, that helps a bunch. Thanks!
Re: Resolve generic type in subclass? [message #885110 is a reply to message #876443] Tue, 12 June 2012 13:35 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 200
Registered: July 2009
Senior Member
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 14:35 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
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 14:21 Go to previous messageGo to next message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 200
Registered: July 2009
Senior Member
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 14:50 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1035
Registered: July 2009
Senior Member
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 15:11 Go to previous message
Paul Fullbright is currently offline Paul FullbrightFriend
Messages: 200
Registered: July 2009
Senior Member
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: Thu Nov 27 05:52:02 GMT 2014

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

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