How to mock private class using jmockit? - java

public String processName() throws Exception {
String name = dbManager.getName(); // getname
String connection = dbManager.getConnection();
String name2 = dbManager.getName();
Mydata mydata = new Mydata();
String getData = mydata.getGetData();
List<String> list = dbManager.getList();
return getData.toLowerCase();
}
private class Mydata {
String getData = "test";
public String getGetData() {
return getData;
}
public void setGetData(String getData) {
this.getData = getData;
}
}

In general, you should not be using private classes and avoid writing the tests for them as they are supposed to be black-boxes for the outer world.
But still if you are using one and want to mock it then here is how you can do it using JMockit:
Object ins = Deencapsulation.newInnerInstance("Mydata", outerClass.class, (Object[]) null);
I have used null because you have not defined any constructor on your inner private class.
Here is the documentation if you are interested:
Object mockit.Deencapsulation.newInnerInstance(String
innerClassSimpleName, Object outerClassInstance, Object...
nonNullArgs)
The same as newInstance(String, Class[], Object), but for
instantiating an inner non-accessible class of some other class, and
where all other (if any) initialization arguments are known to be
non-null.
Parameters:
innerClassSimpleName:
the inner class simple name, that is, the part after the "$" character in its full name
outerClassInstance:
the outer class instance to which the inner class instance will belong
nonNullArgs:
zero or more non-null parameter values for the invocation; if a null value needs to be passed, the Class object for the
corresponding parameter type must be passed instead type to which
the returned instance should be assignable
Returns: a newly created instance of the specified inner class, initialized > with the given arguments
Throws: IllegalArgumentException - if a null reference was provided for a parameter

Related

Using 'Class'-type object to create an object it's representing [duplicate]

Is there a way to create an instance of a particular class given the class name (dynamic) and pass parameters to its constructor.
Something like:
Object object = createInstance("mypackage.MyClass","MyAttributeValue");
Where "MyAttributeValue" is an argument to the constructor of MyClass.
Yes, something like:
Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(new Object[] { ctorArgument });
That will only work for a single string parameter of course, but you can modify it pretty easily.
Note that the class name has to be a fully-qualified one, i.e. including the namespace. For nested classes, you need to use a dollar (as that's what the compiler uses). For example:
package foo;
public class Outer
{
public static class Nested {}
}
To obtain the Class object for that, you'd need Class.forName("foo.Outer$Nested").
You can use Class.forName() to get a Class object of the desired class.
Then use getConstructor() to find the desired Constructor object.
Finally, call newInstance() on that object to get your new instance.
Class<?> c = Class.forName("mypackage.MyClass");
Constructor<?> cons = c.getConstructor(String.class);
Object object = cons.newInstance("MyAttributeValue");
You can use reflections
return Class.forName(className).getConstructor(String.class).newInstance(arg);
If class has only one empty constructor (like Activity or Fragment etc, android classes):
Class<?> myClass = Class.forName("com.example.MyClass");
Constructor<?> constructor = myClass.getConstructors()[0];
when using (i.e.) getConstructor(String.lang) the constructor has to be declared public.
Otherwise a NoSuchMethodException is thrown.
if you want to access a non-public constructor you have to use instead (i.e.) getDeclaredConstructor(String.lang).
If anyone is looking for a way to create an instance of a class despite the class following the Singleton Pattern, here is a way to do it.
// Get Class instance
Class<?> clazz = Class.forName("myPackage.MyClass");
// Get the private constructor.
Constructor<?> cons = clazz.getDeclaredConstructor();
// Since it is private, make it accessible.
cons.setAccessible(true);
// Create new object.
Object obj = cons.newInstance();
This only works for classes that implement singleton pattern using a private constructor.
Another helpful answer. How do I use getConstructor(params).newInstance(args)?
return Class.forName(**complete classname**)
.getConstructor(**here pass parameters passed in constructor**)
.newInstance(**here pass arguments**);
In my case, my class's constructor takes Webdriver as parameter, so used below code:
return Class.forName("com.page.BillablePage")
.getConstructor(WebDriver.class)
.newInstance(this.driver);
You want to be using java.lang.reflect.Constructor.newInstance(Object...)
Very Simple way to create an object in Java using Class<?> with constructor argument(s) passing:
Case 1:-
Here, is a small code in this Main class:
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Main {
public static void main(String args[]) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Get class name as string.
String myClassName = Base.class.getName();
// Create class of type Base.
Class<?> myClass = Class.forName(myClassName);
// Create constructor call with argument types.
Constructor<?> ctr = myClass.getConstructor(String.class);
// Finally create object of type Base and pass data to constructor.
String arg1 = "My User Data";
Object object = ctr.newInstance(new Object[] { arg1 });
// Type-cast and access the data from class Base.
Base base = (Base)object;
System.out.println(base.data);
}
}
And, here is the Base class structure:
public class Base {
public String data = null;
public Base()
{
data = "default";
System.out.println("Base()");
}
public Base(String arg1) {
data = arg1;
System.out.println("Base("+arg1+")");
}
}
Case 2:- You, can code similarly for constructor with multiple argument and copy constructor. For example, passing 3 arguments as parameter to the Base constructor will need the constructor to be created in class and a code change in above as:
Constructor<?> ctr = myClass.getConstructor(String.class, String.class, String.class);
Object object = ctr.newInstance(new Object[] { "Arg1", "Arg2", "Arg3" });
And here the Base class should somehow look like:
public class Base {
public Base(String a, String b, String c){
// This constructor need to be created in this case.
}
}
Note:- Don't forget to handle the various exceptions which need to be handled in the code.
You can also invoke methods inside the created object.
You can create object instant by invoking the first constractor and then invoke the first method in the created object.
Class<?> c = Class.forName("mypackage.MyClass");
Constructor<?> ctor = c.getConstructors()[0];
Object object=ctor.newInstance(new Object[]{"ContstractorArgs"});
c.getDeclaredMethods()[0].invoke(object,Object... MethodArgs);

