Pedantic
Pedantic

Reputation: 1378

How to pass class name in function arguments

I want to create a function that will perform some operation(most time occurring) I created function like following

public void doSth()
{
   //logic
   ClassName.staticMethod();
   //logic
}

In My application there are many times this function will be called. Only the particular line will be change. I decided to give a common function.

Now my question is: How do I pass the ClassName in function arguments so that function body use it dynamically?

Thanks

Upvotes: 1

Views: 8703

Answers (8)

karan vs
karan vs

Reputation: 3364

Make use of getcount(),getitem() methods in your adapter to achieve this: Here list is the datasource you are passing to ur adapter,

public int getCount() {
    int count=0;
    for(int i=0;i<list.size();i++)
    {
        if(//ur condition to include the item)
        {
            count++;
        }
    }
    return count;
}

@Override
public Object getItem(int position) {


    if(//ur condition to include the item)
    {
       return list.get(position);

    }
    else
    {
        return null;
    }
}

and in getview()

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.urlayout, parent, false);



    if(list.get(position)!=null) {


    }

    return convertView;
}

Upvotes: 0

CMR
CMR

Reputation: 985

The more elegant solution would be to use strategy pattern. The code would change to


void f(X x) {
    // some code
    IStrategy strategy = decideStrategy(x);
    strategy.method();
    // rest of the logic
}

Upvotes: 1

aioobe
aioobe

Reputation: 420951

I'd recommend you to consider refactoring your code and specify interface as argument type.

You could for instance use a Runnable and the simply do arg.run() instead of ClassName.staticMethod().

Example:

public void doSth(Runnable action) {
    // logic
    action.run();
    // logic
}

Upvotes: 1

sudarsan
sudarsan

Reputation: 283

use java's reflection mechanism

public void someFunction() {

this.getClass().getName();// returns name of class

}

so this way you don't need to pass any arguments

Upvotes: -1

Artefacto
Artefacto

Reputation: 97805

You'll probably want to pass the class directly, not the its name (otherwise use Class.forName). Then it's just a matter of calling it using reflection:

public void doSth(Class<?> clazz) throws NoSuchMethodException,
        IllegalAccessException, IllegalArgumentException,
        InvocationTargetException {
    Method method = clazz.getMethod("staticMethod");
    if (Modifier.isStatic(method.getModifiers())) {
        Object result = method.invoke(null);
        //do sth with result
    } else {
        // ...
    } 
}

Upvotes: 2

flolo
flolo

Reputation: 15486

You could pass two strings, one with class and one with method name. Then you just invoke with Class.forname(classname).getMethod(classname, null).invoke(null, null).

EDIT: This works only if the method is static and has no arguments (else you would replace the nulls with other values).

EDIT: Other Option you got (as mentioned strings for classes and methods are not nice), is to declare an interface and make all Classes with staticMethod implement it (if necessary a wrapper method to call the real static method for the classes, if the staticMethod name is not equal in all classes) and then you just use the interface-Type as parameter.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074038

You can do that, via Class.forName which accepts a fully-qualified class name and returns a Class instance. Then you have to get the Method for the static method in question via getMethod, and invoke it via invoke.

But passing around class names as strings is a suspect design decision. I'd look at alternatives, such as using singletons rather than static methods and an interface, that sort of thing.

Upvotes: 5

hanumant
hanumant

Reputation: 1101

You can use class.forName() method to get an instance of the required class .. see more here

Upvotes: 1

Related Questions