GetFieldProc.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package kawa.lang;
  2. import gnu.bytecode.*;
  3. import gnu.mapping.*;
  4. import gnu.expr.*;
  5. // Should be called PrimGetField for consistency.
  6. public class GetFieldProc extends Procedure1 implements Inlineable
  7. {
  8. ClassType ctype;
  9. Field field;
  10. public GetFieldProc (Class clas, String fname)
  11. {
  12. this ((ClassType) Type.make(clas), fname);
  13. }
  14. public GetFieldProc (ClassType ctype, String fname)
  15. {
  16. this.ctype = ctype;
  17. this.field = Field.searchField(ctype.getFields(), fname);
  18. }
  19. public GetFieldProc (ClassType ctype, String name, Type ftype, int flags)
  20. {
  21. this.ctype = ctype;
  22. field = ctype.getField(name);
  23. if (field == null)
  24. field = ctype.addField(name, ftype, flags);
  25. }
  26. public Object apply1 (Object arg1)
  27. {
  28. try
  29. {
  30. java.lang.reflect.Field reflectField = field.getReflectField();
  31. return reflectField.get(arg1);
  32. }
  33. catch (NoSuchFieldException ex)
  34. {
  35. throw new RuntimeException ("no such field " + field.getSourceName()
  36. + " in " + ctype.getName());
  37. }
  38. catch (IllegalAccessException ex)
  39. {
  40. throw new RuntimeException("illegal access for field "
  41. + field.getSourceName());
  42. }
  43. }
  44. private gnu.bytecode.Field getField ()
  45. {
  46. return field;
  47. }
  48. public void compile (ApplyExp exp, Compilation comp, Target target)
  49. {
  50. ClassLoader loader = ctype.getReflectClass().getClassLoader();
  51. if (loader instanceof gnu.bytecode.ArrayClassLoader)
  52. {
  53. ApplyExp.compile(exp, comp, target);
  54. return;
  55. }
  56. exp.getArgs()[0].compile(comp, ctype);
  57. gnu.bytecode.CodeAttr code = comp.getCode();
  58. code.emitGetField(field);
  59. target.compileFromStack(comp, field.getType());
  60. }
  61. public gnu.bytecode.Type getReturnType (Expression[] args)
  62. {
  63. return getField().getType();
  64. }
  65. }