You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2020/02/04 16:32:37 UTC
[avro] branch master updated: AVRO-2725: Use one ReflectionUtil for
utilities. (#797)
This is an automated email from the ASF dual-hosted git repository.
rskraba pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 9bc2c62 AVRO-2725: Use one ReflectionUtil for utilities. (#797)
9bc2c62 is described below
commit 9bc2c62c6a82dbce260d4cc0277f1d0276063468
Author: RyanSkraba <ry...@skraba.com>
AuthorDate: Tue Feb 4 17:32:30 2020 +0100
AVRO-2725: Use one ReflectionUtil for utilities. (#797)
* AVRO-2725: Use one ReflectionUtil for utilities.
* Apply spotless.
---
.../java/org/apache/avro/io/FastReaderBuilder.java | 3 +-
.../java/org/apache/avro/io/ReflectionUtils.java | 73 ----------------------
.../org/apache/avro/reflect/ReflectionUtil.java | 50 ++++++++++++++-
3 files changed, 50 insertions(+), 76 deletions(-)
diff --git a/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java b/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
index 40a612c..f6e1ed5 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/io/FastReaderBuilder.java
@@ -49,6 +49,7 @@ import org.apache.avro.generic.GenericFixed;
import org.apache.avro.generic.IndexedRecord;
import org.apache.avro.io.FastReaderBuilder.RecordReader.Stage;
import org.apache.avro.io.parsing.ResolvingGrammarGenerator;
+import org.apache.avro.reflect.ReflectionUtil;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.specific.SpecificRecordBase;
import org.apache.avro.util.Utf8;
@@ -435,7 +436,7 @@ public class FastReaderBuilder {
return stringReader;
} else {
Function<String, ?> transformer = findClass(valueClass)
- .map(clazz -> ReflectionUtils.getConstructorAsFunction(String.class, clazz)).orElse(null);
+ .map(clazz -> ReflectionUtil.getConstructorAsFunction(String.class, clazz)).orElse(null);
if (transformer != null) {
return (old, decoder) -> transformer.apply((String) stringReader.read(null, decoder));
}
diff --git a/lang/java/avro/src/main/java/org/apache/avro/io/ReflectionUtils.java b/lang/java/avro/src/main/java/org/apache/avro/io/ReflectionUtils.java
deleted file mode 100644
index 53faec0..0000000
--- a/lang/java/avro/src/main/java/org/apache/avro/io/ReflectionUtils.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.avro.io;
-
-import java.lang.invoke.CallSite;
-import java.lang.invoke.LambdaMetafactory;
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodType;
-import java.util.function.Function;
-import java.util.function.Supplier;
-
-public class ReflectionUtils {
-
- private ReflectionUtils() {
- // static helper class, don't initiate
- }
-
- public static <D> Supplier<D> getConstructorAsSupplier(Class<D> clazz) {
- try {
- MethodHandles.Lookup lookup = MethodHandles.lookup();
- MethodHandle constructorHandle = lookup.findConstructor(clazz, MethodType.methodType(void.class));
-
- CallSite site = LambdaMetafactory.metafactory(lookup, "get", MethodType.methodType(Supplier.class),
- constructorHandle.type().generic(), constructorHandle, constructorHandle.type());
-
- return (Supplier<D>) site.getTarget().invokeExact();
- } catch (Throwable t) {
- // if anything goes wrong, don't provide a Supplier
- return null;
- }
- }
-
- public static <V, R> Supplier<R> getOneArgConstructorAsSupplier(Class<R> clazz, Class<V> argumentClass, V argument) {
- Function<V, R> supplierFunction = getConstructorAsFunction(argumentClass, clazz);
- if (supplierFunction != null) {
- return () -> supplierFunction.apply(argument);
- } else {
- return null;
- }
- }
-
- public static <V, R> Function<V, R> getConstructorAsFunction(Class<V> parameterClass, Class<R> clazz) {
- try {
- MethodHandles.Lookup lookup = MethodHandles.lookup();
- MethodHandle constructorHandle = lookup.findConstructor(clazz, MethodType.methodType(void.class, parameterClass));
-
- CallSite site = LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(Function.class),
- constructorHandle.type().generic(), constructorHandle, constructorHandle.type());
-
- return (Function<V, R>) site.getTarget().invokeExact();
- } catch (Throwable t) {
- // if something goes wrong, do not provide a Function instance
- return null;
- }
- }
-
-}
diff --git a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectionUtil.java b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectionUtil.java
index b3bace4..18ad475 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectionUtil.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectionUtil.java
@@ -19,11 +19,18 @@ package org.apache.avro.reflect;
import org.apache.avro.AvroRuntimeException;
+import java.lang.invoke.CallSite;
+import java.lang.invoke.LambdaMetafactory;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.IdentityHashMap;
import java.util.Map;
+import java.util.function.Function;
+import java.util.function.Supplier;
/**
* A few utility methods for using @link{java.misc.Unsafe}, mostly for private
@@ -32,9 +39,9 @@ import java.util.Map;
* Use of Unsafe on Android is forbidden, as Android provides only a very
* limited functionality for this class compared to the JDK version.
*
+ * InterfaceAudience.Private
*/
-
-class ReflectionUtil {
+public class ReflectionUtil {
private ReflectionUtil() {
}
@@ -156,4 +163,43 @@ class ReflectionUtil {
return reuse;
}
+ private static <D> Supplier<D> getConstructorAsSupplier(Class<D> clazz) {
+ try {
+ MethodHandles.Lookup lookup = MethodHandles.lookup();
+ MethodHandle constructorHandle = lookup.findConstructor(clazz, MethodType.methodType(void.class));
+
+ CallSite site = LambdaMetafactory.metafactory(lookup, "get", MethodType.methodType(Supplier.class),
+ constructorHandle.type().generic(), constructorHandle, constructorHandle.type());
+
+ return (Supplier<D>) site.getTarget().invokeExact();
+ } catch (Throwable t) {
+ // if anything goes wrong, don't provide a Supplier
+ return null;
+ }
+ }
+
+ private static <V, R> Supplier<R> getOneArgConstructorAsSupplier(Class<R> clazz, Class<V> argumentClass, V argument) {
+ Function<V, R> supplierFunction = getConstructorAsFunction(argumentClass, clazz);
+ if (supplierFunction != null) {
+ return () -> supplierFunction.apply(argument);
+ } else {
+ return null;
+ }
+ }
+
+ public static <V, R> Function<V, R> getConstructorAsFunction(Class<V> parameterClass, Class<R> clazz) {
+ try {
+ MethodHandles.Lookup lookup = MethodHandles.lookup();
+ MethodHandle constructorHandle = lookup.findConstructor(clazz, MethodType.methodType(void.class, parameterClass));
+
+ CallSite site = LambdaMetafactory.metafactory(lookup, "apply", MethodType.methodType(Function.class),
+ constructorHandle.type().generic(), constructorHandle, constructorHandle.type());
+
+ return (Function<V, R>) site.getTarget().invokeExact();
+ } catch (Throwable t) {
+ // if something goes wrong, do not provide a Function instance
+ return null;
+ }
+ }
+
}