[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jdt-dev] get name of the anonymous class

I got name of the anonymous class by this:Â
publicÂbooleanÂvisit(AnonymousClassDeclarationÂnode) {
String binaryName = resolveBinding.getBinaryName();
but it gives me wrong name for inner anonymous classes, example:
new Serializable() {
void x() {
new Serializable() {
it prints Main$1 Main$2, but it should be Main$1 and Main$1$1. 
I get .getJavaElement()=null anyway. Can I fix it without ecpilse plugin? Thanks!

---------- Forwarded message ---------
ÐÑ: Jayaprakash Arthanareeswaran <jarthana@xxxxxxxxxx>
Date: ÑÑ, 10 ÐÐÑ. 2019 Ð. Ð 05:43
Subject: Re: [jdt-dev] get name of the anonymous class
To: Eclipse JDT general developers list. <jdt-dev@xxxxxxxxxxx>
Cc: ÐÐÐÐÑÐÐÐÑ ÐÐÐÐÐÐ <charmik1994@xxxxxxxxx>

One way of achieving that is by using Java model elements, like below:

public boolean visit(AnonymousClassDeclaration node) {
ITypeBinding resolveBinding = node.resolveBinding();
IType javaElement = (IType) resolveBinding.getJavaElement();
return true;


Inactive hide details for "ÐÐÐÐÑÐÐÐÑ ÐÐÐÐÐÐ" ---09/04/2019 10:59:12 PM---Hello, I was wondering how can I get th"ÐÐÐÐÑÐÐÐÑ ÐÐÐÐÐÐ" ---09/04/2019 10:59:12 PM---Hello, I was wondering how can I get the name of the anonymous class (exactly "$1", or Main$1) from

From: "ÐÐÐÐÑÐÐÐÑ ÐÐÐÐÐÐ" <charmik1994@xxxxxxxxx>
To: jdt-dev@xxxxxxxxxxx
Date: 09/04/2019 10:59 PM
Subject: [jdt-dev] get name of the anonymous class
Sent by: jdt-dev-bounces@xxxxxxxxxxx

Hello, I was wondering how can I get the name of the anonymous class (exactly "$1", or Main$1) from AST, after I parse my source file?
If I try to get the name of the class going recursive to the root from the variable, I don't getÂFieldDeclaration -> TypeDeclaration as in normal (Z) class, I get the next one:ÂFieldDeclaration -> AnonymousClassDeclaration -> ClassInstanceCreation
and AnonymousClassDeclaration doesn't have any getName() methods.Â
So how can I get "$1" by AnonymousClassDeclaration/ClassInstanceCreation? thanks!

public class Main {
void f() {
new Serializable() {
public static final String T1 = "T1";
private class Z implements Serializable {
public static final String T2 = "T2";

ASTParser parser = ASTParser.newParser(AST.JLS11);
new String(Files.readAllBytes(Paths.get(
CompilationUnit unit = (CompilationUnit) parser.createAST(
new NullProgressMonitor());

ArrayList<FieldDeclaration> fields =
new ArrayList<>();
new ASTVisitor() {
public boolean visit(final FieldDeclaration node) {
return super.visit(node);
FieldDeclaration fieldDeclaration_T1 = fields.get(
FieldDeclaration fieldDeclaration_T2 = fields.get(
ASTNode T1parent = fieldDeclaration_T1.getParent();
//AnonymousClassDeclaration here (can't get $1)
ASTNode T2parent = fieldDeclaration_T2.getParent();
//TypeDeclaration here. can use .getName() to get a name of nested class) cl
Sincerely, Alex_______________________________________________
jdt-dev mailing list
To change your delivery options, retrieve your password, or unsubscribe from this list, visit