Add classes to software at runtime

Asked

Viewed 169 times

3

Question

There is some feature that some language makes available to be able to add at runtime a class to the running software, that is, the software to recognize and thus be able to instantiate objects of this class and manipulate them?


Let us imagine the following situation:
Take with example the language Java. We have this abstract class available as a skeleton for users to implement:

public abstract class TypeGeneral {

    MyAttribute my_atr;   // um objeto qualquer que toda implementação
                          // de TypeGeneral tem que ter
    public General(MyAttribute e) {
         my_atr = e;
    }

    public abstract void run();    // <==

    public MyAttribute getAttr() { /*...*/ }
    public MyAttribute setAttr(MyAttribute e) { /*...*/ }

    // ...
}

I was wondering if there are any features that give the possibility that after the user makes his or her class extending the TypeGeneral, and thus implementing the method run(), can, at runtime, add it to the system that is running, and the system, recognizing it, makes manipulations with the objects of it.

I saw something about Reflection, but could not adapt to my problem. Someone?

Preferred languages

  • Java
  • Python
  • C#
  • C++
  • Does anyone suggest a better title? Don’t edit, suggest right here.

  • This is highly dependent on the language and the execution environment. It would be interesting to restrict your domain more.

  • I know there’s a way, but me Java I don’t know the details of how to do.

  • 1
  • In what language do you know @bigown? It might be in one of the other 3 I quoted... I’ll look here @Piovezan

  • The question has the tag [tag:java], until not to get wide.

  • For example in a Web environment @Pablo Almeida, with some of these languages, where the server would receive from each user its implementations and return to each one the functionality added by its implementation.

  • I put java on account of the example I present @bigown kk

Show 3 more comments

1 answer

0


This is very common in Java. JDBC drivers, for example, are loaded into memory through this type of mechanism. This is useful in situations where it is not possible to know a priori (at development time) the class that will be instantiated.

A small example. Suppose there is the following class in your system:

package com.company;

class TesteClasseDinamica {
    public int x;
    public int y;
}

Suppose now that you are in another class and want to instantiate a class object com.company.TesteClasseDinamica dynamically:

public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    Class clazz = Class.forName("com.company.TesteClasseDinamica");

    TesteClasseDinamica t = (TesteClasseDinamica) clazz.newInstance();
    t.x = 2;
    t.y = 3;

    System.out.print(t.x);
    System.out.print(t.y);
}

Note that, in this scenario, to instantiate a class object TesteClasseDinamica the method shall be used newInstance() class Class.

This is very important in JDBC, as the interface needs to be generic in order to suit any relational database. Since it is not possible to predict which implementation (JDBC driver) will be used by the application, this mechanism is used to dynamically instantiate the driver.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.