Is it possible to see through which child class the parent static method called Java was?

First, a little background. I am exploring the possibility of implementing Ruby ActiveRecord in Java as cleanly and concisely as possible. To do this, I will need to allow the following method call:

Person person = Person.find("name", "Mike");

To solve something like:

ActiveRecord.find(Person.class, "name", "Mike");

It is assumed that Person extends ActiveRecord, which will have a static search method with two parameters (column, value). This method should know that it was called through Person.find, and not another domain class, such as Car.find, and call the find (Class, String, Object) method to perform the actual operation.

The problem I'm facing is figuring out through which child class of the ActiveRecord the static search method is called (two parameters). The following is a simple example:

public class A {
  public static void testMethod() {
    // need to know whether A.testMethod(), B.testMethod(), or C.testMethod() was called
  }
}

public class B extends A { }
public class C extends A { }

public class Runner {
  public static void main(String[] args) {
    A.testMethod();
    B.testMethod();
    C.testMethod();
  }
}

, , - aspectJ. testMethod() , . , ( VM args) .

?

java - groovy/ruby ​​/python?

- ActiveRecord.find Person.save ?

+3
7

Java, . , B.testMethod() A.testMethod , .

, Java.

+3

, , Java, . , . (, .)

, , , .

.

Person person=(Person)ActiveRecord.find(Person.class, "name", "Mike");

?

, , .

Person person=new Person();
person.find("name", "Mike");

Person, , "this.getClass()".

Person , getClass(), . :

Person dummyPerson=new Person();
Person realPerson=dummyPerson.find("name", "Mike");

, , ActiveRecord , find ActiveRecord, , , , . - .

, , Java , . , , , . "foo" "bar", :

Record bar=Record.get(key);
String foo=bar.get("foo");

BarRecord bar=BarRecord.get(key);
String foo=bar.getFoo();

, , , .

+1

Java. , - :

public interface Finder<T, RT, CT>
{
    T find(RT colName, CT value);
}

public class PersonFinder
    implements Finder<Person, String, String>   
{
    public Person find(String nameCol, String name)
    {
        // code to find a person
    }
}

public class CarFinder
    implements Finder<Car, String, int>   
{
    public Person find(String yearCol, int year)
    {
        // code to find a car
    }
}
0

, .

, .

, , . , .

, :

 class MyClass {
      private static final SomeLogger logger = SomeLogger.getLogger();
      ....
  }

, .

, - :

 class A  {
      public static void myStatic() {
          // find out who call it
          String calledFrom = new RuntimeException()
                              .getStackTrace()[1].getClassName();
      }
 }

. 1000 . , .

AspectJ.

0

, , , . Java, , , , - . , - ...

public class Person extends PersonActiveRecord
{

}

//generated class, do not touch
public class PersonActiveRecord extends ActiveRecord
{
   public Person find(Map params)
   {
      ActiveRecord.find(Person.class, params);
   }
}

. , . .

0

, .

A example = new B(B.class);

, .

, , - - ...

Thread.currentThread().getStackTrace()

, - javassist.

0

, ActiveRecord Java. , . Java, . ActiveJDBC: http://code.google.com/p/activejdbc/

, , , . Model.getClassName().

. , ( , !). Javassist. ActiveRecord Java. , Glassfish Weblogic -. http: activejdbc.googlecode.com/svn/trunk/activejdbc-instrumentation/ Maven.

, .

Enjoy,

0

Source: https://habr.com/ru/post/1724955/


All Articles