Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
Can I get the instance of the calling object in Java?

There's a library which calls my method with a few arguments. I'd like to receive another argument, but the library doesn't provide it to the method it calls.

By decompiling the library, I can see that it has the argument, and it's assigned to an instance variable (not private, but not public either.) I know I can get at the variable using reflection if I have the instance, but I don't have the instance, either.

Is there a way I can get at the instance? SecurityManager has getClassContext(), but that just gives me the class of the instance - I want the instance itself.

As a quick example of what I want:

public class A {
    int b;
    public A(int b, int c) {
        this.b = b;
        D(c);
    }
}

public class D {
    public D(int c) {
        // Somehow I want to get at the calling instance of A's b from here,
        // and A belongs to a library which I didn't write.
    }
}

Alternatively... I do know that b was used as an argument on the callstack. So if someone knows how I could access the b which was passed into A's constructor, that would be acceptable.

If neither of these are doable... I can decompile A and compile it the way I want, and then I'd either need to do some classloading wizardry or I'd have to modify the contents of the jar. Neither of those sound desirable, so I'm hopeful someone knows how to either access the instance of A or the argument b from the call stack.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

With the help of code you provided, i could think of writing an aspect i mean use aop and try using joinpoint to get the arguments that are passed to constructor A()

about 4 years ago · Santiago Trujillo Report

0

Somehow I want to get at the calling instance of A's b from here

Short answer: you can't.

Long answer: you can get it if you either run it on a debugger, or your own custom JVM.

Yet another solution: use aspect-oriented programming with a framwork like AspectJ

about 4 years ago · Santiago Trujillo Report

0

You cannot do that. See this post for more details.

There is no such thing as an 'owner', so you cannot get the calling instance, unless an explicit reference to the parent is specified:

class A {
    int b;
    A(int b, int c) {
        this.b = b;
        new D(this, c);
    }
}

class D {
    D(A a, int c) {
        ...
    }
}

It seems that either according to the library your are not supposed to get such instance, or it is poorly designed.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!