Failed to create proxy class

Discussion in 'Plugin Development' started by RcExtract, Apr 27, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    RcExtract

    Related codes:
    Code:Java
    1. public static void main(String[] args) {
    2. JavaBinder binder = new JavaBaseBinder();
    3. System.out.println(binder.registerBindConfiguration(Person.class));//line 18
    4. System.out.println(((JavaBaseBinder) binder).configurations.size());
    5. for (Entry<Class<?>, Serializable> entry : ((JavaBaseBinder) binder).configurations.entrySet())
    6. System.out.println(entry.getKey().getName() + entry.getValue());
    7. try {
    8. for (Pair<String, Object> pair : ((Properties<?>) binder.deepSerialize(new Person("bob", true, "hi", 13), Properties.class)).entryList())
    9. System.out.println(pair.getKey() + ": " + pair.getValue());
    10. } catch (InvalidInformationException e) {
    11. e.printStackTrace();
    12. }
    13. }

    Code:JAVA
    1. @Override
    2. public boolean registerBindConfiguration(Class<?> clazz, Serializable serializable) {
    3. if (serializable == null || serializable.name() == null || serializable.type() == null || serializable.priority() == null || serializable.priority().length < 1) return false;
    4. if (serializable.provided()) {
    5. List<SerialType> priority = Arrays.asList(serializable.priority());
    6. for (SerialType type : SerialType.values())
    7. if (priority.indexOf(type) != priority.lastIndexOf(type)) return false;
    8. test:
    9. switch (serializable.type()) {
    10. case FIELDS:
    11. if (priority.contains(SerialType.CONVERSION) || priority.contains(SerialType.COMPILATION) || priority.size() != 1) return false;
    12. List<Class<?>> finaltypes = new ArrayList<Class<?>>();
    13. for (Field field : clazz.getFields()) if (Modifier.isFinal(field.getModifiers()) || field.getAnnotation(Constant.class) != null) finaltypes.add(field.getType());
    14. if (finaltypes.isEmpty()) break;
    15. for (Constructor<?> constructor : clazz.getConstructors())
    16. if (constructor.isAccessible() && Utility.containsSequence(Arrays.asList(constructor.getParameterTypes()), finaltypes) && constructor.getExceptionTypes().length == 0) break test;
    17. return false;
    18. case GETTERS_AND_SETTERS:
    19. if (priority.contains(SerialType.CONVERSION) || priority.contains(SerialType.COMPILATION) || priority.size() != 1) return false;
    20. List<Class<?>> finaltypes2 = new ArrayList<Class<?>>();
    21. for (Method method : clazz.getMethods()) {
    22. String name = method.getName();
    23. boolean constant = method.getAnnotation(Constant.class) != null;
    24. if (method.getAnnotation(Transient.class) == null && Modifier.isPublic(method.getModifiers()) && !(Modifier.isStatic(method.getModifiers()))) {
    25. if (name.startsWith("get") && method.getReturnType() != null) {
    26. Method test;
    27. try {
    28. test = clazz.getMethod("set" + name.substring(3), method.getReturnType());
    29. return false;
    30. }
    31. if (!test.isAccessible() || Modifier.isStatic(test.getModifiers())) return false;
    32. if (constant) finaltypes2.add(method.getReturnType());
    33. } else if (name.startsWith("set") && method.getParameterCount() == 1) {
    34. Method test;
    35. try {
    36. test = clazz.getMethod("get" + name.substring(3));
    37. return false;
    38. }
    39. if (!(test.isAccessible() && test.getReturnType() == method.getParameterTypes()[0]) || Modifier.isStatic(test.getModifiers())) return false;
    40. if (constant) finaltypes2.add(method.getParameterTypes()[0]);
    41. }
    42. }
    43. }
    44. if (finaltypes2.isEmpty()) break;
    45. for (Constructor<?> constructor : clazz.getConstructors())
    46. if (constructor.isAccessible() && Utility.containsSequence(Arrays.asList(constructor.getParameterTypes()), finaltypes2) && constructor.getExceptionTypes().length == 0) break test;
    47. return false;
    48. case UTILITY_METHODS:
    49. if (priority.contains(SerialType.CONVERSION)) {
    50. Method method;
    51. try {
    52. method = clazz.getDeclaredMethod("convert");
    53. return false;
    54. }
    55. if (method.getExceptionTypes().length == 0 && method.getReturnType() == null || Modifier.isStatic(method.getModifiers()) || !(Modifier.isPublic(method.getModifiers()))) return false;
    56. Class<?> o = method.getReturnType();
    57. Method smethod;
    58. try {
    59. smethod = clazz.getDeclaredMethod("revert", o);
    60. return false;
    61. }
    62. if (!(smethod.getExceptionTypes().length == 0 && smethod.getReturnType() == clazz && Modifier.isPublic(smethod.getModifiers()) && Modifier.isStatic(smethod.getModifiers()))) return false;
    63. registerInternalUtility(method, smethod, Converter.class);
    64. }
    65. if (priority.contains(SerialType.COMPILATION)) {
    66. Method method;
    67. try {
    68. method = clazz.getDeclaredMethod("compile");
    69. return false;
    70. }
    71. if (!(method.getExceptionTypes().length == 0 && method.getReturnType() == Properties.class && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()))) return false;
    72. Method smethod;
    73. try {
    74. smethod = clazz.getDeclaredMethod("decompile", Properties.class);
    75. return false;
    76. }
    77. if (!(smethod.getExceptionTypes().length == 0 && smethod.getReturnType() == clazz && Modifier.isPublic(smethod.getModifiers()) && Modifier.isStatic(smethod.getModifiers()))) return false;
    78. registerInternalUtility(method, smethod, Compiler.class);//line 338
    79. }
    80. if (priority.contains(SerialType.ENCRYPTION)) {
    81. Method method;
    82. try {
    83. method = clazz.getDeclaredMethod("encrypt");
    84. return false;
    85. }
    86. if (!(method.getExceptionTypes().length == 0 && method.getReturnType() == List.class && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()))) return false;
    87. Method smethod;
    88. try {
    89. smethod = clazz.getDeclaredMethod("decrypt", List.class);
    90. return false;
    91. }
    92. if (!(smethod.getExceptionTypes().length == 0 && smethod.getReturnType() == clazz && Modifier.isPublic(smethod.getModifiers()) && Modifier.isStatic(smethod.getModifiers()))) return false;
    93. registerInternalUtility(method, smethod, Cipher.class);
    94. }
    95. break;
    96. }
    97. }
    98. if (configurations.putIfAbsent(clazz, serializable) != null) return false;
    99. return true;
    100. }
    101.  
    102. @Override
    103. public void registerInternalUtility(Method i, Method s, Class<?> utility) throws IllegalArgumentException {
    104. if (!(Arrays.asList(new Class<?>[] {
    105. Converter.class,
    106. Compiler.class,
    107. Cipher.class
    108. }).contains(utility))) throw new IllegalArgumentException();
    109. /*line 371*/functions.add((Function<?, ?>) Proxy.newProxyInstance(utility.getClassLoader(), new Class<?>[] { Function.class }, new InvocationHandler() {
    110.  
    111. @Override
    112. public Object invoke(Object proxy, Method method, Object[] args) {
    113. System.out.println("here2?");
    114. System.out.println(proxy.getClass().getName());
    115. System.out.println(method.getName());
    116. if (method.getName().equalsIgnoreCase("apply"))
    117. try {
    118. return i.invoke(args[0]);
    119. //These exceptions are never thrown.
    120. throw new UnreachableError();
    121. }
    122. else if (method.getName().equalsIgnoreCase("revert"))
    123. try {
    124. return s.invoke(null, args[0]);
    125. //These exceptions are never thrown.
    126. throw new UnreachableError();
    127. }
    128. throw new UnsupportedOperationException();//line 392
    129. }
    130.  
    131. }));
    132. }
    133.  
    134. public List<Converter<?, ?>> getSerializingConversionPath(Class<?> clazz, Class<?> target) {
    135. System.out.println("here?");
    136. List<Converter<?, ?>> path = new ArrayList<Converter<?, ?>>();
    137. @SuppressWarnings("unchecked")
    138. EnhancedSet<Converter<?, ?>> converters = (EnhancedSet<Converter<?, ?>>) functions.getIf(function -> function instanceof Converter);
    139. for (Converter<?, ?> converter : converters)
    140. if (converter.getInputType() == clazz) {
    141. path.add(converter);
    142. List<Converter<?, ?>> extra = getSerializingConversionPath(converter.getOutputType(), target);
    143. if (extra.get(extra.size() - 1).getOutputType() == target) {
    144. path.addAll(extra);
    145. break;
    146. }
    147. }
    148. return path;
    149. }
    in JavaBaseBinder.
    Code:JAVA
    1. public default boolean registerBindConfiguration(Class<?> clazz) {
    2. return registerBindConfiguration(clazz, clazz.getDeclaredAnnotation(Serializable.class));//line 111
    3. }
    in JavaBinder.
    Stack trace:
    Code:JAVA
    1. Exception in thread "main" java.lang.UnsupportedOperationException
    2. at com.rcextract.commons.io.JavaBaseBinder$1.invoke(JavaBaseBinder.java:392)
    3. at com.sun.proxy.$Proxy2.hashCode(Unknown Source)
    4. at java.util.HashMap.hash(HashMap.java:339)
    5. at java.util.HashMap.put(HashMap.java:612)
    6. at java.util.HashSet.add(HashSet.java:220)
    7. at com.rcextract.commons.io.JavaBaseBinder.registerInternalUtility(JavaBaseBinder.java:371)
    8. at com.rcextract.commons.io.JavaBaseBinder.registerBindConfiguration(JavaBaseBinder.java:338)
    9. at com.rcextract.commons.io.JavaBinder.registerBindConfiguration(JavaBinder.java:111)
    10. at com.rcextract.commons.lang.Test.main(Test.java:18)

    Any help is appreciated (idk what is going on).

    This is a library which i will use it soon on my plugin.
     
  2. Offline

    MightyOne

    I don't even know what a BaseBinder is. I mean surely there is someone who already is familiar with that kind of stuff buuuut since this one person is not here yet :D you could explain (to me) what you are going to achieve with this code

    And comments wouldn't be that bad as well :mad:
     
  3. Offline

    RcExtract

    Thats a temporary name for the class. Also, the library is mine. I was debugging it and came across a problem which is the topic of this thread. (No one will be familiar with my library except myself)
    The library basically aims to serialize and deserialize objects in different ways.
    I know Proxy class is rarely used by others and i expected not much people know about it. Anyway, thx for reading my thread. Help is appreciated.

    EDIT:
    The UnsupportedOperationException was obviously thrown from somewhere u know, which is in the proxy class. It was thrown obviously because the method invoked was neither named "apply" or revert. But when i check the method name invoked, it was called "hashCode", and the proxy object is "Proxy" and with something behind. Also, i realized that much more information is required for u to understand what i am doing in order to help me.

    EDIT2:
    So i found that only final methods will not be handled by the invocationhandler. So for hashCode, toString and equals method, i copied the statments from java.lang.Object to my invocationhandler. That solves the problem, but not a good solution.
     
    Last edited: Apr 27, 2018
Thread Status:
Not open for further replies.

Share This Page