-1

So I have a neat little function which I can use to construct objects when my functions don't know what classes they'll be dealing with. (Yes I do have some nice uses for this before Soritos or anyone else wants to tell me I'm illogical)

public static Object Construct(Class type, Class[] params, Object[] values){
    //Create an object of any class
    try{
       Constructor con = type.getConstructor(params);
       Object o = con.newInstance(values);
       return o;
    }
    catch(Exception e){
       System.out.println("CONSTRUCTOR EXCEPTION"); 
       return null;
    }
}

Anyway I have a class declared within another class file that I want the main class to be able to pass to this function.

public class aClass {
   public aClass(String S, String s){
      Class c = otherClass.class;
      Object o = Functions.Construct(c, new Class[]{String.class, String.class}, new Object[]{S, s});
   }    
}

class otherClass{   
    public otherClass(String arg1, String arg2){}
}

But with this I get an exception. Is this because otherClass is not visible to my constructor function outside of this class?

T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
TheOnlyWAR10CK93
  • 113
  • 1
  • 10
  • 1
    Aside from any comment on the Rube-Goldberg quality of this code, you probably want to post your stack trace first. – Mena Apr 22 '15 at 10:09
  • What's funny about my code? It's a simplified example of my problem. Well that's the thing it's catching the exception so I'm not sure what's causing it. – TheOnlyWAR10CK93 Apr 22 '15 at 10:11
  • Use a debugger? Use proper `Exception` handling instead of your suppression? There's little to answer here unless you post the whole problem, not just some code dump. – Mena Apr 22 '15 at 10:11
  • No exceptions for me! Code executed successfully. – gopi1410 Apr 22 '15 at 10:12
  • This is the whole problem, stop being obnoxious. I'll find a way to get information about the Exception. Seeing as it's still throwing an exception at this simplified level however it works perfectly for public classes I thought this might be something obvious for an experienced java programmer. – TheOnlyWAR10CK93 Apr 22 '15 at 10:15
  • Someone posted an answer that worked but deleted it http://stackoverflow.com/questions/17485297/java-how-to-instantiate-inner-class-with-reflection – TheOnlyWAR10CK93 Apr 22 '15 at 10:17

2 Answers2

2

If Functions and aClass (assuming otherClass is in the same file as aClass) are in the same package, your code does not raise any exception. ITOH, if these 2 classes are not in the same package, there is an exception:

java.lang.IllegalAccessException: Class pack1.Functions can not access a member of class pack2.otherClass with modifiers "public"

The constructor of otherClass is public and is found by reflection, but the class itself has package private visibility. That's why Functions#Construct(...) cannot instantiate it.

If you really want to instantiate through reflection package private classes, you can bypass the access security check with setAccessibility(true) as described in the javadoc and suggested by @Anuj

A value of true indicates that the reflected object should suppress Java language access checking when it is used.

public static Object Construct(Class type, Class[] params, Object[] values){
    //Create an object of any class
    try{
       Constructor con = type.getConstructor(params);
       con.setAccessible(true);
       Object o = con.newInstance(values);
       return o;
    }
    catch(Exception e){
       System.out.println("CONSTRUCTOR EXCEPTION"); 
       return null;
    }
}
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
1

If your context doesn't use a security manager you can try making the constructor accessible by reflection.

con.setAccessible(true);
Anuj Shah
  • 71
  • 4