Discussion:
[jboss-user] [Javassist] - Re: Grabbing an existing class's constructors?
Robby Cornelissen
2013-05-21 10:46:46 UTC
Permalink
Robby Cornelissen [https://community.jboss.org/people/robby.cornelissen] created the discussion

"Re: Grabbing an existing class's constructors?"

To view the discussion, visit: https://community.jboss.org/message/818605#818605

--------------------------------------------------------------
I think you're mixing two concepts:
* Class initializers, AKA static constructor, AKA static initialization block, AKA static { // ... }. This can be obtained by calling getClassInitializer() on a CtClass object, which will return you a corresponding CtConstructor object (if a static initializer was declared in the class).
* Constructors, which can be obtained by calling getConstructors() on a CtClass object, which will return you an array of CtConstructor objects.
As you are undoubtedly aware, the former is used for class initialization, the latter for object initialization.
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/818605#818605]

Start a new discussion in Javassist at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2062]
Melissa Dale
2013-05-23 00:43:08 UTC
Permalink
Melissa Dale [https://community.jboss.org/people/mdale] created the discussion

"Re: Grabbing an existing class's constructors?"

To view the discussion, visit: https://community.jboss.org/message/818976#818976

--------------------------------------------------------------
Thank you for replying, You are right, I do understand the difference between class initialization and object initialization in theory, and what I had posted was an extremely sloppy attempt made in desperation (although I acknowledge it is entirely possible I am failing to keep them separate in practice

Instead of using ".isEmpty()" I have used a try/catch, where it tries to create the coupling, and if it fails I assume it's because a ctclass's constructor is not empty. In the catch block I attempt to insert an empty class constructor on the ctclass object (so to overload the constructor) So it's like this:

String from_class = from.getName();
//Persistent Coupling
if(strength == 'P' || strength == 'p'){
try{
String toBeAdded = from_class + varname+" = new " + from_class+"();";
CtField f = CtField.make(toBeAdded, to);
to.addField(f);
}
//Hopefully this will catch cases where constructors are not empty and insert a dummy instructor
catch(Exception e){
from.makeClassInitializer();

String toBeAdded = from_class + varname+" = new " + from_class+"();";
CtField f = CtField.make(toBeAdded, to);
to.addField(f);
}


I still get a 'Exception in thread "main" javassist.CannotCompileException: [source error] no such constructor'. And as I mentioned, ideally I would like to use the getConstructors() on a ctclass to get the array of constructor objects, but this is much fuzzier in my mind. Once I have the array of constructor objects, is there a way to determine the object types? If I can loop through the array I can make a coupling with dummy variables, becasue I don't really care about the program's execution after it it coupled, only that it will compile. Something *roughly* like:

CtConstructor [] consts = new CtConstructor[];


ArrayList <?> results = new ArrayList<?>();


for (int i=0; i<consts.length(); i++){
          if (consts[i].type == int){
                    results[i]=13;
          else if (consts[i].type == double){
                    results[i] = 13.5;
          }
          else if (consts[i].type == char){
                    results[i] = 'M';
          }
          else if (consts[i].type == float){
                    results[i] = 13.5;
          }
          else if (consts[i].type == double){
                    results[i] = 13.5;
          }
          else if (consts[i].type == boolean){
                    results[i] = 13.5;
          }
          else{
                    results[i]=null;
          }
          }//for
}//method
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/818976#818976]

Start a new discussion in Javassist at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2062]
Melissa Dale
2013-05-24 00:52:14 UTC
Permalink
Melissa Dale [https://community.jboss.org/people/mdale] created the discussion

"Re: Grabbing an existing class's constructors?"

To view the discussion, visit: https://community.jboss.org/message/819206#819206

--------------------------------------------------------------
Thank you, you are correct, I was confusing the two (even though I did understand the difference, I was being sloppy in my code). I have not attempted to grab the constructors, but inserting an empty constructor takes care of my problem, and I think I have an idea of how to carry forward in my attempts to work with the array from the getConstructors(). Thank you again!
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/819206#819206]

Start a new discussion in Javassist at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2062]
Loading...