|
Re: Resolve generic type in subclass? [message #877438 is a reply to message #876443] |
Sat, 26 May 2012 18:25 |
Stephan Herrmann Messages: 1853 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
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04170 seconds