Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What I'm trying to do: I have a Java program in which I use JavaFX. I created a fxml file in which I created JavaFx controllers, which I then declared in the AddEmployeeOrderController class. I would like to transfer these created controller fields to a POJO. Since I think it is very time-consuming and leads to a lot of careless mistakes, I wanted to automate this process more. My idea was to make an annotation for each declared JavaFX controller field so that, for example, all annotated fields are gonna be retrieved automatically at for example a push of a button in another method. So you can understand it that way instead of writing everything by hand yourself, e.g.:

EmployeeDto employeeDto = new EmployeeDto(textfield.getText(), textfield2.getText()..);

I first formed the AddEmployeeOrderController and declared some JavaFX field and added an annotation. Again in the AddEmployeeOrderController class I tried to access the annotated field.

Then, logically, I would have to cast to cast the java.lang.reflect.Field to a JavaFX TextField, but that is obviously not possible. It throws only IllegalArgumentException errors, and of course because you can't cast a java.lang.reflect.Field to a JavaFX TextField.

Is there a way in which my idea can be achieved with the help of annotation, or am I forced to write everything by hand and generate so-called boilerplate code.

 public class AddEmployeeOrderController implements Initializale {
     @FXML
     @MyAnno
     public TextField orderDateFromTextField;

     public void atPushButton() {
         for (Field field : AddEmployeeOrderController.class.getDeclaredFields()) {
             if (field.isAnnotationPresent(MyAnno.class)) {
                 if (((Field) object).getType().getCanonicalName()
                                               .contains("TextField")) {
                     TextField textField = (TextField) field.get(this);
                     textField.getText();
                 }
             }
         }
     }
 }

 @Retention(RetentionPolicy.RUNTIME)
     public @interface MyAnno {
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
116 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have not provided sufficient (relevant) code to understand what you are actually doing. However, we can deduce the following:

  1. The Field class you are using is java.lang.reflect.Field.

  2. According to the javadoc, the Field.get(Object) should be called with a reference to an instance ... or null. If an instance is provided, then it needs to be an instance the class that declares the field, or a subclass of that class.

  3. If Field.get is called with a parameter that is NOT of the required class, IllegalArgumentException is thrown.

So ... if what you have told us and shown us is correct ... this is not the correct object to be passing to Field.get on that Field object.

You are saying that the field reflection code you are showing us is in the AddEmployeeController class. That means that this would be a AddEmployeeController instance. But the Field instances you are iterating are for the fields declared by the class AddEmployeeOrderController. So, you should be calling get with a (non-null) value that refers to an AddEmployeeOrderController instance. (Or maybe you should be iterating the declared fields of AddEmployeeController. Without more context it is difficult to say what the correct fix is.)


If we strip away all of the dynamic / reflective stuff, what your code is doing is analogous to this non-reflective code:

public class AddEmployeeOrderController {
    public TextField someField; 
}

public class AddEmployeeController {
    public void someMethod() {
        TextField t = (TextField)(this.someField);
    }
}

It won't compile because AddEmployeeController doesn't have a field called someField.

What you actually need to do is the analog of this:

public class AddEmployeeController {
    public void someMethod(AddEmployeeOrderController aeoc) {
        TextField t = (TextField)(aeoc.someField);
    }
}   

Just to clarify, the problem is not coming from the typecast in

(TextField) field.get(this);

If the typecast was failing, then the exception would be a ClassCastException. The exception you are seeing comes from the get call!

And the problem is not due to annotations.


FOLLOW-UP

I have taken your (attempted) MRE, factored out the JavaFX stuff, and turned it into working code. My version shows how to extract a field value reflectively by matching the field's type and an annotation.

package test;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
}

// End of MyAnno

package test;
import java.lang.reflect.Field;

public class MyTest {
    @MyAnno
    public String someField = "Hi Mom";
    
    public void doIt() throws Exception {
        for (Field field : this.getClass().getDeclaredFields()) {
            if (field.isAnnotationPresent(MyAnno.class)) {
                if (field.getType().getCanonicalName().equals("java.lang.String")) {
                    String value = (String) field.get(this);
                    System.out.println(value);
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        new MyTest().doIt();
    }
}

Note that this is REAL code. It compiles, runs and ... works. Try it. If you change String to (say) TextField, you can adapt it to your problem. (The actual type of the field is almost entirely irrelevant to the core problem. As you can see.)

(One thing that could be improved and simplified is that the type test should use Class.equals rather than testing the name of the class. It is cleaner and more reliable.)

One of your concerns was (I think) that you would need a lot of boilerplate code to make this work for your problem. I don't think that is the case:

  1. If I declare an abstract superclass for MyTest and put the implementation of doIt() there ... it should just work. Notice that doIt() uses this.getClass() to get the current object's class.

  2. It would also work if doIt() was in an unrelated class, provided that the method had an argument for passing the target Object.

  3. It would even be possible to parameterize this on the type of the field. Or look for fields that are subtypes of a given type. And so on. (Hint: you would need to pass the type as a Class object.)


I said "(attempted) MRE" for a reason. In fact, your code doesn't compile. Indeed, you have a variable (object) which is not declared, and whose intended type and purpose is hard to fathom. I have assumed that it was a mistake, and guessed that your intention was to use field there. But I should not have to guess!

A real MRE needs to be complete and compilable (unless your problem is how to get it to compile). Ideally it should also be runnable, and running the MRE should reproduce the problem you are asking about.

The point is that we (people trying to help you) need to be sure that we understand what your problem is. That is the purpose of the MRE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...