Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » working with the AST
working with the AST [message #639099] Mon, 15 November 2010 10:00 Go to next message
Jochen Huck is currently offline Jochen HuckFriend
Messages: 8
Registered: August 2010
Junior Member
Hi,

lets assume that I've got a reference to a MethodInvocation m and now I want to get the return type to create a new Variable of this type. Therefore I need a reference of type Type.
So I call m.resolveBinding().getReturnType() but thats of type ITypeBinding. How can I get a reference of type Type?

Thanks,

Jochen
Re: working with the AST [message #639168 is a reply to message #639099] Mon, 15 November 2010 13:28 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
You can create a type reference using the factory methods in the Ast class. You can retrieve the ast object from your method invocation using getAst().

The type binding has a fully qualified name that you can use to create a new type reference.

HTH,
--
Olivier
Re: working with the AST [message #639436 is a reply to message #639099] Tue, 16 November 2010 14:35 Go to previous messageGo to next message
Jochen Huck is currently offline Jochen HuckFriend
Messages: 8
Registered: August 2010
Junior Member
Hi,

that's exactly what I actually did. I just wonder why this functionallity isn't already implemented. I would have expected that it is possible to say ITypeBinding bind; Type type = bind.getType() or something like that. Here is the code that does the trick:

public static Type getTypeFormITypeBinding(ITypeBinding binding, AST ast) {
Type type = null;
if (binding.isPrimitive()) {
PrimitiveType.Code code = null;
if (binding.getName().equals("byte")) {
code = PrimitiveType.BYTE;
}
if (binding.getName().equals("short")) {
code = PrimitiveType.SHORT;
}
if (binding.getName().equals("char")) {
code = PrimitiveType.CHAR;
}
if (binding.getName().equals("int")) {
code = PrimitiveType.INT;
}
if (binding.getName().equals("long")) {
code = PrimitiveType.LONG;
}
if (binding.getName().equals("float")) {
code = PrimitiveType.FLOAT;
}
if (binding.getName().equals("double")) {
code = PrimitiveType.DOUBLE;
}
if (binding.getName().equals("boolean")) {
code = PrimitiveType.BOOLEAN;
}
type = ast.newPrimitiveType(code);
}
if (binding.isClass()) {
type = getSimpleTypeFromString(binding.getQualifiedName(), ast);
}
if (binding.isArray()) {
Type elementType = getTypeFormITypeBinding(binding.getElementType(), ast);
type = ast.newArrayType(elementType, binding.getDimensions());
}
if (binding.isWildcardType()) {
// TODO
}
if (binding.isParameterizedType()) {
// TODO
}

return type;
}

public static SimpleType getSimpleTypeFromString(String canonicalName, AST ast) {
String[] names = canonicalName.split("\\.");
SimpleName first = ast.newSimpleName(names[0]);
QualifiedName qName = null;
for (int i = 1; i < names.length; i++) {
SimpleName second = ast.newSimpleName(names[i]);
if (qName == null) {
qName = ast.newQualifiedName(first, second);
} else {
qName = ast.newQualifiedName(qName, second);
}
}

if (qName == null) {
return ast.newSimpleType(first);
} else {
return ast.newSimpleType(qName);
}
}

[Updated on: Tue, 16 November 2010 14:36]

Report message to a moderator

Re: working with the AST [message #639536 is a reply to message #639436] Tue, 16 November 2010 20:14 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
This would make sense for a helper method maybe, but it would not make sense on the API itself.

So we could have a method:
BindingUtils.getType(bind, ast);

but not what you suggest Smile.

We might have a lack of factory method for bindings. Not sure we would have a good place to add this.

Please open a bug report against JDT/Core as an enhancement to get a new API that would provide helper methods. JDT/UI might also take advantages of these APIs when available. At least with an enhancement request, we can discuss about adding some methods.

Thanks,

Olivier
Re: working with the AST [message #640037 is a reply to message #639536] Thu, 18 November 2010 18:38 Go to previous message
Jochen Huck is currently offline Jochen HuckFriend
Messages: 8
Registered: August 2010
Junior Member
Hi,

if anybody should be interested in furute here is the link to the bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=330416
Previous Topic:Errors with extends class
Next Topic:Shortcut Key to Select Token
Goto Forum:
  


Current Time: Thu Apr 25 12:35:10 GMT 2024

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

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

Back to the top