getDeclaredFields() returning nothing [duplicate]

I'm a little confused about the difference between the getFields method and the getDeclaredFields method when using Java reflection.
I read that getDeclaredFields gives you access to all the fields of the class and that getFields only returns public fields. If this is the case, why wouldn't you just always use getDeclaredFields?
Can someone please elaborate on this, and explain the difference between the two methods, and when/why you would want to use one over the other?
getFields()
All the public fields up the entire class hierarchy.
getDeclaredFields()
All the fields, regardless of their accessibility but only for the current class, not any base classes that the current class might be inheriting from.
To get all the fields up the hierarchy, I have written the following function:
public static Iterable<Field> getFieldsUpTo(#Nonnull Class<?> startClass,
#Nullable Class<?> exclusiveParent) {
List<Field> currentClassFields = Lists.newArrayList(startClass.getDeclaredFields());
Class<?> parentClass = startClass.getSuperclass();
if (parentClass != null &&
(exclusiveParent == null || !(parentClass.equals(exclusiveParent)))) {
List<Field> parentClassFields =
(List<Field>) getFieldsUpTo(parentClass, exclusiveParent);
currentClassFields.addAll(parentClassFields);
}
return currentClassFields;
}
The exclusiveParent class is provided to prevent the retrieval of fields from Object. It may be null if you DO want the Object fields.
To clarify, Lists.newArrayList comes from Guava.
Update
FYI, the above code is published on GitHub in my LibEx project in ReflectionUtils.
From Java Reflection tutorials:
As already mentioned, Class.getDeclaredField(String) only looks at the fields from the Class in which you call it.
If you want to search a Field in the Class hierarchy, you can use this simple function:
/**
* Returns the first {#link Field} in the hierarchy for the specified name
*/
public static Field getField(Class<?> clazz, String name) {
Field field = null;
while (clazz != null && field == null) {
try {
field = clazz.getDeclaredField(name);
} catch (Exception e) {
}
clazz = clazz.getSuperclass();
}
return field;
}
This is useful to find a private field from a superclass, for example. Also, if you want to modify its value, you can use it like this:
/**
* Sets {#code value} to the first {#link Field} in the {#code object} hierarchy, for the specified name
*/
public static void setField(Object object, String fieldName, Object value) throws Exception {
Field field = getField(object.getClass(), fieldName);
field.setAccessible(true);
field.set(object, value);
}
public Field[] getFields() throws SecurityException
Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface has no accessible public fields, or if it represents an array class, a primitive type, or void.
Specifically, if this Class object represents a class, this method returns the public fields of this class and of all its superclasses. If this Class object represents an interface, this method returns the fields of this interface and of all its superinterfaces.
The implicit length field for array class is not reflected by this method. User code should use the methods of class Array to manipulate arrays.
public Field[] getDeclaredFields() throws SecurityException
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.
And what if I need all fields from all parent classes?
Some code is needed, e.g. from https://stackoverflow.com/a/35103361/755804:
public static List<Field> getAllModelFields(Class aClass) {
List<Field> fields = new ArrayList<>();
do {
Collections.addAll(fields, aClass.getDeclaredFields());
aClass = aClass.getSuperclass();
} while (aClass != null);
return fields;
}

