You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2022/01/05 13:17:00 UTC

[jira] [Work logged] (AVRO-3164) Allow stringable types deserialization if java-class is specified

     [ https://issues.apache.org/jira/browse/AVRO-3164?focusedWorklogId=703926&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-703926 ]

ASF GitHub Bot logged work on AVRO-3164:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 05/Jan/22 13:16
            Start Date: 05/Jan/22 13:16
    Worklog Time Spent: 10m 
      Work Description: opwvhk commented on a change in pull request #1277:
URL: https://github.com/apache/avro/pull/1277#discussion_r778809555



##########
File path: lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
##########
@@ -874,6 +874,40 @@ public int getNonNullIndex(Schema s) {
     return (s.getTypes().get(0).equals(NULL_SCHEMA) ? 1 : 0);
   }
 
+  /**
+   * Utility for template use. Returns true if specified java-class is stringable
+   * and should be deserialized using constructor with String.
+   */
+  public boolean shouldBeDeserializedFromString(Schema schema) {
+    try {
+      Class<?> clazz = Class.forName(javaType(schema));
+      return specificData.isStringable(clazz);
+    } catch (ClassNotFoundException e) {
+      return false;
+    }
+  }
+
+  /**
+   * Utility for template use. Returns true constructor that accepts string
+   * parameter (e.g. new URL(string)) throws checked exception.
+   */
+  public boolean constructorThrowsCheckedException(Schema schema) {
+    try {
+      Class<?> clazz = Class.forName(javaType(schema));
+      Class<?>[] possibleExceptions = clazz.getDeclaredConstructor(String.class).getExceptionTypes();
+      for (Class<?> exceptionClass : possibleExceptions) {
+        if (!Error.class.isAssignableFrom(exceptionClass) && !RuntimeException.class.isAssignableFrom(exceptionClass)) {
+          return true;
+        }
+      }
+      return false;
+    } catch (ClassNotFoundException e) {
+      throw new RuntimeException("Unable to find class " + javaType(schema), e);

Review comment:
       Instead of a bare `RuntimeException`, is shouldn't we throw `AvroRuntimeException` ?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Issue Time Tracking
-------------------

            Worklog Id:     (was: 703926)
    Remaining Estimate: 0h
            Time Spent: 10m

> Allow stringable types deserialization if java-class is specified
> -----------------------------------------------------------------
>
>                 Key: AVRO-3164
>                 URL: https://issues.apache.org/jira/browse/AVRO-3164
>             Project: Apache Avro
>          Issue Type: New Feature
>          Components: java
>            Reporter: Artur Kalimullin
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> Currently, compiler supports java-class and generates java classes with the given class. It works properly with serialization as toString() is being called, however, on deserialization compiler generates casting instead of calling a constructor with the string argument.
> It works with the ReflectData but not with the SpecificData which throws ClassCastException in runtime.
> To solve this, the same behaviour as ReflectData could be used (calling a constructor with a String argument)
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)