This is probably a very silly way to go about things, but say we had a class with lots of Fields that were components, how would one go about adding them in a for each loop with reflection?
Here is what I've tried so far (though it is obviously doomed to fail):
for(Field bits: this.getClass().getDeclaredFields()){
try {
this.add((Component)Class.forName(bits.getName()).newInstance());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Each of the fields is not a class so doing the above will not work, but I have defined what they are, and they should exist at runtime.
How should I be doing this?
You try to create a class from a field name, so it won't work.
bits.getName() returns something like "myHelloWorldLabel" and not javax.swing.JLabel.
You can either add the value of the field bits.get(this) or create a new object from the class bits.getDeclaringClass().newInstance().
I would also add a check that the class extends JComponent.
Related
So I have class Foo in package com.one.
//package com.one
class Foo{
protected static void a(){
//...
}
}
and class Bar in package com.two extending Foo
//package com.two
class Bar extends Foo{
//...
}
Can I use reflection, if I'm inside com.two, to make a() not protected, and then call it?
Yes you can. Just grab this method and use setAccessible with true.
To grab this method you can't use setDeclaredMethod nor getMethod from subclass, because it wasn't declared there or is not public. Easiest way to get it is to do it via superclass like
Method method = Foo.class.getDeclaredMethod("a");
or
Method method = Bar.class.getSuperclass().getDeclaredMethod("a");
// ^^^^^^^^^^^^^ assuming that `a` is declared in superclass,
// it is possible that you may want to use
// `getSuperclass` few more times
then you can just call
method.setAccessible(true);
to change its accessibility and use it
method.invoke(null);//static methods require no instance.
Yes, you should be able to use reflection for it. Write below code:
try {
Method method = Foo.class.getDeclaredMethod("a");
method.setAccessible(true);
try {
method.invoke(null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am creating java files from json Objects using a library called jsonschema2pojo-core.jar. It successfully creates the required files for me. Now I need to access the newly(dynamically) created file and creates its instance to use it further.
But as the newly created class is still not in the classpath I am unable to do this. Tried to do my part of research and figured out that Eclipse jars allows such refresh only in plugin projects. Can anyone suggest some thing for this?
public static void main(String[] args){
String fileName = "MyJavaFile";
POJOBuilder pojo = new POJOBuilder();
pojo.buildPOJO("file:///C:/mypath/myJSON.json", fileName); //generates the java file MyJavaFile.java
Object obj = null;
try {
obj = Class.forName("com.mypackage."+fileName).newInstance(); // Java file not available yet
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Can this be done through threads? I mean wait until the creation of the POJO is done and then start with the rest after that?
Whenever I run the below code on a Windows 7 machine, I get odd artifacts above one of the buttons.
Typically it's a straight line above whatever button is highlighted by default and as tab over the other buttons I get a straight line above the other buttons as well.
It also looks like I get a line at the very top of the Window. When I've witnessed these same artifacts before in different java UI's I've worked on I've had to do object.setBorder(BorderFactory.createLineBorder(Color.someMatchingColor)). This obviously isn't an option if I'm doing the JOptionPane method.
Is there some known issue with java?
public class TestProj {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showConfirmDialog(null, "Test");
}
}
I am attempting to load the corresponding hashmap using reflection. However I get a field not found exception. Please let me know what you think the issue is. Thanks
//Find the map
HashMap<String, Matches> map = null;
//Reflection to find the appropriate map
try {
Field field = Field.class.getField(mapName); //exception (mapname = lookupHashmap) this class has a lookupHashmap declared)
try {
//Set the map
map = (HashMap<String, Matches>)field.get(this); //Not sure if this is correct
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Stack trace
java.lang.NoSuchFieldException: majorFieldLookup
at java.lang.Class.getField(Class.java:1522)
at MatchingGraph.getResultsForMap(MatchingGraph.java:245)
at MatchingGraph.getmajorFieldMatches(MatchingGraph.java:196)
at Matcher.findMatches(Matcher.java:95)
at Tester.main(Tester.java:27)
You do not want Field.class.getField(mapName);
You want to use whatever class it is you have the map on, call it 'MyClass'
Field field = MyClass.class.getDeclaredField(mapName);
Edit: changed to getDeclaredField(...) from getField(..) because the field was private.
#Rolfl solved your problem i guess.
And I'm suggesting Apache Commons BeanUtils.
And use the method
BeanUtils.copyProperties(source, target);
http://commons.apache.org/proper/commons-beanutils/
Below is the code snippet, I am trying to invoke the usingClass method using REFLECTION. Calling the usingClass() method directly(w/o reflection) works when I pass an object of type Child, though when I try to achieve the same thing using Reflection it throws NoSuchMethodFoundException. Would like to understand if I am missing something or is there any logic behind this? Please help
package Reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestMethodInvocation {
/**
* #param args
*/
public static void main(String[] args) {
TestMethodInvocation test = new TestMethodInvocation();
Child child = new Child();
Parent parent = (Parent)child;
Class<? extends Parent> argClassType = parent.getClass();
Class<? extends TestMethodInvocation> thisClassType = test.getClass();
test.usingClass(child);
Method methodToCall;
try {
methodToCall = thisClassType.getDeclaredMethod("usingClass", argClassType);
methodToCall.invoke(test, parent);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void usingClass(Parent p){
System.out.println("UsingClass: " + p.getClass());
}
}
Output is as below.
UsingClass: class Reflection.Child
java.lang.NoSuchMethodException: Reflection.TestMethodInvocation.usingClass(Reflection.Child)
at java.lang.Class.getDeclaredMethod(Unknown Source)
at Reflection.TestMethodInvocation.main(TestMethodInvocation.java:20)
The reason your code does not work is that getClass() is dynamically bound. Casting to Parent does not affect the runtime type of your object and so the variables child and parent contain the same class object.
Unless you explicitly query your instance for its parent class via getGenericSuperclass() or something similar, you will have to use the static way mentioned by dystroy.
You should use
methodToCall = thisClassType.getDeclaredMethod("usingClass", Parent.class);
because the precise exact class of parent (which is Child), is used at runtime and the type of the variable holding it changes nothing.
Another (too heavy) way to solve it would be :
Class<? extends Parent> argClassType2 = (new Parent()).getClass();
...
methodToCall = thisClassType.getDeclaredMethod("usingClass", argClassType2);