JPA: Is it possible to pass in the subclass type as an argument using a superclass constructor? [duplicate]

Is there a way to create an instance of a particular class given the class name (dynamic) and pass parameters to its constructor.
Something like:
Object object = createInstance("mypackage.MyClass","MyAttributeValue");
Where "MyAttributeValue" is an argument to the constructor of MyClass.
Yes, something like:
Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(new Object[] { ctorArgument });
That will only work for a single string parameter of course, but you can modify it pretty easily.
Note that the class name has to be a fully-qualified one, i.e. including the namespace. For nested classes, you need to use a dollar (as that's what the compiler uses). For example:
package foo;
public class Outer
{
public static class Nested {}
}
To obtain the Class object for that, you'd need Class.forName("foo.Outer$Nested").
You can use Class.forName() to get a Class object of the desired class.
Then use getConstructor() to find the desired Constructor object.
Finally, call newInstance() on that object to get your new instance.
Class<?> c = Class.forName("mypackage.MyClass");
Constructor<?> cons = c.getConstructor(String.class);
Object object = cons.newInstance("MyAttributeValue");
You can use reflections
return Class.forName(className).getConstructor(String.class).newInstance(arg);
If class has only one empty constructor (like Activity or Fragment etc, android classes):
Class<?> myClass = Class.forName("com.example.MyClass");
Constructor<?> constructor = myClass.getConstructors()[0];
when using (i.e.) getConstructor(String.lang) the constructor has to be declared public.
Otherwise a NoSuchMethodException is thrown.
if you want to access a non-public constructor you have to use instead (i.e.) getDeclaredConstructor(String.lang).
If anyone is looking for a way to create an instance of a class despite the class following the Singleton Pattern, here is a way to do it.
// Get Class instance
Class<?> clazz = Class.forName("myPackage.MyClass");
// Get the private constructor.
Constructor<?> cons = clazz.getDeclaredConstructor();
// Since it is private, make it accessible.
cons.setAccessible(true);
// Create new object.
Object obj = cons.newInstance();
This only works for classes that implement singleton pattern using a private constructor.
Another helpful answer. How do I use getConstructor(params).newInstance(args)?
return Class.forName(**complete classname**)
.getConstructor(**here pass parameters passed in constructor**)
.newInstance(**here pass arguments**);
In my case, my class's constructor takes Webdriver as parameter, so used below code:
return Class.forName("com.page.BillablePage")
.getConstructor(WebDriver.class)
.newInstance(this.driver);
You want to be using java.lang.reflect.Constructor.newInstance(Object...)
Very Simple way to create an object in Java using Class<?> with constructor argument(s) passing:
Case 1:-
Here, is a small code in this Main class:
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Main {
public static void main(String args[]) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Get class name as string.
String myClassName = Base.class.getName();
// Create class of type Base.
Class<?> myClass = Class.forName(myClassName);
// Create constructor call with argument types.
Constructor<?> ctr = myClass.getConstructor(String.class);
// Finally create object of type Base and pass data to constructor.
String arg1 = "My User Data";
Object object = ctr.newInstance(new Object[] { arg1 });
// Type-cast and access the data from class Base.
Base base = (Base)object;
System.out.println(base.data);
}
}
And, here is the Base class structure:
public class Base {
public String data = null;
public Base()
{
data = "default";
System.out.println("Base()");
}
public Base(String arg1) {
data = arg1;
System.out.println("Base("+arg1+")");
}
}
Case 2:- You, can code similarly for constructor with multiple argument and copy constructor. For example, passing 3 arguments as parameter to the Base constructor will need the constructor to be created in class and a code change in above as:
Constructor<?> ctr = myClass.getConstructor(String.class, String.class, String.class);
Object object = ctr.newInstance(new Object[] { "Arg1", "Arg2", "Arg3" });
And here the Base class should somehow look like:
public class Base {
public Base(String a, String b, String c){
// This constructor need to be created in this case.
}
}
Note:- Don't forget to handle the various exceptions which need to be handled in the code.
You can also invoke methods inside the created object.
You can create object instant by invoking the first constractor and then invoke the first method in the created object.
Class<?> c = Class.forName("mypackage.MyClass");
Constructor<?> ctor = c.getConstructors()[0];
Object object=ctor.newInstance(new Object[]{"ContstractorArgs"});
c.getDeclaredMethods()[0].invoke(object,Object... MethodArgs);

