Java Advanced Reflection - java

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();
}

Related

How to check if an object has at least one field that is not null?

I have a set of beans that are generated by a third-party library.
How can I check if each bean has at least one field that is not null?
The problem is easily solved using reflection. Just add this method to your bean:
public boolean hasAtLeastOneNonEmpty() {
Class<? extends QueryBean> class1 = this.getClass();
Field[] fields = class1.getDeclaredFields();
for (Field field : fields) {
try {
if (field.get(this) != null) {
return true;
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}

Adding components via reflection in swing?

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.

Get hashmap using reflection Java, code error

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/

When does the TransportEvent get fired in Java mail?

I am trying to get the Message-Id of the sent message by using listeners.
I am implementing
javax.mail.event.TransportListener with concrete methods given in code sample.
It listens to javax.mail.event.TransportEvent which gets generated when void javax.mail.Transport.sendMessage(.....) is called.
To my surprise I get none of the method gets called when I actually send the mail..??? When does it actually get called ? Do I need to add any wait time after calling sendMessage(..)??
Doesn't it happen in real time ?
#Override
public void messageDelivered(TransportEvent e)
{
try {
System.out.println(e.getMessage().getHeader("Message-Id")[0]);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
#Override
public void messageNotDelivered(TransportEvent e)
{
try {
System.out.println(e.getMessage().getHeader("Message-Id")[0]);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
#Override
public void messagePartiallyDelivered(TransportEvent e)
{
try {
System.out.println(e.getMessage().getHeader("Message-Id")[0]);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Did you register your listener with the Transport instance that's being used to send the message? Remember that the static Transport.send() method creates its own Transport instance that you never see.

In Java Invoking a method using Reflection is not able to find method, when method has baseclass as parameter

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);

Categories

Resources