Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda