Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding methods to exists class in android

I'm looking for a way to add some methods into exists class like this:

String s = "";
s.doSomething();

In objective C, I can use category to do this.

@interface NSString( Stuff)
-(void)doSomething();
@end

Is android has something like that? Or another hack?


Update: Actually, I got this problem: I use a class (not final) from jar file (so, I can't touch its source code). Then I want to add methods( or something like that) into this class without using inheritance. For example:

 public class Provider{
       // many methods and fields go here...
       public String getName(){}
 }

All I want to do is:

 provider.print(); //that call getName() method;

I also tried proxy pattern, it worked, but I don't like that way (because it like a wrapper class, I must store an object with many fields and methods to use only one method):

 public class ProxyProvider{
       Provider provider;
       public ProxyProvider(Provider provider){
           this.provider = provider;
       }
       public void print(){
           String name = provider.getName();
           //do something
       }
 }

Is there any way to solve that?

like image 691
ductran Avatar asked Jul 26 '12 06:07

ductran


People also ask

How do you call a method from one class to another?

To do that, first, we need to create an object of “FirstClass” in the main method of the “SecondClass”. Once an object of the “FirstClass” is created, then we can invoke any method or attribute of the “FirstClass” within the “SecondClass” using that object.

What is super class in Android?

The superclass of a class is the class that the class was extended from, or null if it wasn't extended. for example, say you have a class called object, containing an onDestroy() method.


2 Answers

You could create a utility class with static methods:

public final class ProviderUtils {

    private ProviderUtils() {} // not instantiable, it is a utility class

    public static void print(Provider provider) {
        String name = provider.getName();
        // print the name
    }
}

In your code, you can then call it:

Provider p = new Provider(...);
ProviderUtils.print(p);

And if that class only has one print method, you can maybe call it ProviderPrinter instead of ProviderUtils.

In the end you don't have thousands of possibilities - you can:

  • extend the class and whatever method you need in the sub class => you said you don't want that
  • modify the source code of the class and recompile your own version of the jar
  • wrap the class in a wrapper that adds the methods you need (your ProxyProvider example)
  • put the methods you need in a static utility class (what I proposed above)
  • modify the class at runtime and add a method, but that's a complicated path because you need to play with classloaders.
like image 116
assylias Avatar answered Sep 21 '22 05:09

assylias


It is not possible, however, there is a java like DSL available called Xtend that can be used as a compelling replacement for JAVA that might be work looking at which supports extension methods like this.

http://www.eclipse.org/xtend/

DISCLAIMER: I am in no way associated to this I am just an avid user of the core technology that was used to create xtend called xtext. I have considered using xtend on an Android project

like image 35
Ian Warwick Avatar answered Sep 23 '22 05:09

Ian Warwick