Iterating through classes with static functions

I have Model class like this

class ModelName {
    public static void createTable(SQLiteDatabase db){
        // code to create Table in DB
    }

    public static void deleteTable(SQLiteDatabase db){
        // code to delete Table from DB
    }
}

Every class has these static functions createTable, deleteTable I'd like to iterate through classes like this:

SQLiteDatabase db_object = ....; //init the object
Class<?>[] models = {ModelName, OtherModelName};
for(Class<?> model : models){
    model.deleteTable(db_object);
}

But defining Interface for public static void is not possible, and I have no reason to create Instances

How to achieve this? Can't really find the right words to describe my idea

Answers


I don't know if this is what you are looking for but I just tested it with following.

Classes:

class ModelName {
    public static void createTable(SQLiteDatabase db){
        System.out.println(Thread.currentThread().getStackTrace()[1]);
    }

    public static void deleteTable(SQLiteDatabase db){
        System.out.println(Thread.currentThread().getStackTrace()[1]);
    }
}

class OtherModelName {
    public static void createTable(SQLiteDatabase db){
        System.out.println(Thread.currentThread().getStackTrace()[1]);
    }

    public static void deleteTable(SQLiteDatabase db){
        System.out.println(Thread.currentThread().getStackTrace()[1]);
    }
}

Main code:

Class<?>[] models = {ModelName.class, OtherModelName.class};

for(Class<?> model : models){
    Method method = model.getMethod("createTable", SQLiteDatabase.class);
    method.invoke(null, new SQLiteDatabase());
    method = model.getMethod("deleteTable", SQLiteDatabase.class);
    method.invoke(null, new SQLiteDatabase());
}

Output:

demo.reflection.ModelName.createTable(IterateClassStaticMethod.java:12)
demo.reflection.ModelName.deleteTable(IterateClassStaticMethod.java:16)
demo.reflection.OtherModelName.createTable(IterateClassStaticMethod.java:22)
demo.reflection.OtherModelName.deleteTable(IterateClassStaticMethod.java:26)

Note: To me this looks like more overhead than just creating instances. Also, the code looks less readable and manageable like this.


Try this (untested):

for(Class<?> model : models){
    model.getMethod("deleteTable",SQLiteDatabase.class).invoke(null,db_object);
}

Need Your Help

How to search an array of bytes for “StringA”?

arrays delphi byte freepascal

Using FreePascal (or Delphi if no FP examples), given a 2048 byte buffer that is as an "array of bytes", how can I search the buffer for "StringA"?

How to append a constant value to a relation in pig

hadoop apache-pig

I want to keep the tuple count of a relation to do some calculation, what is the most efficient solution. Currently i approach it like this :

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.