Getting a Field from an instance?

Is there any way to get a Field reference from an instance (not from a class) ?
This is an example :
public class Element {
#MyAnnotation("hello")
public String label1;
#MyAnnotation("world")
public String label2;
}
public class App {
private Element elem = new Element();
public void printAnnotations() {
String elemLabel1 = elem1.label;
String elemLabel2 = elem2.label;
// cannot do elemLabel.getField().getDeclaredAnnotations();
String elemLabel1AnnotationValue = // how ?
String elemLabel2AnnotationValue = // how ?
}
}
Sorry for not being too clear, but i already know how to fetch Fields from a class (Class --> Field --> DeclaredAnnotations)
What i am wondering is how to get the Field for a particular instance.
In this example, from elemLabel1 string instance, i wish to be able to get the Field of Element.label1.
What exactly do you mean? A Field on defined on the Class. You can get the value for a specific instance:-
private static class Test {
private int test = 10;
}
public static void main(String[] args) throws Exception {
final Test test = new Test();
final Field field = Test.class.getDeclaredField("test");
field.setAccessible(true);
final int value = field.getInt(test);
System.out.println(value);
}
The the class Test has a Field called test. This is true of any Test - it is defined in the Class. The instance of the class is has a specific value for that Field, in this case 10. This can retrieved for a specific instance using the getXXX or get method.
EDIT
From the code in your question it looks like you want the value of an Annotation field not the value of a class field.
In Java, values in annotations are compile time constants and therefore are also defined at the class rather than instance level.
public class Element {
#MyAnnotation("l")
public String label;
}
In your example, the MyAnnotation value field must be equal to 1 for every instance of Element.
Field belongs to class. Therefore you actually want to do the following:
elemLabel.getClass().getField("theFieldName").getDeclaredAnnotations();
However although your field is public typically all fields should be private. In this case use getDeclaredField() instead of getField().
EDIT
you have to call field.setAccessible(true) before using the field.

Create new object from a string in Java

