MemberAccess.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package org.ssssssss.script.parsing.ast;
  2. import org.apache.commons.lang3.exception.ExceptionUtils;
  3. import org.ssssssss.script.MagicScriptContext;
  4. import org.ssssssss.script.MagicScriptError;
  5. import org.ssssssss.script.exception.ScriptException;
  6. import org.ssssssss.script.interpreter.AbstractReflection;
  7. import org.ssssssss.script.interpreter.AstInterpreter;
  8. import org.ssssssss.script.parsing.Span;
  9. import java.lang.reflect.Array;
  10. import java.lang.reflect.InvocationTargetException;
  11. import java.util.Arrays;
  12. import java.util.Collections;
  13. import java.util.Map;
  14. public class MemberAccess extends Expression {
  15. private final Expression object;
  16. private final Span name;
  17. private Object cachedMember;
  18. public MemberAccess(Expression object, Span name) {
  19. super(name);
  20. this.object = object;
  21. this.name = name;
  22. }
  23. /**
  24. * Returns the object on which to access the member.
  25. **/
  26. public Expression getObject() {
  27. return object;
  28. }
  29. /**
  30. * The name of the member.
  31. **/
  32. public Span getName() {
  33. return name;
  34. }
  35. /**
  36. * Returns the cached member descriptor as returned by {@link AbstractReflection#getField(Object, String)} or
  37. * {@link AbstractReflection#getMethod(Object, String, Object...)}. See {@link #setCachedMember(Object)}.
  38. **/
  39. public Object getCachedMember() {
  40. return cachedMember;
  41. }
  42. /**
  43. * Sets the member descriptor as returned by {@link AbstractReflection#getField(Object, String)} or
  44. * {@link AbstractReflection#getMethod(Object, String, Object...)} for faster member lookups. Called by {@link AstInterpreter} the
  45. * first time this node is evaluated. Subsequent evaluations can use the cached descriptor, avoiding a costly reflective
  46. * lookup.
  47. **/
  48. public void setCachedMember(Object cachedMember) {
  49. this.cachedMember = cachedMember;
  50. }
  51. @SuppressWarnings("rawtypes")
  52. @Override
  53. public Object evaluate(MagicScriptContext context) {
  54. Object object = getObject().evaluate(context);
  55. if (object == null) {
  56. return null;
  57. }
  58. // special case for array.length
  59. if (object.getClass().isArray() && "length".equals(getName().getText())) {
  60. return Array.getLength(object);
  61. }
  62. // special case for map, allows to do map.key instead of map[key]
  63. if (object instanceof Map) {
  64. Map map = (Map) object;
  65. return map.get(getName().getText());
  66. }
  67. Object field = getCachedMember();
  68. if (field != null) {
  69. try {
  70. return AbstractReflection.getInstance().getFieldValue(object, field);
  71. } catch (Throwable t) {
  72. // fall through
  73. }
  74. }
  75. String text = getName().getText();
  76. field = AbstractReflection.getInstance().getField(object, text);
  77. if (field == null) {
  78. String methodName;
  79. if (text.length() > 1) {
  80. methodName = text.substring(0, 1).toUpperCase() + text.substring(1);
  81. } else {
  82. methodName = text.toUpperCase();
  83. }
  84. MemberAccess access = new MemberAccess(this.object, new Span("get" + methodName));
  85. MethodCall methodCall = new MethodCall(getName(), access, Collections.emptyList());
  86. try {
  87. return methodCall.evaluate(context);
  88. } catch (ScriptException e) {
  89. if (ExceptionUtils.indexOfThrowable(e, InvocationTargetException.class) > -1) {
  90. MagicScriptError.error(String.format("在%s中调用方法get%s发生异常"
  91. , object.getClass()
  92. , methodName), getSpan(), e);
  93. return null;
  94. }
  95. access = new MemberAccess(this.object, new Span("get"));
  96. methodCall = new MethodCall(getName(), access, Arrays.asList(new StringLiteral(getName())));
  97. try {
  98. return methodCall.evaluate(context);
  99. } catch (ScriptException e3) {
  100. if (ExceptionUtils.indexOfThrowable(e3, InvocationTargetException.class) > -1) {
  101. MagicScriptError.error(String.format("在%s中调用方法get发生异常"
  102. , object.getClass()
  103. , methodName), getSpan(), e);
  104. return null;
  105. }
  106. access = new MemberAccess(this.object, new Span("is" + methodName));
  107. methodCall = new MethodCall(getName(), access, Collections.emptyList());
  108. try {
  109. return methodCall.evaluate(context);
  110. } catch (ScriptException e1) {
  111. if (ExceptionUtils.indexOfThrowable(e1, InvocationTargetException.class) > -1) {
  112. MagicScriptError.error(String.format("在%s中调用方法is%s发生异常"
  113. , object.getClass()
  114. , methodName), getSpan(), e);
  115. return null;
  116. }
  117. MagicScriptError.error(String.format("在%s中找不到属性%s或者方法get%s、方法is%s"
  118. , object.getClass()
  119. , getName().getText()
  120. , methodName
  121. , methodName), getSpan());
  122. }
  123. }
  124. }
  125. }
  126. setCachedMember(field);
  127. return AbstractReflection.getInstance().getFieldValue(object, field);
  128. }
  129. }