fork(2) download
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.lang.reflect.Field;
  5.  
  6. class WebElement
  7. {
  8. public WebElement(String id)
  9. {
  10. this.id = id;
  11. }
  12.  
  13. private String id;
  14.  
  15. public String getId()
  16. {
  17. return this.id;
  18. }
  19. }
  20.  
  21. class Program
  22. {
  23. public List<WebElement> elem1 = new ArrayList<WebElement>();
  24. public List<WebElement> elem2 = new ArrayList<WebElement>();
  25.  
  26. public Program()
  27. {
  28. this.elem1.add(new WebElement("id1"));
  29. this.elem2.add(new WebElement("id2"));
  30. }
  31.  
  32. public WebElement getElement(String name) throws Exception
  33. {
  34. Field field = this.getClass().getField(name);
  35.  
  36. List<WebElement> targets = (List<WebElement>)field.get(this);
  37.  
  38. return targets.get(0);
  39. }
  40.  
  41. public static void main (String[] args) throws java.lang.Exception
  42. {
  43. Program prog = new Program();
  44. try{
  45. System.out.println(prog.getElement("elem1").getId());
  46. System.out.println(prog.getElement("elem2").getId());
  47. }catch(Exception e){
  48. System.out.println(e);
  49. }
  50. }
  51. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
id1
id2