An interview question about Java reflection.

huangapple go评论93阅读模式
英文:

An interview question about Java reflection

问题

  1. public class Test {
  2. public static void main(String[] arges) throws Exception {
  3. IA ia = (IA) createObject(IA.class.getName() + "$getName=Abc");
  4. System.out.println(ia.getName()); //output: Abc
  5. ia = (IA) createObject(IA.class.getName() + "$getName=Bcd");
  6. System.out.println(ia.getName()); //output: Bcd
  7. }
  8. public static Object createObject(String str) throws Exception {
  9. String[] parts = str.split("\$");
  10. String className = parts[0];
  11. String[] methodParts = parts[1].split("=");
  12. String methodName = methodParts[0];
  13. String returnValue = methodParts[1];
  14. Class<?> clazz = Class.forName(className);
  15. IA proxy = (IA) java.lang.reflect.Proxy.newProxyInstance(
  16. clazz.getClassLoader(),
  17. new Class[]{IA.class},
  18. (proxyObj, method, args) -> returnValue
  19. );
  20. return proxy;
  21. }
  22. interface IA {
  23. String getName();
  24. }
  25. }
英文:
  1. public class Test {
  2. // 修改方法createObject内容,实现main里面的两处打印
  3. public static void main(String[] arges) throws Exception {
  4. IA ia = (IA) createObject(IA.class.getName() + &quot;$getName =Abc&quot;);
  5. System.out.println(ia.getName()); //output: Abc
  6. ia = (IA) createObject(IA.class.getName() + &quot;$getName= Bcd&quot;);
  7. System.out.println(ia.getName()); //output: Bcd
  8. }
  9. // please coding in createObject for true output
  10. public static Object createObject(String str) throws Exception {
  11. // your coding
  12. }
  13. interface IA {
  14. String getName();
  15. }
  16. }

Through Google, I learned about coding through the use of reflection and dynamic proxies. But when I tried coding, I found that I couldn't do it...

An interview question about Java reflection.

答案1

得分: 0

Sure, here's the translated code:

  1. public class Test {
  2. public static void main(String[] args) throws Exception {
  3. IA ia = (IA) createObject(IA.class.getName() + "$getName =Abc");
  4. System.out.println(ia.getName()); //output: Abc
  5. ia = (IA) createObject(IA.class.getName() + "$getName= Bcd");
  6. System.out.println(ia.getName()); //output: Bcd
  7. }
  8. // please code in createObject for true output
  9. public static Object createObject(String str) throws Exception {
  10. String[] split = str.split("\$getName\\s?=\\s?");
  11. String classname = split[0];
  12. String value = split[1];
  13. return Proxy.newProxyInstance(
  14. Class.forName(classname).getClassLoader(),
  15. new Class[] { IA.class },
  16. (proxy, method, args) -> {
  17. if ("getName".equals(method.getName()))
  18. return value;
  19. throw new Exception();
  20. });
  21. }
  22. interface IA {
  23. String getName();
  24. }
  25. }

Outputs:

  1. Abc
  2. Bcd
英文:

What about this?

  1. public class Test {
  2. public static void main(String[] arges) throws Exception {
  3. IA ia = (IA) createObject(IA.class.getName() + &quot;$getName =Abc&quot;);
  4. System.out.println(ia.getName()); //output: Abc
  5. ia = (IA) createObject(IA.class.getName() + &quot;$getName= Bcd&quot;);
  6. System.out.println(ia.getName()); //output: Bcd
  7. }
  8. // please coding in createObject for true output
  9. public static Object createObject(String str) throws Exception {
  10. String[] split = str.split(&quot;\$getName\\s?=\\s?&quot;);
  11. String classname = split[0];
  12. String value = split[1];
  13. return Proxy.newProxyInstance(Class.forName(classname).getClassLoader(), new Class[] { IA.class },
  14. (proxy, method, args) -&gt; {
  15. if (&quot;getName&quot;.equals(method.getName()))
  16. return value;
  17. throw new Exception();
  18. });
  19. }
  20. interface IA {
  21. String getName();
  22. }
  23. }

Outputs:

  1. Abc
  2. Bcd

答案2

得分: 0

Sure, here's the translated code:

  1. public static Object createObject(String str) throws Exception {
  2. String value = str.split("=")[1].trim();
  3. return (IA) () -> value;
  4. }

Reflection isn't necessary since we're only interested in the part after the equal sign.

英文:

I would do it like this:

  1. public static Object createObject(String str) throws Exception {
  2. String value = str.split(&quot;=&quot;)[1].trim();
  3. return (IA) () -&gt; value;
  4. }

There is not really reflection needed because we only care about the part after the equation mark.

答案3

得分: 0

Here's the translation of the code you provided:

  1. public static Object createObject(String str) throws Exception {
  2. final String className = str.substring(0, str.lastIndexOf("$"));
  3. final String methodName = str.substring(str.lastIndexOf("$") + 1, str.indexOf("=")).trim();
  4. final String value = str.substring(str.indexOf("=") + 1).trim();
  5. return Proxy.newProxyInstance(
  6. Class.forName(className).getClassLoader(),
  7. new Class[] { Class.forName(className) },
  8. (proxy, method, methodArgs) -> {
  9. if (method.getName().equals(methodName)) {
  10. return value;
  11. } else {
  12. throw new UnsupportedOperationException("Unsupported method: " + method.getName());
  13. }
  14. }
  15. );
  16. }
英文:

As the class name and method name are provided in the parameter, I made no assumption on the IA interface. Everything is dynamic.

  1. // please coding in createObject for true output
  2. public static Object createObject(String str) throws Exception {
  3. final String className = str.substring(0, str.lastIndexOf(&quot;$&quot;));
  4. final String methodName=str.substring(str.lastIndexOf(&quot;$&quot;)+1,str.indexOf(&quot;=&quot;)).trim();
  5. final String value = str.substring(str.indexOf(&quot;=&quot;) + 1).trim();
  6. return Proxy.newProxyInstance(Class.forName(className).getClassLoader(), new Class[] { Class.forName(className) },
  7. (proxy, method, methodArgs) -&gt; {
  8. if (method.getName().equals(methodName)) {
  9. return value;
  10. } else {
  11. throw new UnsupportedOperationException(&quot;Unsupported method: &quot; + method.getName());
  12. }
  13. });
  14. }

huangapple
  • 本文由 发表于 2020年8月14日 20:27:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63412760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定