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

Hi Jay,

we have a similar challenge in the EclEmma code coverage plug-in: We get binary type names from the JVM and need to map them to the JDT model elements of Java sources. For this we implemented a interpolation of the binary names for anonymous inner classes:
 
https://github.com/eclipse/eclemma/blob/master/org.eclipse.eclemma.core/src/org/eclipse/eclemma/internal/core/analysis/TypeTraverser.java#L186

Not saying that this this works for all cases but here you can see our positiv test cases:

https://github.com/eclipse/eclemma/blob/master/org.eclipse.eclemma.core.test/src/org/eclipse/eclemma/internal/core/analysis/TypeTraverserTest.java
https://github.com/eclipse/eclemma/blob/master/org.eclipse.eclemma.core.test/testdata/src/typetraverser/Samples.java


Maybe this helps.

Regards,
-marc



On 11. Apr 2019, at 11:53, Jayaprakash Arthanareeswaran <jarthana@xxxxxxxxxx> wrote:

Unfortunately, Java model elements don't work without the context of a Java project.
So, yes, it is unlikely to work for you.

Now, I am less hopeful of finding another way. These things (the anonymous names)
are decided much later in the compilation process and not sure DOM, which solely
works with source code has the capability to find that. May be others can correct me
if I am wrong.

Regards,
Jay


<graycol.gif>Alex Lovkov ---11/04/2019 02:46:47 PM---I just tried all methods which return something:) I read in docs: that if I don't use java-plugin, I

From: Alex Lovkov <charmik1994@xxxxxxxxx>
To: Jayaprakash Arthanareeswaran <jarthana@xxxxxxxxxx>
Cc: "Eclipse JDT general developers list." <jdt-dev@xxxxxxxxxxx>
Date: 11/04/2019 02:46 PM
Subject: Re: [jdt-dev] get name of the anonymous class





I just tried all methods which return something:) 
I read in docs: that if I don't use java-plugin, I can specify ASTParser by unitName and Environment, but it didn't help me at all. .getJavaElement returns null anyway, from your code. So how can I get real name of anonymous class? Like Main$1$2 for example? Is it really possible? Thanks!

ÑÑ, 11 ÐÐÑ. 2019 Ð. Ð 06:41, Jayaprakash Arthanareeswaran <jarthana@xxxxxxxxxx>:
    I am surprised it returned non null for a source file. I thought the getBinaryName() will return
    anything meaningful only for elements loaded as binaries.


    Jay


    <graycol.gif>Alex Lovkov ---10/04/2019 08:44:45 PM---I got name of the anonymous class by this: @Override

    From:
    Alex Lovkov <charmik1994@xxxxxxxxx>
    To:
    Jayaprakash Arthanareeswaran <jarthana@xxxxxxxxxx>
    Cc:
    "Eclipse JDT general developers list." <jdt-dev@xxxxxxxxxxx>
    Date:
    10/04/2019 08:44 PM
    Subject:
    Re: [jdt-dev] get name of the anonymous class




    I got name of the anonymous class by this: 
    @Override

    public
     boolean visit(AnonymousClassDeclaration node) {
    ITypeBinding 
    resolveBinding = node.resolveBinding();
    String binaryName = resolveBinding.getBinaryName();

    return
     true;
    }
    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:

    @Override

    public
    boolean visit(AnonymousClassDeclaration node) {
    ITypeBinding
    resolveBinding = node.resolveBinding();
    IType
    javaElement = (IType) resolveBinding.getJavaElement();
    System.
    out.println(javaElement.getFullyQualifiedName());
    return
    true;
    }


    Jay


    <graycol.gif>"ÐÐÐÐÑÐÐÐÑ ÐÐÐÐÐÐ" ---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!

    example:
    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);
    parser.setKind(ASTParser.
    K_COMPILATION_UNIT);
    parser.setSource(
    new String(Files.readAllBytes(Paths.get(
    "src/main/java/Main.java"
    ))).toCharArray());
    CompilationUnit unit = (CompilationUnit) parser.createAST(
    new NullProgressMonitor());

    ArrayList<FieldDeclaration> fields =
    new ArrayList<>();
    unit.accept(
    new ASTVisitor() {
    @Override

    public boolean
    visit(final FieldDeclaration node) {
    fields
    .add(node);
    return super
    .visit(node);
    }
    });
    FieldDeclaration fieldDeclaration_T1 = fields.get(
    0);
    FieldDeclaration fieldDeclaration_T2 = fields.get(
    1);
    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

    jdt-dev@xxxxxxxxxxx
    To change your delivery options, retrieve your password, or unsubscribe from this list, visit

    https://www.eclipse.org/mailman/listinfo/jdt-dev



    --
    Ð ÐÐÐÐÐÐÐÐÐ, ÐÐÐÐÐÐ Ð.Ð.





--
Ð ÐÐÐÐÐÐÐÐÐ, ÐÐÐÐÐÐ Ð.Ð.


_______________________________________________
jdt-dev mailing list
jdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/jdt-dev