Posted By

lasagna7355608 on 07/16/15


Tagged

Reflection


Versions (?)

Example of reflection in Java using Math Class


 / Published in: Java
 

URL: reflectionmathsexample

A nifty little example of reflection in Java using the Math class.

  1. //Main
  2. import javafx.application.Application;
  3. import javafx.collections.FXCollections;
  4. import javafx.geometry.Insets;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.ComboBox;
  7. import javafx.scene.control.Label;
  8. import javafx.scene.control.TextField;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.layout.VBox;
  11. import javafx.stage.Stage;
  12. import java.lang.reflect.Method;
  13. import java.lang.reflect.Parameter;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16.  
  17. public class Main extends Application{
  18.  
  19. public ArrayList<Method> methodArrayList = new ArrayList<Method>(Arrays.asList(Math.class.getMethods()));
  20. public static ArrayList<Method> validMethodList = new ArrayList<Method>();
  21. public ArrayList<String> methodNameList = new ArrayList<String>();
  22.  
  23. public static void main(String[] args) {
  24. launch(args);
  25. }
  26.  
  27.  
  28. public Scene createScene(){
  29. VBox root = new VBox();
  30. root.setSpacing(10);
  31. root.setPadding(new Insets(10,10,10,10));
  32.  
  33. Label x = new Label("X");
  34.  
  35. TextField value = new TextField();
  36. value.setId("value");
  37.  
  38. for(Method method : methodArrayList)
  39. {
  40. if (method.getParameterCount() == 1 && method.getReturnType().equals(Double.TYPE)){
  41. Parameter parameter = method.getParameters()[0];
  42. if(parameter.getType().equals(Double.TYPE)) {
  43. methodNameList.add(method.getName());
  44. validMethodList.add(method);
  45. }
  46. }
  47. }
  48.  
  49. ComboBox<String> methodComboBox = new ComboBox<String>(FXCollections.observableArrayList(methodNameList));
  50. methodComboBox.setId("method");
  51.  
  52. Button btnCalc = new Button("Calculate");
  53. btnCalc.setId("button");
  54.  
  55. Label lblAnswer = new Label("Answer: ?");
  56. lblAnswer.setId("answer");
  57.  
  58. root.getChildren().addAll(x, value, methodComboBox, btnCalc, lblAnswer);
  59.  
  60. Scene scene = new Scene(root);
  61.  
  62. return scene;
  63.  
  64. }
  65.  
  66. @Override
  67. public void start(Stage primaryStage) throws Exception {
  68. primaryStage.setTitle("Reflection");
  69. Scene scene = createScene();
  70. primaryStage.setScene(scene);
  71. Controller controller = new Controller();
  72. controller.connectSceneUI(scene);
  73. primaryStage.show();
  74. }
  75. }
  76.  
  77. //Controller
  78. import javafx.scene.Scene;
  79. import javafx.scene.control.*;
  80. import java.lang.reflect.InvocationTargetException;
  81. import java.lang.reflect.Method;
  82. import java.util.ArrayList;
  83.  
  84. public class Controller {
  85.  
  86. Button button = new Button();
  87. Label label = new Label();
  88. ComboBox comboBox = new ComboBox();
  89. TextField textField = new TextField();
  90.  
  91. public void connectSceneUI(Scene scene) {
  92. button = (Button) scene.lookup("#button");
  93. label = (Label) scene.lookup("#answer");
  94. comboBox = (ComboBox) scene.lookup("#method");
  95. textField = (TextField) scene.lookup("#value");
  96.  
  97. button.setOnAction((event) -> {
  98. ArrayList<Method> validMethodList = nmmu.wrap301.Main.validMethodList;
  99. Method method = validMethodList.get(comboBox.getSelectionModel().getSelectedIndex());
  100. try {
  101. Double answer = (Double) method.invoke(null,Double.parseDouble(textField.getText()));
  102. label.setText("Answer: " + answer.toString());
  103. } catch (IllegalAccessException e) {
  104. e.printStackTrace();
  105. e.printStackTrace();
  106. }
  107. });
  108. }
  109. }

Report this snippet  

You need to login to post a comment.