Is there a way to create a new class from a String variable in Java?
String className = "Class1";
//pseudocode follows
Object xyz = new className(param1, param2);
Also, if possible does the resulting object have to be of type Object?
There may be a better way, but I want to be able to retrieve values from an XML file, then instantiate the classes named after those strings. Each of these classes implement the same interface and are derived from the same parent class, so I would then be able to call a particular method in that class.
This is what you want to do:
String className = "Class1";
Object xyz = Class.forName(className).newInstance();
Note that the newInstance method does not allow a parametrized constructor to be used. (See Class.newInstance documentation)
If you do need to use a parametrized constructor, this is what you need to do:
import java.lang.reflect.*;
Param1Type param1;
Param2Type param2;
String className = "Class1";
Class cl = Class.forName(className);
Constructor con = cl.getConstructor(Param1Type.class, Param2Type.class);
Object xyz = con.newInstance(param1, param2);
See Constructor.newInstance documentation
Yes, you can load a class on your classpath given the String name using reflection, using Class.forName(name), grabbing the constructor and invoking it. I'll do you an example.
Consider I have a class:
com.crossedstreams.thingy.Foo
Which has a constructor with signature:
Foo(String a, String b);
I would instantiate the class based on these two facts as follows:
// Load the Class. Must use fully qualified name here!
Class clazz = Class.forName("com.crossedstreams.thingy.Foo");
// I need an array as follows to describe the signature
Class[] parameters = new Class[] {String.class, String.class};
// Now I can get a reference to the right constructor
Constructor constructor = clazz.getConstructor(parameters);
// And I can use that Constructor to instantiate the class
Object o = constructor.newInstance(new Object[] {"one", "two"});
// To prove it's really there...
System.out.println(o);
Output:
com.crossedstreams.thingy.Foo#20cf2c80
There's plenty of resources out there which go into more detail about this, and you should be aware that you're introducing a dependency that the compiler can't check for you - if you misspell the class name or anything, it will fail at runtime.
Also, there's quite a few different types of Exception that might be throws during this process. It's a very powerful technique though.
This should work:
import java.lang.reflect.*;
FirstArgType arg1;
SecondArgType arg2;
Class cl = Class.forName("TheClassName");
Constructor con = cl.getConstructor(FirstArgType.class, SecondArgType.class);
Object obj = con.newInstance(arg1, arg2);
From there you can cast to a known type.
This worked a little more cleanly for me in JDK7, while the answers above made things a bit more difficult than they needed to be from a newbie perspective: (assumes you've declared 'className' as a String variable passed as a method parameter or earlier in the method using this code):
Class<?> panel = Class.forName( className );
JPanel newScreen = (JPanel) panel.newInstance();
From this point you can use properties / methods from your dynamically-named class exactly as you would expect to be able to use them:
JFrame frame = new JFrame(); // <<< Just so no-one gets lost here
frame.getContentPane().removeAll();
frame.getContentPane().add( newScreen );
frame.validate();
frame.repaint();
The examples in other answers above resulted in errors when I tried to .add() the new 'Object' type object to the frame. The technique shown here gave me a usable object with just those 2 lines of code above.
Not exactly certain why that was - I'm a Java newbie myself.
Another one:
import java.lang.reflect.Constructor;
public class Test {
public Test(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return this.name;
}
private String name;
public static void main(String[] args) throws Exception {
String className = "Test";
Class clazz = Class.forName(className);
Constructor tc = clazz.getConstructor(String.class);
Object t = tc.newInstance("John");
System.out.println(t);
}
}
A Sample Program to Get Instance of a class using String Object.
public class GetStudentObjectFromString
{
public static void main (String[] args) throws java.lang.Exception
{
String className = "Student"; //Passed the Class Name in String Object.
//A Object of Student will made by Invoking its Default Constructor.
Object o = Class.forName(className).newInstance();
if(o instanceof Student) // Verify Your Instance that Is it Student Type or not?
System.out.println("Hurrey You Got The Instance of " + className);
}
}
class Student{
public Student(){
System.out.println("Constructor Invoked");
}
}
Output :-
Constructor Invoked
Hurrey You Got The Instance of Student

Categories