4

I need to create a class that extends an abstract class in runtime in Android. What I really need is to generate something like this:

Class A extends AbstractClass{

    A(){
         super("A name that is saved on AbstractClass");
         fieldFromAbstractClass =...
    }

    @Override
    public void aMethodFromAbstractClass(){
       //some code....
    }
}

I want to generate this at runtime. Is this possible?

Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • http://stackoverflow.com/questions/1011443/extending-or-adding-new-classes-at-runtime-in-java – Kazekage Gaara May 20 '12 at 16:21
  • I am confused as to what should stop you from doing this. – OmniOwl May 20 '12 at 16:21
  • @KazekageGaara Does it work on android? I also saw some libraries that seemed to work in Java but I have no idea if they work on android... Also, usually those libraries have a poor documentation :/ – Tiago Almeida May 20 '12 at 16:26

2 Answers2

5

In "traditional" Java, you can create and compile classes at runtime, or use byte code generators such as ASM to augment or generate class files.

However, you need to keep in mind that Android is not a Java virtual machine. When you create an APK, class files are converted into specialized bytecode that is processed by Android's Dalvik virtual machine.

I'm not aware of Dalvik-specific runtime bytecode generators, so I don't believe that it is (currently) possible to do what you described.

EDIT

There is a library called Dexmaker, which might accomplish this. I discovered it from this related answer.

Community
  • 1
  • 1
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
  • That is what I was afraid of. So the methods provided by @Kazekage Gaara will not work, right? – Tiago Almeida May 20 '12 at 17:06
  • It works for regular Java, but I doubt it will run on Android. I'm not 100% certain, though -- would love to hear from one of the gurus. – Tony the Pony May 20 '12 at 19:59
  • It doesn't seem easy to do it and I just don't want to spend time trying and then I figure it out it can't be done. How can we call those gurus? – Tiago Almeida May 20 '12 at 20:05
  • I'll post it as a separate question tomorrow, because I'm curious to know more. But what exactly are you trying to do in your app? – Tony the Pony May 20 '12 at 20:57
  • Does it make a difference if you are the one that created the question? Anyways, if you do that please post here the link. Kinda hard to explain by comment what I am trying to do. But already thought in some alternative solutions ;) – Tiago Almeida May 20 '12 at 22:45
  • No difference... I was going to post a more general question, but after some research, I found an interesting tool called Dexmaker (see edit) – Tony the Pony May 21 '12 at 09:50
  • It will take some time to have sure if it works but it seems like an option. Thanks. – Tiago Almeida May 21 '12 at 11:05
  • @TonythePony Did you get the chance to test out DexMaker? I'm only trying to create models. Class with a few fields that are strings and public this is it. I want o create an array list out of it after setting value inside its fields. Any help will be deeply appreciated. – hamada147 Jul 05 '18 at 10:32
0

You can make A an anonymous inner class. However, you cannot provide a new constructor. For your example define fieldFromAbstractClass in AbstractClass and provide a getter and a setter.

    AbstractClass a = new AbstractClass() {
        @Override
        public void aMethodFromAbstractClass() {
           // ...
        }
    }
    a.setFieldFromAbstractClass( someValue );
// do something with A

You can also reference variables from the containing block in the inner class if the are declared final. For more information I recommend to read some Java tutorial or book.

Stefan
  • 4,645
  • 1
  • 19
  • 35