From c852ef48ffed548f9ac17a42872bf67b326ca218 Mon Sep 17 00:00:00 2001 From: zhouping Date: Thu, 14 Nov 2019 11:15:40 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-1324=20fine-kryo=E6=94=AF=E6=8C=81JDK11?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kryo/util/UnsafeUtil.java | 2 +- .../fr/third/org/objenesis/ObjenesisBase.java | 116 ----------- .../instantiator/util/ClassUtils.java | 78 -------- .../instantiator/util/DefineClassHelper.java | 147 -------------- .../com/fr/third/org/objenesis/Objenesis.java | 4 +- .../fr/third/org/objenesis/ObjenesisBase.java | 102 ++++++++++ .../org/objenesis/ObjenesisException.java | 4 +- .../third/org/objenesis/ObjenesisHelper.java | 6 +- .../org/objenesis/ObjenesisSerializer.java | 13 +- .../fr/third/org/objenesis/ObjenesisStd.java | 15 +- .../instantiator/ObjectInstantiator.java | 4 +- .../SerializationInstantiatorHelper.java | 4 +- .../android/Android10Instantiator.java | 11 +- .../android/Android17Instantiator.java | 22 ++- .../android/Android18Instantiator.java | 24 ++- .../AndroidSerializationInstantiator.java | 25 ++- .../annotations/Instantiator.java | 4 +- .../instantiator/annotations/Typology.java | 4 +- .../basic/AccessibleInstantiator.java | 7 +- .../basic/ConstructorInstantiator.java | 6 +- .../basic/FailingInstantiator.java | 6 +- .../basic/NewInstanceInstantiator.java | 13 +- .../instantiator/basic/NullInstantiator.java | 4 +- .../basic/ObjectInputStreamInstantiator.java | 14 +- .../basic/ObjectStreamClassInstantiator.java | 11 +- .../basic/ProxyingInstantiator.java | 71 ++++--- .../instantiator/gcj/GCJInstantiator.java | 15 +- .../instantiator/gcj/GCJInstantiatorBase.java | 17 +- .../gcj/GCJSerializationInstantiator.java | 9 +- .../instantiator/perc/PercInstantiator.java | 15 +- .../perc/PercSerializationInstantiator.java | 28 ++- .../instantiator/sun/MagicInstantiator.java | 97 +++++---- .../sun/SunReflectionFactoryHelper.java | 25 ++- .../sun/SunReflectionFactoryInstantiator.java | 6 +- ...ctionFactorySerializationInstantiator.java | 6 +- .../sun/UnsafeFactoryInstantiator.java | 8 +- .../util/ClassDefinitionUtils.java | 74 +++++-- .../instantiator/util/DefineClassHelper.java | 149 ++++++++++++++ .../instantiator/util/UnsafeUtils.java | 4 +- .../strategy/BaseInstantiatorStrategy.java | 4 +- .../strategy/InstantiatorStrategy.java | 4 +- .../strategy/PlatformDescription.java | 16 +- .../SerializingInstantiatorStrategy.java | 18 +- .../strategy/SingleInstantiatorStrategy.java | 12 +- .../strategy/StdInstantiatorStrategy.java | 29 +-- .../com/fr/third/sun/misc/BASE64Decoder.java | 96 +++++++++ .../com/fr/third/sun/misc/BASE64Encoder.java | 62 ++++++ .../fr/third/sun/misc/CEFormatException.java | 16 ++ .../fr/third/sun/misc/CEStreamExhausted.java | 15 ++ .../fr/third/sun/misc/CharacterDecoder.java | 107 ++++++++++ .../fr/third/sun/misc/CharacterEncoder.java | 186 ++++++++++++++++++ .../src/com/fr/third/sun/misc/Cleaner.java | 84 ++++++++ 52 files changed, 1236 insertions(+), 583 deletions(-) delete mode 100644 fine-kryo/src/com/fr/third/org/objenesis/ObjenesisBase.java delete mode 100644 fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/ClassUtils.java delete mode 100644 fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/DefineClassHelper.java rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/Objenesis.java (96%) create mode 100644 fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisBase.java rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/ObjenesisException.java (95%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/ObjenesisHelper.java (95%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/ObjenesisSerializer.java (69%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/ObjenesisStd.java (63%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/ObjectInstantiator.java (93%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/SerializationInstantiatorHelper.java (96%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/android/Android10Instantiator.java (91%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/android/Android17Instantiator.java (84%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/android/Android18Instantiator.java (81%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/android/AndroidSerializationInstantiator.java (81%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/annotations/Instantiator.java (94%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/annotations/Typology.java (95%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/basic/AccessibleInstantiator.java (89%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/basic/ConstructorInstantiator.java (96%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/basic/FailingInstantiator.java (95%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/basic/NewInstanceInstantiator.java (83%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/basic/NullInstantiator.java (95%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/basic/ObjectInputStreamInstantiator.java (95%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/basic/ObjectStreamClassInstantiator.java (92%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/basic/ProxyingInstantiator.java (73%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiator.java (79%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiatorBase.java (83%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/gcj/GCJSerializationInstantiator.java (89%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/perc/PercInstantiator.java (90%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/perc/PercSerializationInstantiator.java (82%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/sun/MagicInstantiator.java (74%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryHelper.java (81%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryInstantiator.java (97%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactorySerializationInstantiator.java (97%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/sun/UnsafeFactoryInstantiator.java (96%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/util/ClassDefinitionUtils.java (69%) create mode 100644 fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/DefineClassHelper.java rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/instantiator/util/UnsafeUtils.java (95%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/strategy/BaseInstantiatorStrategy.java (93%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/strategy/InstantiatorStrategy.java (94%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/strategy/PlatformDescription.java (95%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/strategy/SerializingInstantiatorStrategy.java (87%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/strategy/SingleInstantiatorStrategy.java (88%) rename {fine-kryo => fine-objenesis}/src/com/fr/third/org/objenesis/strategy/StdInstantiatorStrategy.java (81%) create mode 100644 fine-third-jdk11/src/com/fr/third/sun/misc/BASE64Decoder.java create mode 100644 fine-third-jdk11/src/com/fr/third/sun/misc/BASE64Encoder.java create mode 100644 fine-third-jdk11/src/com/fr/third/sun/misc/CEFormatException.java create mode 100644 fine-third-jdk11/src/com/fr/third/sun/misc/CEStreamExhausted.java create mode 100644 fine-third-jdk11/src/com/fr/third/sun/misc/CharacterDecoder.java create mode 100644 fine-third-jdk11/src/com/fr/third/sun/misc/CharacterEncoder.java create mode 100644 fine-third-jdk11/src/com/fr/third/sun/misc/Cleaner.java diff --git a/fine-kryo/src/com/fr/third/esotericsoftware/kryo/util/UnsafeUtil.java b/fine-kryo/src/com/fr/third/esotericsoftware/kryo/util/UnsafeUtil.java index 18be3eb24..3014b53ff 100644 --- a/fine-kryo/src/com/fr/third/esotericsoftware/kryo/util/UnsafeUtil.java +++ b/fine-kryo/src/com/fr/third/esotericsoftware/kryo/util/UnsafeUtil.java @@ -27,7 +27,7 @@ import java.util.Comparator; import java.util.List; import com.fr.third.esotericsoftware.minlog.Log; -import sun.misc.Cleaner; +import com.fr.third.sun.misc.Cleaner; import sun.misc.Unsafe; import sun.nio.ch.DirectBuffer; diff --git a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisBase.java b/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisBase.java deleted file mode 100644 index 85f4aee11..000000000 --- a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisBase.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright 2006-2018 the original author or authors. - * - * Licensed 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 com.fr.third.org.objenesis; - -import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; -import com.fr.third.org.objenesis.strategy.InstantiatorStrategy; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Base class to extend if you want to have a class providing your own default strategy. Can also be - * instantiated directly. - * - * @author Henri Tremblay - */ -public class ObjenesisBase implements Objenesis { - - /** - * Strategy used by this Objenesi implementation to create classes - */ - protected final InstantiatorStrategy strategy; - - /** - * Strategy cache. Key = Class, Value = InstantiatorStrategy - */ - protected Map> cache; - - /** - * Constructor allowing to pick a strategy and using cache - * - * @param strategy Strategy to use - */ - public ObjenesisBase(InstantiatorStrategy strategy) { - this(strategy, true); - } - - /** - * Flexible constructor allowing to pick the strategy and if caching should be used - * - * @param strategy Strategy to use - * @param useCache If {@link ObjectInstantiator}s should be cached - */ - public ObjenesisBase(InstantiatorStrategy strategy, boolean useCache) { - if (strategy == null) { - throw new IllegalArgumentException("A strategy can't be null"); - } - this.strategy = strategy; - this.cache = useCache ? new ConcurrentHashMap>() : null; - } - - @Override - public String toString() { - return getClass().getName() + " using " + strategy.getClass().getName() - + (cache == null ? " without" : " with") + " caching"; - } - - /** - * Will create a new object without any constructor being called - * - * @param clazz Class to instantiate - * @return New instance of clazz - */ - public T newInstance(Class clazz) { - return getInstantiatorOf(clazz).newInstance(); - } - - /** - * Will pick the best instantiator for the provided class. If you need to create a lot of - * instances from the same class, it is way more efficient to create them from the same - * ObjectInstantiator than calling {@link #newInstance(Class)}. - * - * @param clazz Class to instantiate - * @return Instantiator dedicated to the class - */ - @SuppressWarnings("unchecked") - public ObjectInstantiator getInstantiatorOf(Class clazz) { - if (clazz.isPrimitive()) { - throw new IllegalArgumentException("Primitive types can't be instantiated in Java"); - } - if (cache == null) { - return strategy.newInstantiatorOf(clazz); - } - ObjectInstantiator instantiator = cache.get(clazz); - if (instantiator == null) { - ObjectInstantiator newInstantiator = strategy.newInstantiatorOf(clazz); - instantiator = putIfAbsent(clazz, newInstantiator); - if (instantiator == null) { - instantiator = newInstantiator; - } - } - return (ObjectInstantiator) instantiator; - } - - private ObjectInstantiator putIfAbsent(Class key, ObjectInstantiator newInstantiator) { - - ObjectInstantiator instantiator = cache.get(key); - if (instantiator == null) { - instantiator = cache.put(key, newInstantiator); - } - return instantiator; - } -} diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/ClassUtils.java b/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/ClassUtils.java deleted file mode 100644 index 56df360b0..000000000 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/ClassUtils.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2006-2018 the original author or authors. - * - * Licensed 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 com.fr.third.org.objenesis.instantiator.util; - -import com.fr.third.org.objenesis.ObjenesisException; - -/** - * Helper class for to play with classes. It contains everything needed to play with a class - * except the dodgy (Java 8) code you will find in {@link ClassDefinitionUtils}. - * - * @author Henri Tremblay - */ -public final class ClassUtils { - - private ClassUtils() { } - - /** - * Will convert a class name to its name in the class definition format (e.g {@code com.fr.third.org.objenesis.EmptyClass} - * becomes {@code org/objenesis/EmptyClass}) - * - * @param className full class name including the package - * @return the internal name - */ - public static String classNameToInternalClassName(String className) { - return className.replace('.', '/'); - } - - /** - * Will convert a class name to its class loader resource name (e.g {@code com.fr.third.org.objenesis.EmptyClass} - * becomes {@code org/objenesis/EmptyClass.class}) - * - * @param className full class name including the package - * @return the resource name - */ - public static String classNameToResource(String className) { - return classNameToInternalClassName(className) + ".class"; - } - - /** - * Check if this class already exists in the class loader and return it if it does - * - * @param type of the class returned - * @param classLoader Class loader where to search the class - * @param className Class name with full path - * @return the class if it already exists or null - */ - @SuppressWarnings("unchecked") - public static Class getExistingClass(ClassLoader classLoader, String className) { - try { - return (Class) Class.forName(className, true, classLoader); - } - catch (ClassNotFoundException e) { - return null; - } - } - - @SuppressWarnings("deprecation") - public static T newInstance(Class clazz) { - try { - return clazz.newInstance(); - } catch (Exception e) { - throw new ObjenesisException(e); - } - } -} diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/DefineClassHelper.java b/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/DefineClassHelper.java deleted file mode 100644 index bc26ed0ec..000000000 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/DefineClassHelper.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright 2006-2018 the original author or authors. - * - * Licensed 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 com.fr.third.org.objenesis.instantiator.util; - -import sun.misc.Unsafe; -import com.fr.third.org.objenesis.ObjenesisException; -import com.fr.third.org.objenesis.strategy.PlatformDescription; - -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodType; -import java.security.ProtectionDomain; - -/** - * Java 11+ removed sun.misc.Unsafe.defineClass. This class bridges the gap to work from Java 1.8 up to 11. - *

- * It was inspired from javassist. - * - * @author Henri Tremblay - */ -public final class DefineClassHelper { - - private static abstract class Helper { - abstract Class defineClass(String name, byte[] b, int off, int len, Class neighbor, - ClassLoader loader, ProtectionDomain protectionDomain); - } - - private static class Java8 extends Helper { - - private final MethodHandle defineClass = defineClass(); - - private MethodHandle defineClass() { - MethodType mt = MethodType.methodType(Class.class, String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class); - MethodHandle m; - try { - m = MethodHandles.publicLookup().findVirtual(Unsafe.class, "defineClass", mt); - } catch(Exception e) { - throw new ObjenesisException(e); - } - Unsafe unsafe = UnsafeUtils.getUnsafe(); - return m.bindTo(unsafe); - } - - @Override - Class defineClass(String className, byte[] b, int off, int len, Class neighbor, ClassLoader loader, ProtectionDomain protectionDomain) { - try { - return (Class) defineClass.invokeExact(className, b, off, len, loader, protectionDomain); - } catch (Throwable e) { - if(e instanceof Error) { - throw (Error) e; - } - if(e instanceof RuntimeException) { - throw (RuntimeException) e; - } - throw new ObjenesisException(e); - } - } - } - - private static class Java11 extends Helper { - - private final Class module = module(); - private final MethodHandles.Lookup lookup = MethodHandles.lookup(); - private final MethodHandle getModule = getModule(); - private final MethodHandle addReads = addReads(); - private final MethodHandle privateLookupIn = privateLookupIn(); - private final MethodHandle defineClass = defineClass(); - - private Class module() { - try { - return Class.forName("java.lang.Module"); - } catch (ClassNotFoundException e) { - throw new ObjenesisException(e); - } - } - - private MethodHandle getModule() { - try { - return lookup.findVirtual(Class.class, "getModule", MethodType.methodType(module)); - } catch (Exception e) { - throw new ObjenesisException(e); - } - } - - private MethodHandle addReads() { - try { - return lookup.findVirtual(module, "addReads", MethodType.methodType(module, module)); - } catch (Exception e) { - throw new ObjenesisException(e); - } - } - - private MethodHandle privateLookupIn() { - try { - return lookup.findStatic(MethodHandles.class, "privateLookupIn", MethodType.methodType(MethodHandles.Lookup.class, Class.class, MethodHandles.Lookup.class)); - } catch (Exception e) { - throw new ObjenesisException(e); - } - } - - private MethodHandle defineClass() { - try { - return lookup.findVirtual(MethodHandles.Lookup.class, "defineClass", MethodType.methodType(Class.class, byte[].class)); - } catch (Exception e) { - throw new ObjenesisException(e); - } - } - - @Override - Class defineClass(String className, byte[] b, int off, int len, Class neighbor, ClassLoader loader, ProtectionDomain protectionDomain) { - try { - Object module = getModule.invokeWithArguments(DefineClassHelper.class); - Object neighborModule = getModule.invokeWithArguments(neighbor); - addReads.invokeWithArguments(module, neighborModule); - MethodHandles.Lookup prvlookup = (MethodHandles.Lookup) privateLookupIn.invokeExact(neighbor, lookup); - return (Class) defineClass.invokeExact(prvlookup, b); - } catch (Throwable e) { - throw new ObjenesisException(neighbor.getName() + " has no permission to define the class", e); - } - } - } - - // Java 11+ removed sun.misc.Unsafe.defineClass, so we fallback to invoking defineClass on - // ClassLoader until we have an implementation that uses MethodHandles.Lookup.defineClass - private static final Helper privileged = PlatformDescription.isAfterJava11() ? - new Java11() : new Java8(); - - public static Class defineClass(String name, byte[] b, int off, int len, Class neighbor, - ClassLoader loader, ProtectionDomain protectionDomain) { - return privileged.defineClass(name, b, off, len, neighbor, loader, protectionDomain); - } - - private DefineClassHelper() {} -} diff --git a/fine-kryo/src/com/fr/third/org/objenesis/Objenesis.java b/fine-objenesis/src/com/fr/third/org/objenesis/Objenesis.java similarity index 96% rename from fine-kryo/src/com/fr/third/org/objenesis/Objenesis.java rename to fine-objenesis/src/com/fr/third/org/objenesis/Objenesis.java index 15e166a1c..7fdc4a503 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/Objenesis.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/Objenesis.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisBase.java b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisBase.java new file mode 100644 index 000000000..2d4b2209f --- /dev/null +++ b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisBase.java @@ -0,0 +1,102 @@ +/** + * Copyright 2006-2017 the original author or authors. + * + * Licensed 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 com.fr.third.org.objenesis; + +import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.strategy.InstantiatorStrategy; + +import java.util.concurrent.ConcurrentHashMap; + +/** + * Base class to extend if you want to have a class providing your own default strategy. Can also be + * instantiated directly. + * + * @author Henri Tremblay + */ +public class ObjenesisBase implements Objenesis { + + /** Strategy used by this Objenesi implementation to create classes */ + protected final InstantiatorStrategy strategy; + + /** Strategy cache. Key = Class, Value = InstantiatorStrategy */ + protected ConcurrentHashMap> cache; + + /** + * Constructor allowing to pick a strategy and using cache + * + * @param strategy Strategy to use + */ + public ObjenesisBase(InstantiatorStrategy strategy) { + this(strategy, true); + } + + /** + * Flexible constructor allowing to pick the strategy and if caching should be used + * + * @param strategy Strategy to use + * @param useCache If {@link ObjectInstantiator}s should be cached + */ + public ObjenesisBase(InstantiatorStrategy strategy, boolean useCache) { + if(strategy == null) { + throw new IllegalArgumentException("A strategy can't be null"); + } + this.strategy = strategy; + this.cache = useCache ? new ConcurrentHashMap>() : null; + } + + @Override + public String toString() { + return getClass().getName() + " using " + strategy.getClass().getName() + + (cache == null ? " without" : " with") + " caching"; + } + + /** + * Will create a new object without any constructor being called + * + * @param clazz Class to instantiate + * @return New instance of clazz + */ + public T newInstance(Class clazz) { + return getInstantiatorOf(clazz).newInstance(); + } + + /** + * Will pick the best instantiator for the provided class. If you need to create a lot of + * instances from the same class, it is way more efficient to create them from the same + * ObjectInstantiator than calling {@link #newInstance(Class)}. + * + * @param clazz Class to instantiate + * @return Instantiator dedicated to the class + */ + @SuppressWarnings("unchecked") + public ObjectInstantiator getInstantiatorOf(Class clazz) { + if(clazz.isPrimitive()) { + throw new IllegalArgumentException("Primitive types can't be instantiated in Java"); + } + if(cache == null) { + return strategy.newInstantiatorOf(clazz); + } + ObjectInstantiator instantiator = cache.get(clazz.getName()); + if(instantiator == null) { + ObjectInstantiator newInstantiator = strategy.newInstantiatorOf(clazz); + instantiator = cache.putIfAbsent(clazz.getName(), newInstantiator); + if(instantiator == null) { + instantiator = newInstantiator; + } + } + return (ObjectInstantiator) instantiator; + } +} diff --git a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisException.java b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisException.java similarity index 95% rename from fine-kryo/src/com/fr/third/org/objenesis/ObjenesisException.java rename to fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisException.java index 678131a2a..84b64fa44 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisException.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisException.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisHelper.java b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisHelper.java similarity index 95% rename from fine-kryo/src/com/fr/third/org/objenesis/ObjenesisHelper.java rename to fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisHelper.java index 764b424ff..1a2b180bf 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisHelper.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisHelper.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,7 +53,7 @@ public final class ObjenesisHelper { * @return New instance of clazz */ public static T newSerializableInstance(Class clazz) { - return OBJENESIS_SERIALIZER.newInstance(clazz); + return (T) OBJENESIS_SERIALIZER.newInstance(clazz); } /** diff --git a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisSerializer.java b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisSerializer.java similarity index 69% rename from fine-kryo/src/com/fr/third/org/objenesis/ObjenesisSerializer.java rename to fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisSerializer.java index 0eadfc057..869af45a2 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisSerializer.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisSerializer.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ package com.fr.third.org.objenesis; +import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.strategy.SerializingInstantiatorStrategy; /** @@ -25,17 +26,17 @@ import com.fr.third.org.objenesis.strategy.SerializingInstantiatorStrategy; public class ObjenesisSerializer extends ObjenesisBase { /** - * Default constructor using the {@link com.fr.third.org.objenesis.strategy.SerializingInstantiatorStrategy} + * Default constructor using the {@link SerializingInstantiatorStrategy} */ public ObjenesisSerializer() { super(new SerializingInstantiatorStrategy()); } /** - * Instance using the {@link com.fr.third.org.objenesis.strategy.SerializingInstantiatorStrategy} with or without caching - * {@link com.fr.third.org.objenesis.instantiator.ObjectInstantiator}s + * Instance using the {@link SerializingInstantiatorStrategy} with or without caching + * {@link ObjectInstantiator}s * - * @param useCache If {@link com.fr.third.org.objenesis.instantiator.ObjectInstantiator}s should be cached + * @param useCache If {@link ObjectInstantiator}s should be cached */ public ObjenesisSerializer(boolean useCache) { super(new SerializingInstantiatorStrategy(), useCache); diff --git a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisStd.java b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisStd.java similarity index 63% rename from fine-kryo/src/com/fr/third/org/objenesis/ObjenesisStd.java rename to fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisStd.java index d8fe2602b..ce3a65bea 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/ObjenesisStd.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/ObjenesisStd.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,27 +15,28 @@ */ package com.fr.third.org.objenesis; +import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.strategy.StdInstantiatorStrategy; /** - * Objenesis implementation using the {@link com.fr.third.org.objenesis.strategy.StdInstantiatorStrategy}. + * Objenesis implementation using the {@link StdInstantiatorStrategy}. * * @author Henri Tremblay */ public class ObjenesisStd extends ObjenesisBase { /** - * Default constructor using the {@link com.fr.third.org.objenesis.strategy.StdInstantiatorStrategy} + * Default constructor using the {@link StdInstantiatorStrategy} */ public ObjenesisStd() { super(new StdInstantiatorStrategy()); } /** - * Instance using the {@link com.fr.third.org.objenesis.strategy.StdInstantiatorStrategy} with or without - * caching {@link com.fr.third.org.objenesis.instantiator.ObjectInstantiator}s + * Instance using the {@link StdInstantiatorStrategy} with or without + * caching {@link ObjectInstantiator}s * - * @param useCache If {@link com.fr.third.org.objenesis.instantiator.ObjectInstantiator}s should be cached + * @param useCache If {@link ObjectInstantiator}s should be cached */ public ObjenesisStd(boolean useCache) { super(new StdInstantiatorStrategy(), useCache); diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/ObjectInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/ObjectInstantiator.java similarity index 93% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/ObjectInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/ObjectInstantiator.java index 4ecf2d9be..6878e49ae 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/ObjectInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/ObjectInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/SerializationInstantiatorHelper.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/SerializationInstantiatorHelper.java similarity index 96% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/SerializationInstantiatorHelper.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/SerializationInstantiatorHelper.java index 17bf3a470..ba4277814 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/SerializationInstantiatorHelper.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/SerializationInstantiatorHelper.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/Android10Instantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/Android10Instantiator.java similarity index 91% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/Android10Instantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/Android10Instantiator.java index 76c3942b0..2b7bc703e 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/Android10Instantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/Android10Instantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,10 @@ package com.fr.third.org.objenesis.instantiator.android; import java.io.ObjectInputStream; import java.lang.reflect.Method; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; +import com.fr.third.org.objenesis.ObjenesisException; /** * Instantiator for Android API level 10 and lover which creates objects without driving their @@ -56,7 +56,10 @@ public class Android10Instantiator implements ObjectInstantiator { newStaticMethod.setAccessible(true); return newStaticMethod; } - catch(Exception e) { + catch(RuntimeException e) { + throw new ObjenesisException(e); + } + catch(NoSuchMethodException e) { throw new ObjenesisException(e); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/Android17Instantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/Android17Instantiator.java similarity index 84% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/Android17Instantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/Android17Instantiator.java index 1986696e7..de14f0319 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/Android17Instantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/Android17Instantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,10 +19,10 @@ import java.io.ObjectStreamClass; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; +import com.fr.third.org.objenesis.ObjenesisException; /** * Instantiator for Android API level 11 to 17 which creates objects without driving their @@ -58,7 +58,10 @@ public class Android17Instantiator implements ObjectInstantiator { newInstanceMethod.setAccessible(true); return newInstanceMethod; } - catch(Exception e) { + catch(RuntimeException e) { + throw new ObjenesisException(e); + } + catch(NoSuchMethodException e) { throw new ObjenesisException(e); } } @@ -71,7 +74,16 @@ public class Android17Instantiator implements ObjectInstantiator { return (Integer) newInstanceMethod.invoke(null, Object.class); } - catch(Exception e) { + catch(RuntimeException e) { + throw new ObjenesisException(e); + } + catch(NoSuchMethodException e) { + throw new ObjenesisException(e); + } + catch(IllegalAccessException e) { + throw new ObjenesisException(e); + } + catch(InvocationTargetException e) { throw new ObjenesisException(e); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/Android18Instantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/Android18Instantiator.java similarity index 81% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/Android18Instantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/Android18Instantiator.java index dc9b23cf0..34bb5aa3d 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/Android18Instantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/Android18Instantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,13 +19,13 @@ import java.io.ObjectStreamClass; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; +import com.fr.third.org.objenesis.ObjenesisException; /** - * Instantiator for Android API level 18 and higher. Same as the version 17 but the + * Instantiator for Android API leve 18 and higher. Same as the version 17 but the * newInstance now takes a long in parameter * * @author Henri Tremblay @@ -58,7 +58,10 @@ public class Android18Instantiator implements ObjectInstantiator { newInstanceMethod.setAccessible(true); return newInstanceMethod; } - catch(Exception e) { + catch(RuntimeException e) { + throw new ObjenesisException(e); + } + catch(NoSuchMethodException e) { throw new ObjenesisException(e); } } @@ -71,7 +74,16 @@ public class Android18Instantiator implements ObjectInstantiator { return (Long) newInstanceMethod.invoke(null, Object.class); } - catch(Exception e) { + catch(RuntimeException e) { + throw new ObjenesisException(e); + } + catch(NoSuchMethodException e) { + throw new ObjenesisException(e); + } + catch(IllegalAccessException e) { + throw new ObjenesisException(e); + } + catch(InvocationTargetException e) { throw new ObjenesisException(e); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/AndroidSerializationInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/AndroidSerializationInstantiator.java similarity index 81% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/AndroidSerializationInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/AndroidSerializationInstantiator.java index 9b2678901..db054207d 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/android/AndroidSerializationInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/android/AndroidSerializationInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,10 +19,10 @@ import java.io.ObjectStreamClass; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; +import com.fr.third.org.objenesis.ObjenesisException; /** * {@link ObjectInstantiator} for Android which creates objects using the constructor from the first @@ -40,7 +40,7 @@ public class AndroidSerializationInstantiator implements ObjectInstantiator type) { this.type = type; newInstanceMethod = getNewInstanceMethod(); - Method m; + Method m = null; try { m = ObjectStreamClass.class.getMethod("lookupAny", Class.class); } catch (NoSuchMethodException e) { @@ -48,7 +48,9 @@ public class AndroidSerializationInstantiator implements ObjectInstantiator implements ObjectInstantiator implements ObjectInstantiator extends ConstructorInstantiator { diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ConstructorInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ConstructorInstantiator.java similarity index 96% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ConstructorInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ConstructorInstantiator.java index 773f769ca..90dc290ee 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ConstructorInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ConstructorInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,10 +17,10 @@ package com.fr.third.org.objenesis.instantiator.basic; import java.lang.reflect.Constructor; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; +import com.fr.third.org.objenesis.ObjenesisException; /** * Instantiates a class by grabbing the no args constructor and calling Constructor.newInstance(). diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/FailingInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/FailingInstantiator.java similarity index 95% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/FailingInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/FailingInstantiator.java index 3796dec05..1802b735b 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/FailingInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/FailingInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,10 +15,10 @@ */ package com.fr.third.org.objenesis.instantiator.basic; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; +import com.fr.third.org.objenesis.ObjenesisException; /** * The instantiator that always throws an exception. Mainly used for tests diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/NewInstanceInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/NewInstanceInstantiator.java similarity index 83% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/NewInstanceInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/NewInstanceInstantiator.java index fc07813e2..93f34cdf2 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/NewInstanceInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/NewInstanceInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ package com.fr.third.org.objenesis.instantiator.basic; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; -import com.fr.third.org.objenesis.instantiator.util.ClassUtils; +import com.fr.third.org.objenesis.ObjenesisException; /** * The simplest instantiator - simply calls Class.newInstance(). This can deal with default public @@ -37,7 +37,12 @@ public class NewInstanceInstantiator implements ObjectInstantiator { } public T newInstance() { - return ClassUtils.newInstance(type); + try { + return type.newInstance(); + } + catch(Exception e) { + throw new ObjenesisException(e); + } } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/NullInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/NullInstantiator.java similarity index 95% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/NullInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/NullInstantiator.java index 59f1318b9..26f7936b8 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/NullInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/NullInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ObjectInputStreamInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ObjectInputStreamInstantiator.java similarity index 95% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ObjectInputStreamInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ObjectInputStreamInstantiator.java index 83431c46e..22d0262d2 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ObjectInputStreamInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ObjectInputStreamInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,10 +25,10 @@ import java.io.ObjectStreamClass; import java.io.ObjectStreamConstants; import java.io.Serializable; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; +import com.fr.third.org.objenesis.ObjenesisException; /** * Instantiates a class by using a dummy input stream that always feeds data for an empty object of @@ -38,7 +38,7 @@ import com.fr.third.org.objenesis.instantiator.annotations.Typology; * completely different class. * * @author Leonardo Mesquita - * @see com.fr.third.org.objenesis.instantiator.ObjectInstantiator + * @see ObjectInstantiator */ @Instantiator(Typology.SERIALIZATION) public class ObjectInputStreamInstantiator implements ObjectInstantiator { @@ -120,7 +120,7 @@ public class ObjectInputStreamInstantiator implements ObjectInstantiator { } @Override - public int read() { + public int read() throws IOException { int result = data[pointer++]; if(pointer >= data.length) { advanceBuffer(); @@ -130,12 +130,12 @@ public class ObjectInputStreamInstantiator implements ObjectInstantiator { } @Override - public int available() { + public int available() throws IOException { return Integer.MAX_VALUE; } @Override - public int read(byte[] b, int off, int len) { + public int read(byte[] b, int off, int len) throws IOException { int left = len; int remaining = data.length - pointer; diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ObjectStreamClassInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ObjectStreamClassInstantiator.java similarity index 92% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ObjectStreamClassInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ObjectStreamClassInstantiator.java index d6099faf8..4e3e7d7e6 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ObjectStreamClassInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ObjectStreamClassInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,10 @@ package com.fr.third.org.objenesis.instantiator.basic; import java.io.ObjectStreamClass; import java.lang.reflect.Method; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; +import com.fr.third.org.objenesis.ObjenesisException; /** * Instantiates a class by using reflection to make a call to private method @@ -44,7 +44,10 @@ public class ObjectStreamClassInstantiator implements ObjectInstantiator { newInstanceMethod = ObjectStreamClass.class.getDeclaredMethod("newInstance"); newInstanceMethod.setAccessible(true); } - catch(Exception e) { + catch(RuntimeException e) { + throw new ObjenesisException(e); + } + catch(NoSuchMethodException e) { throw new ObjenesisException(e); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ProxyingInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ProxyingInstantiator.java similarity index 73% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ProxyingInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ProxyingInstantiator.java index e4354c5b3..490b06aeb 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/basic/ProxyingInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/basic/ProxyingInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,14 +19,11 @@ import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; import com.fr.third.org.objenesis.instantiator.util.ClassDefinitionUtils; -import com.fr.third.org.objenesis.instantiator.util.ClassUtils; - -import static com.fr.third.org.objenesis.instantiator.util.ClassDefinitionUtils.*; +import com.fr.third.org.objenesis.ObjenesisException; /** * This instantiator creates a class by dynamically extending it. It will skip the call to the parent constructor @@ -47,9 +44,9 @@ public class ProxyingInstantiator implements ObjectInstantiator { private static final int INDEX_UTF8_CLASS = 7; private static final int INDEX_UTF8_SUPERCLASS = 8; - private static final int CONSTANT_POOL_COUNT = 9; + private static int CONSTANT_POOL_COUNT = 9; - private static final byte[] CODE = { OPS_aload_0, OPS_return}; + private static final byte[] CODE = { ClassDefinitionUtils.OPS_aload_0, ClassDefinitionUtils.OPS_return}; private static final int CODE_ATTRIBUTE_LENGTH = 12 + CODE.length; private static final String SUFFIX = "$$$Objenesis"; @@ -57,11 +54,11 @@ public class ProxyingInstantiator implements ObjectInstantiator { private static final String CONSTRUCTOR_NAME = ""; private static final String CONSTRUCTOR_DESC = "()V"; - private final Class newType; + private final Class newType; public ProxyingInstantiator(Class type) { - byte[] classBytes = writeExtendingClass(type); + byte[] classBytes = writeExtendingClass(type, SUFFIX); try { newType = ClassDefinitionUtils.defineClass(type.getName() + SUFFIX, classBytes, type, type.getClassLoader()); @@ -70,8 +67,15 @@ public class ProxyingInstantiator implements ObjectInstantiator { } } + @SuppressWarnings("unchecked") public T newInstance() { - return ClassUtils.newInstance(newType); + try { + return (T) newType.newInstance(); + } catch (InstantiationException e) { + throw new ObjenesisException(e); + } catch (IllegalAccessException e) { + throw new ObjenesisException(e); + } } /** @@ -79,58 +83,61 @@ public class ProxyingInstantiator implements ObjectInstantiator { * only have an empty default constructor * * @param type type to extend + * @param suffix the suffix appended to the class name to create the next extending class name * @return the byte for the class * @throws ObjenesisException is something goes wrong */ - private static byte[] writeExtendingClass(Class type) { - String parentClazz = ClassUtils.classNameToInternalClassName(type.getName()); - String clazz = parentClazz + SUFFIX; + private static byte[] writeExtendingClass(Class type, String suffix) { + String parentClazz = ClassDefinitionUtils.classNameToInternalClassName(type.getName()); + String clazz = parentClazz + suffix; + DataOutputStream in = null; ByteArrayOutputStream bIn = new ByteArrayOutputStream(1000); // 1000 should be large enough to fit the entire class - try{ - DataOutputStream in = new DataOutputStream(bIn); - in.write(MAGIC); - in.write(VERSION); + try { + in = new DataOutputStream(bIn); + + in.write(ClassDefinitionUtils.MAGIC); + in.write(ClassDefinitionUtils.VERSION); in.writeShort(CONSTANT_POOL_COUNT); // set all the constant pool here // 1. class - in.writeByte(CONSTANT_Class); + in.writeByte(ClassDefinitionUtils.CONSTANT_Class); in.writeShort(INDEX_UTF8_CLASS); // 2. super class - in.writeByte(CONSTANT_Class); + in.writeByte(ClassDefinitionUtils.CONSTANT_Class); in.writeShort(INDEX_UTF8_SUPERCLASS); // 3. default constructor name - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF(CONSTRUCTOR_NAME); // 4. default constructor description - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF(CONSTRUCTOR_DESC); // 5. Code - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF("Code"); // 6. Class name - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF("L" + clazz + ";"); // 7. Class name (again) - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF(clazz); // 8. Superclass name - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF(parentClazz); // end of constant pool // access flags: We want public, ACC_SUPER is always there - in.writeShort(ACC_PUBLIC | ACC_SUPER); + in.writeShort(ClassDefinitionUtils.ACC_PUBLIC | ClassDefinitionUtils.ACC_SUPER); // this class index in the constant pool in.writeShort(INDEX_CLASS_THIS); @@ -148,7 +155,7 @@ public class ProxyingInstantiator implements ObjectInstantiator { in.writeShort(1); // default constructor method_info - in.writeShort(ACC_PUBLIC); + in.writeShort(ClassDefinitionUtils.ACC_PUBLIC); in.writeShort(INDEX_UTF8_CONSTRUCTOR_NAME); // index of the method name () in.writeShort(INDEX_UTF8_CONSTRUCTOR_DESC); // index of the description in.writeShort(1); // number of attributes: only one, the code @@ -169,6 +176,14 @@ public class ProxyingInstantiator implements ObjectInstantiator { } catch (IOException e) { throw new ObjenesisException(e); + } finally { + if(in != null) { + try { + in.close(); + } catch (IOException e) { + throw new ObjenesisException(e); + } + } } return bIn.toByteArray(); diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiator.java similarity index 79% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiator.java index c232579a8..793d2f21f 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package com.fr.third.org.objenesis.instantiator.gcj; import java.lang.reflect.InvocationTargetException; +import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; @@ -26,7 +27,7 @@ import com.fr.third.org.objenesis.instantiator.annotations.Typology; * work on GCJ JVMs. This instantiator will not call any constructors. * * @author Leonardo Mesquita - * @see com.fr.third.org.objenesis.instantiator.ObjectInstantiator + * @see ObjectInstantiator */ @Instantiator(Typology.STANDARD) public class GCJInstantiator extends GCJInstantiatorBase { @@ -39,7 +40,13 @@ public class GCJInstantiator extends GCJInstantiatorBase { try { return type.cast(newObjectMethod.invoke(dummyStream, type, Object.class)); } - catch(Exception e) { + catch(RuntimeException e) { + throw new ObjenesisException(e); + } + catch(IllegalAccessException e) { + throw new ObjenesisException(e); + } + catch(InvocationTargetException e) { throw new ObjenesisException(e); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiatorBase.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiatorBase.java similarity index 83% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiatorBase.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiatorBase.java index 25ea93f0e..733869919 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiatorBase.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/gcj/GCJInstantiatorBase.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,8 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.lang.reflect.Method; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.ObjenesisException; /** * Base class for GCJ-based instantiators. It initializes reflection access to method @@ -41,11 +41,18 @@ public abstract class GCJInstantiatorBase implements ObjectInstantiator { private static void initialize() { if(newObjectMethod == null) { try { - newObjectMethod = ObjectInputStream.class.getDeclaredMethod("newObject", Class.class, Class.class); + newObjectMethod = ObjectInputStream.class.getDeclaredMethod("newObject", new Class[] { + Class.class, Class.class}); newObjectMethod.setAccessible(true); dummyStream = new DummyStream(); } - catch(Exception e) { + catch(RuntimeException e) { + throw new ObjenesisException(e); + } + catch(NoSuchMethodException e) { + throw new ObjenesisException(e); + } + catch(IOException e) { throw new ObjenesisException(e); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/gcj/GCJSerializationInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/gcj/GCJSerializationInstantiator.java similarity index 89% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/gcj/GCJSerializationInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/gcj/GCJSerializationInstantiator.java index 956c20fa0..5a52513f4 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/gcj/GCJSerializationInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/gcj/GCJSerializationInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ package com.fr.third.org.objenesis.instantiator.gcj; +import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.SerializationInstantiatorHelper; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; @@ -26,11 +27,11 @@ import com.fr.third.org.objenesis.instantiator.annotations.Typology; * calling the first non-serializable superclass' no-arg constructor. * * @author Leonardo Mesquita - * @see com.fr.third.org.objenesis.instantiator.ObjectInstantiator + * @see ObjectInstantiator */ @Instantiator(Typology.SERIALIZATION) public class GCJSerializationInstantiator extends GCJInstantiatorBase { - private final Class superType; + private Class superType; public GCJSerializationInstantiator(Class type) { super(type); diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/perc/PercInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/perc/PercInstantiator.java similarity index 90% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/perc/PercInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/perc/PercInstantiator.java index 875a1768e..0334d9704 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/perc/PercInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/perc/PercInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,8 @@ package com.fr.third.org.objenesis.instantiator.perc; import java.io.ObjectInputStream; import java.lang.reflect.Method; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; @@ -29,7 +29,7 @@ import com.fr.third.org.objenesis.instantiator.annotations.Typology; * Aonix Perc support team. * * @author Henri Tremblay - * @see com.fr.third.org.objenesis.instantiator.ObjectInstantiator + * @see ObjectInstantiator */ @Instantiator(Typology.STANDARD) public class PercInstantiator implements ObjectInstantiator { @@ -47,10 +47,13 @@ public class PercInstantiator implements ObjectInstantiator { Boolean.TYPE); newInstanceMethod.setAccessible(true); } - catch(Exception e) { + catch(RuntimeException e) { throw new ObjenesisException(e); } - } + catch(NoSuchMethodException e) { + throw new ObjenesisException(e); + } + } @SuppressWarnings("unchecked") public T newInstance() { diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/perc/PercSerializationInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/perc/PercSerializationInstantiator.java similarity index 82% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/perc/PercSerializationInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/perc/PercSerializationInstantiator.java index c3a9f4a76..e2be57461 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/perc/PercSerializationInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/perc/PercSerializationInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,8 +20,8 @@ import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; @@ -33,7 +33,7 @@ import com.fr.third.org.objenesis.instantiator.annotations.Typology; * Based on code provided by Aonix but doesn't work right now * * @author Henri Tremblay - * @see com.fr.third.org.objenesis.instantiator.ObjectInstantiator + * @see ObjectInstantiator */ @Instantiator(Typology.SERIALIZATION) public class PercSerializationInstantiator implements ObjectInstantiator { @@ -56,7 +56,7 @@ public class PercSerializationInstantiator implements ObjectInstantiator { Class percMethodClass = Class.forName("COM.newmonics.PercClassLoader.Method"); newInstanceMethod = ObjectInputStream.class.getDeclaredMethod("noArgConstruct", - Class.class, Object.class, percMethodClass); + new Class[] {Class.class, Object.class, percMethodClass}); newInstanceMethod.setAccessible(true); // Create invoke params @@ -64,13 +64,22 @@ public class PercSerializationInstantiator implements ObjectInstantiator { Method getPercClassMethod = percClassClass.getDeclaredMethod("getPercClass", Class.class); Object someObject = getPercClassMethod.invoke(null, unserializableType); Method findMethodMethod = someObject.getClass().getDeclaredMethod("findMethod", - String.class); + new Class[] {String.class}); Object percMethod = findMethodMethod.invoke(someObject, "()V"); typeArgs = new Object[] {unserializableType, type, percMethod}; } - catch(Exception e) { + catch(ClassNotFoundException e) { + throw new ObjenesisException(e); + } + catch(NoSuchMethodException e) { + throw new ObjenesisException(e); + } + catch(InvocationTargetException e) { + throw new ObjenesisException(e); + } + catch(IllegalAccessException e) { throw new ObjenesisException(e); } } @@ -80,7 +89,10 @@ public class PercSerializationInstantiator implements ObjectInstantiator { try { return (T) newInstanceMethod.invoke(null, typeArgs); } - catch(Exception e) { + catch(IllegalAccessException e) { + throw new ObjenesisException(e); + } + catch(InvocationTargetException e) { throw new ObjenesisException(e); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/MagicInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/MagicInstantiator.java similarity index 74% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/MagicInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/MagicInstantiator.java index c865333a6..531fbc840 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/MagicInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/MagicInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,14 +19,11 @@ import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.instantiator.util.ClassDefinitionUtils; +import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; -import com.fr.third.org.objenesis.instantiator.util.ClassDefinitionUtils; -import com.fr.third.org.objenesis.instantiator.util.ClassUtils; - -import static com.fr.third.org.objenesis.instantiator.util.ClassDefinitionUtils.*; /** * This instantiator will correctly bypass the constructors by instantiating the class using the default @@ -58,18 +55,18 @@ public class MagicInstantiator implements ObjectInstantiator { private static final int INDEX_CLASS_TYPE = 17; private static final int INDEX_UTF8_TYPE = 18; - private static final int CONSTANT_POOL_COUNT = 19; + private static int CONSTANT_POOL_COUNT = 19; - private static final byte[] CONSTRUCTOR_CODE = { OPS_aload_0, OPS_invokespecial, 0, INDEX_METHODREF_OBJECT_CONSTRUCTOR, OPS_return}; + private static final byte[] CONSTRUCTOR_CODE = { ClassDefinitionUtils.OPS_aload_0, ClassDefinitionUtils.OPS_invokespecial, 0, INDEX_METHODREF_OBJECT_CONSTRUCTOR, ClassDefinitionUtils.OPS_return}; private static final int CONSTRUCTOR_CODE_ATTRIBUTE_LENGTH = 12 + CONSTRUCTOR_CODE.length; - private static final byte[] NEWINSTANCE_CODE = { OPS_new, 0, INDEX_CLASS_TYPE, OPS_dup, OPS_invokespecial, 0, INDEX_METHODREF_OBJECT_CONSTRUCTOR, OPS_areturn}; + private static final byte[] NEWINSTANCE_CODE = { ClassDefinitionUtils.OPS_new, 0, INDEX_CLASS_TYPE, ClassDefinitionUtils.OPS_dup, ClassDefinitionUtils.OPS_invokespecial, 0, INDEX_METHODREF_OBJECT_CONSTRUCTOR, ClassDefinitionUtils.OPS_areturn}; private static final int NEWINSTANCE_CODE_ATTRIBUTE_LENGTH = 12 + NEWINSTANCE_CODE.length; private static final String CONSTRUCTOR_NAME = ""; private static final String CONSTRUCTOR_DESC = "()V"; - private final ObjectInstantiator instantiator; + private ObjectInstantiator instantiator; public MagicInstantiator(Class type) { instantiator = newInstantiatorOf(type); @@ -88,11 +85,11 @@ public class MagicInstantiator implements ObjectInstantiator { return instantiator; } - private ObjectInstantiator newInstantiatorOf(Class type) { + private ObjectInstantiator newInstantiatorOf(Class type) { String suffix = type.getSimpleName(); String className = getClass().getName() + "$$$" + suffix; - Class> clazz = ClassUtils.getExistingClass(getClass().getClassLoader(), className); + Class> clazz = ClassDefinitionUtils.getExistingClass(getClass().getClassLoader(), className); if(clazz == null) { byte[] classBytes = writeExtendingClass(type, className); @@ -104,7 +101,13 @@ public class MagicInstantiator implements ObjectInstantiator { } } - return ClassUtils.newInstance(clazz); + try { + return clazz.newInstance(); + } catch (InstantiationException e) { + throw new ObjenesisException(e); + } catch (IllegalAccessException e) { + throw new ObjenesisException(e); + } } /** @@ -117,96 +120,98 @@ public class MagicInstantiator implements ObjectInstantiator { * @throws ObjenesisException is something goes wrong */ private byte[] writeExtendingClass(Class type, String className) { - String clazz = ClassUtils.classNameToInternalClassName(className); + String clazz = ClassDefinitionUtils.classNameToInternalClassName(className); - ByteArrayOutputStream bIn = new ByteArrayOutputStream(1000); // 1000 should be large enough to fit the entire class - try{ - DataOutputStream in = new DataOutputStream(bIn); - in.write(MAGIC); - in.write(VERSION); + DataOutputStream in = null; + ByteArrayOutputStream bIn = new ByteArrayOutputStream(1000); // 1000 should be large enough to fit the entire class + try { + in = new DataOutputStream(bIn); + + in.write(ClassDefinitionUtils.MAGIC); + in.write(ClassDefinitionUtils.VERSION); in.writeShort(CONSTANT_POOL_COUNT); // set all the constant pool here // 1. class - in.writeByte(CONSTANT_Class); + in.writeByte(ClassDefinitionUtils.CONSTANT_Class); in.writeShort(INDEX_UTF8_INSTANTIATOR_CLASS); // 2. super class - in.writeByte(CONSTANT_Class); + in.writeByte(ClassDefinitionUtils.CONSTANT_Class); in.writeShort(INDEX_UTF8_SUPERCLASS); // 3. default constructor name - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF(CONSTRUCTOR_NAME); // 4. default constructor description - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF(CONSTRUCTOR_DESC); // 5. Code - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF("Code"); // 6. Class name - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF("L" + clazz + ";"); // 7. Class name (again) - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF(clazz); // 8. Superclass name - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); // in.writeUTF("java/lang/Object"); in.writeUTF(MAGIC_ACCESSOR); // 9. ObjectInstantiator interface - in.writeByte(CONSTANT_Class); + in.writeByte(ClassDefinitionUtils.CONSTANT_Class); in.writeShort(INDEX_UTF8_INTERFACE); // 10. ObjectInstantiator name - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF(ObjectInstantiator.class.getName().replace('.', '/')); // 11. newInstance name - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF("newInstance"); // 12. newInstance desc - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF("()Ljava/lang/Object;"); // 13. Methodref to the Object constructor - in.writeByte(CONSTANT_Methodref); + in.writeByte(ClassDefinitionUtils.CONSTANT_Methodref); in.writeShort(INDEX_CLASS_OBJECT); in.writeShort(INDEX_NAMEANDTYPE_DEFAULT_CONSTRUCTOR); // 14. Object class - in.writeByte(CONSTANT_Class); + in.writeByte(ClassDefinitionUtils.CONSTANT_Class); in.writeShort(INDEX_UTF8_OBJECT); // 15. Object class name - in.writeByte(CONSTANT_Utf8); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); in.writeUTF("java/lang/Object"); // 16. Default constructor name and type - in.writeByte(CONSTANT_NameAndType); + in.writeByte(ClassDefinitionUtils.CONSTANT_NameAndType); in.writeShort(INDEX_UTF8_CONSTRUCTOR_NAME); in.writeShort(INDEX_UTF8_CONSTRUCTOR_DESC); // 17. Type to instantiate class - in.writeByte(CONSTANT_Class); + in.writeByte(ClassDefinitionUtils.CONSTANT_Class); in.writeShort(INDEX_UTF8_TYPE); // 18. Type to instantiate name - in.writeByte(CONSTANT_Utf8); - in.writeUTF(ClassUtils.classNameToInternalClassName(type.getName())); + in.writeByte(ClassDefinitionUtils.CONSTANT_Utf8); + in.writeUTF(ClassDefinitionUtils.classNameToInternalClassName(type.getName())); // end of constant pool // access flags: We want public, ACC_SUPER is always there - in.writeShort(ACC_PUBLIC | ACC_SUPER | ACC_FINAL); + in.writeShort(ClassDefinitionUtils.ACC_PUBLIC | ClassDefinitionUtils.ACC_SUPER | ClassDefinitionUtils.ACC_FINAL); // this class index in the constant pool in.writeShort(INDEX_CLASS_THIS); @@ -225,7 +230,7 @@ public class MagicInstantiator implements ObjectInstantiator { in.writeShort(2); // default constructor method_info - in.writeShort(ACC_PUBLIC); + in.writeShort(ClassDefinitionUtils.ACC_PUBLIC); in.writeShort(INDEX_UTF8_CONSTRUCTOR_NAME); // index of the method name () in.writeShort(INDEX_UTF8_CONSTRUCTOR_DESC); // index of the description in.writeShort(1); // number of attributes: only one, the code @@ -241,7 +246,7 @@ public class MagicInstantiator implements ObjectInstantiator { in.writeShort(0); // attributes count = 0, no need to have LineNumberTable and LocalVariableTable // newInstance method_info - in.writeShort(ACC_PUBLIC); + in.writeShort(ClassDefinitionUtils.ACC_PUBLIC); in.writeShort(INDEX_UTF8_NEWINSTANCE_NAME); // index of the method name (newInstance) in.writeShort(INDEX_UTF8_NEWINSTANCE_DESC); // index of the description in.writeShort(1); // number of attributes: only one, the code @@ -261,6 +266,14 @@ public class MagicInstantiator implements ObjectInstantiator { } catch (IOException e) { throw new ObjenesisException(e); + } finally { + if(in != null) { + try { + in.close(); + } catch (IOException e) { + throw new ObjenesisException(e); + } + } } return bIn.toByteArray(); diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryHelper.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryHelper.java similarity index 81% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryHelper.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryHelper.java index b40215c83..40e9df661 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryHelper.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryHelper.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,8 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.ObjenesisException; /** @@ -44,7 +44,13 @@ class SunReflectionFactoryHelper { return (Constructor) newConstructorForSerializationMethod.invoke( reflectionFactory, type, constructor); } - catch(Exception e) { + catch(IllegalArgumentException e) { + throw new ObjenesisException(e); + } + catch(IllegalAccessException e) { + throw new ObjenesisException(e); + } + catch(InvocationTargetException e) { throw new ObjenesisException(e); } } @@ -64,7 +70,16 @@ class SunReflectionFactoryHelper { "getReflectionFactory"); return method.invoke(null); } - catch(Exception e) { + catch(NoSuchMethodException e) { + throw new ObjenesisException(e); + } + catch(IllegalAccessException e) { + throw new ObjenesisException(e); + } + catch(IllegalArgumentException e) { + throw new ObjenesisException(e); + } + catch(InvocationTargetException e) { throw new ObjenesisException(e); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryInstantiator.java similarity index 97% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryInstantiator.java index 0ea6d3300..a732fe0ff 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactoryInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,8 @@ package com.fr.third.org.objenesis.instantiator.sun; import java.lang.reflect.Constructor; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactorySerializationInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactorySerializationInstantiator.java similarity index 97% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactorySerializationInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactorySerializationInstantiator.java index fdfd120cf..ef7574674 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactorySerializationInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/SunReflectionFactorySerializationInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,8 @@ package com.fr.third.org.objenesis.instantiator.sun; import java.io.NotSerializableException; import java.lang.reflect.Constructor; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.SerializationInstantiatorHelper; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/UnsafeFactoryInstantiator.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/UnsafeFactoryInstantiator.java similarity index 96% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/UnsafeFactoryInstantiator.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/UnsafeFactoryInstantiator.java index d351bdaf6..709276a29 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/sun/UnsafeFactoryInstantiator.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/sun/UnsafeFactoryInstantiator.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,12 @@ */ package com.fr.third.org.objenesis.instantiator.sun; +import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.instantiator.util.UnsafeUtils; import sun.misc.Unsafe; import com.fr.third.org.objenesis.ObjenesisException; -import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; import com.fr.third.org.objenesis.instantiator.annotations.Instantiator; import com.fr.third.org.objenesis.instantiator.annotations.Typology; -import com.fr.third.org.objenesis.instantiator.util.UnsafeUtils; /** * Instantiates an object, WITHOUT calling it's constructor, using diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/ClassDefinitionUtils.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/ClassDefinitionUtils.java similarity index 69% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/ClassDefinitionUtils.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/ClassDefinitionUtils.java index d44afcdb4..fca50d9c1 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/ClassDefinitionUtils.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/ClassDefinitionUtils.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,11 +67,15 @@ public final class ClassDefinitionUtils { private ClassDefinitionUtils() { } +// private static Method DEFINE_CLASS; private static final ProtectionDomain PROTECTION_DOMAIN; static { - - PROTECTION_DOMAIN = AccessController.doPrivileged((PrivilegedAction) ClassDefinitionUtils.class.getProtectionDomain()); + PROTECTION_DOMAIN = AccessController.doPrivileged(new PrivilegedAction() { + public ProtectionDomain run() { + return ClassDefinitionUtils.class.getProtectionDomain(); + } + }); } /** @@ -79,9 +83,8 @@ public final class ClassDefinitionUtils { * ReflectUtils.defineClass * * @param type of the class returned - * @param className class name in the format com.fr.third.org.objenesis.MyClass + * @param className class name in the format org.objenesis.MyClass * @param b bytes representing the class - * @param neighbor a class in the same package as the loaded class * @param loader the class loader where the class will be loaded * @return the newly loaded class * @throws Exception whenever something goes wrong @@ -89,7 +92,7 @@ public final class ClassDefinitionUtils { @SuppressWarnings("unchecked") public static Class defineClass(String className, byte[] b, Class neighbor, ClassLoader loader) throws Exception { - Class c = (Class) DefineClassHelper.defineClass(className, b, 0, b.length, neighbor, loader, PROTECTION_DOMAIN); + Class c = (Class) DefineClassHelper.defineClass(className, b, 0, b.length, neighbor, loader, PROTECTION_DOMAIN); // Force static initializers to run. Class.forName(className, true, loader); return c; @@ -105,17 +108,18 @@ public final class ClassDefinitionUtils { */ public static byte[] readClass(String className) throws IOException { // convert to a resource - className = ClassUtils.classNameToResource(className); + className = classNameToResource(className); byte[] b = new byte[2500]; // I'm assuming that I'm reading class that are not too big - int length=0; + int length; + InputStream in = ClassDefinitionUtils.class.getClassLoader().getResourceAsStream(className); try { - InputStream in = ClassDefinitionUtils.class.getClassLoader().getResourceAsStream(className); length = in.read(b); - } catch (Exception e) { - + } + finally { + in.close(); } if(length >= 2500) { @@ -135,12 +139,52 @@ public final class ClassDefinitionUtils { * @throws IOException if we fail to write the class */ public static void writeClass(String fileName, byte[] bytes) throws IOException { - + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName)); try { - BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName)); out.write(bytes); - } catch (Throwable e) { + } + finally { + out.close(); + } + } + + /** + * Will convert a class name to its name in the class definition format (e.g {@code org.objenesis.EmptyClass} + * becomes {@code org/objenesis/EmptyClass}) + * + * @param className full class name including the package + * @return the internal name + */ + public static String classNameToInternalClassName(String className) { + return className.replace('.', '/'); + } + /** + * Will convert a class name to its class loader resource name (e.g {@code org.objenesis.EmptyClass} + * becomes {@code org/objenesis/EmptyClass.class}) + * + * @param className full class name including the package + * @return the resource name + */ + public static String classNameToResource(String className) { + return classNameToInternalClassName(className) + ".class"; + } + + /** + * Check if this class already exists in the class loader and return it if it does + * + * @param type of the class returned + * @param classLoader Class loader where to search the class + * @param className Class name with full path + * @return the class if it already exists or null + */ + @SuppressWarnings("unchecked") + public static Class getExistingClass(ClassLoader classLoader, String className) { + try { + return (Class) Class.forName(className, true, classLoader); + } + catch (ClassNotFoundException e) { + return null; } } } diff --git a/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/DefineClassHelper.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/DefineClassHelper.java new file mode 100644 index 000000000..6c2a4ebf7 --- /dev/null +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/DefineClassHelper.java @@ -0,0 +1,149 @@ +/* + * Copyright 2006-2019 the original author or authors. + * + * Licensed 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 com.fr.third.org.objenesis.instantiator.util; + +import com.fr.third.org.objenesis.ObjenesisException; +import com.fr.third.org.objenesis.strategy.PlatformDescription; +import sun.misc.Unsafe; + + + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.security.ProtectionDomain; + +/** + * Java 11+ removed sun.misc.Unsafe.defineClass. This class bridges the gap to work from Java 1.8 up to 11. + *

+ * It was inspired from javassist. + * + * @author Henri Tremblay + */ +public final class DefineClassHelper { + + private static abstract class Helper { + abstract Class defineClass(String name, byte[] b, int off, int len, Class neighbor, + ClassLoader loader, ProtectionDomain protectionDomain); + } + + private static class Java8 extends Helper { + + private final MethodHandle defineClass = defineClass(); + + private MethodHandle defineClass() { + MethodType mt = MethodType.methodType(Class.class, String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class); + MethodHandle m; + try { + m = MethodHandles.publicLookup().findVirtual(Unsafe.class, "defineClass", mt); + } catch(NoSuchMethodException | IllegalAccessException e) { + throw new ObjenesisException(e); + } + Unsafe unsafe = UnsafeUtils.getUnsafe(); + return m.bindTo(unsafe); + } + + @Override + Class defineClass(String className, byte[] b, int off, int len, Class neighbor, ClassLoader loader, ProtectionDomain protectionDomain) { + try { + return (Class) defineClass.invokeExact(className, b, off, len, loader, protectionDomain); + } catch (Throwable e) { + if(e instanceof Error) { + throw (Error) e; + } + if(e instanceof RuntimeException) { + throw (RuntimeException) e; + } + throw new ObjenesisException(e); + } + } + } + + private static class Java11 extends Helper { + + private final Class module = module(); + private final MethodHandles.Lookup lookup = MethodHandles.lookup(); + private final MethodHandle getModule = getModule(); + private final MethodHandle addReads = addReads(); + private final MethodHandle privateLookupIn = privateLookupIn(); + private final MethodHandle defineClass = defineClass(); + + private Class module() { + try { + return Class.forName("java.lang.Module"); + } catch (ClassNotFoundException e) { + throw new ObjenesisException(e); + } + } + + private MethodHandle getModule() { + try { + return lookup.findVirtual(Class.class, "getModule", MethodType.methodType(module)); + } catch (NoSuchMethodException | IllegalAccessException e) { + throw new ObjenesisException(e); + } + } + + private MethodHandle addReads() { + try { + return lookup.findVirtual(module, "addReads", MethodType.methodType(module, module)); + } catch (NoSuchMethodException | IllegalAccessException e) { + throw new ObjenesisException(e); + } + } + + private MethodHandle privateLookupIn() { + try { + return lookup.findStatic(MethodHandles.class, "privateLookupIn", MethodType.methodType(MethodHandles.Lookup.class, Class.class, MethodHandles.Lookup.class)); + } catch (NoSuchMethodException | IllegalAccessException e) { + throw new ObjenesisException(e); + } + } + + private MethodHandle defineClass() { + try { + return lookup.findVirtual(MethodHandles.Lookup.class, "defineClass", MethodType.methodType(Class.class, byte[].class)); + } catch (NoSuchMethodException | IllegalAccessException e) { + throw new ObjenesisException(e); + } + } + + @Override + Class defineClass(String className, byte[] b, int off, int len, Class neighbor, ClassLoader loader, ProtectionDomain protectionDomain) { + try { + Object module = getModule.invokeWithArguments(DefineClassHelper.class); + Object neighborModule = getModule.invokeWithArguments(neighbor); + addReads.invokeWithArguments(module, neighborModule); + MethodHandles.Lookup prvlookup = (MethodHandles.Lookup) privateLookupIn.invokeExact(neighbor, lookup); + return (Class) defineClass.invokeExact(prvlookup, b); + } catch (Throwable e) { + throw new ObjenesisException(neighbor.getName() + " has no permission to define the class", e); + } + } + } + + // Java 11+ removed sun.misc.Unsafe.defineClass, so we fallback to invoking defineClass on + // ClassLoader until we have an implementation that uses MethodHandles.Lookup.defineClass + private static final Helper privileged = PlatformDescription.isAfterJava11() ? + new Java11() : new Java8(); + + public static Class defineClass(String name, byte[] b, int off, int len, Class neighbor, + ClassLoader loader, ProtectionDomain protectionDomain) { + return privileged.defineClass(name, b, off, len, neighbor, loader, protectionDomain); + } + + private DefineClassHelper() {} +} \ No newline at end of file diff --git a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/UnsafeUtils.java b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/UnsafeUtils.java similarity index 95% rename from fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/UnsafeUtils.java rename to fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/UnsafeUtils.java index b793af373..150c87627 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/instantiator/util/UnsafeUtils.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/instantiator/util/UnsafeUtils.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/fine-kryo/src/com/fr/third/org/objenesis/strategy/BaseInstantiatorStrategy.java b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/BaseInstantiatorStrategy.java similarity index 93% rename from fine-kryo/src/com/fr/third/org/objenesis/strategy/BaseInstantiatorStrategy.java rename to fine-objenesis/src/com/fr/third/org/objenesis/strategy/BaseInstantiatorStrategy.java index 31ef3809e..20f445d9e 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/strategy/BaseInstantiatorStrategy.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/BaseInstantiatorStrategy.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/fine-kryo/src/com/fr/third/org/objenesis/strategy/InstantiatorStrategy.java b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/InstantiatorStrategy.java similarity index 94% rename from fine-kryo/src/com/fr/third/org/objenesis/strategy/InstantiatorStrategy.java rename to fine-objenesis/src/com/fr/third/org/objenesis/strategy/InstantiatorStrategy.java index 95e6fce57..3a7e697f1 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/strategy/InstantiatorStrategy.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/InstantiatorStrategy.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/fine-kryo/src/com/fr/third/org/objenesis/strategy/PlatformDescription.java b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/PlatformDescription.java similarity index 95% rename from fine-kryo/src/com/fr/third/org/objenesis/strategy/PlatformDescription.java rename to fine-objenesis/src/com/fr/third/org/objenesis/strategy/PlatformDescription.java index 1706834cc..b5c886e27 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/strategy/PlatformDescription.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/PlatformDescription.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,10 +15,10 @@ */ package com.fr.third.org.objenesis.strategy; -import java.lang.reflect.Field; - import com.fr.third.org.objenesis.ObjenesisException; +import java.lang.reflect.Field; + /** * List of constants describing the currently used platform. * @@ -26,6 +26,9 @@ import com.fr.third.org.objenesis.ObjenesisException; */ public final class PlatformDescription { + /** JVM_NAME prefix for JRockit */ + public static final String JROCKIT = "BEA"; + /** JVM_NAME prefix for GCJ */ public static final String GNU = "GNU libgcj"; @@ -90,8 +93,9 @@ public final class PlatformDescription { + "JVM version=" + VM_VERSION + ", " + "JVM info=" + VM_INFO; - // Add the API level if it's an Android platform - if(ANDROID_VERSION != 0) { + // Add the API level is it's an Android platform + int androidVersion = ANDROID_VERSION; + if(androidVersion != 0) { desc += ", API level=" + ANDROID_VERSION; } desc += ")"; diff --git a/fine-kryo/src/com/fr/third/org/objenesis/strategy/SerializingInstantiatorStrategy.java b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/SerializingInstantiatorStrategy.java similarity index 87% rename from fine-kryo/src/com/fr/third/org/objenesis/strategy/SerializingInstantiatorStrategy.java rename to fine-objenesis/src/com/fr/third/org/objenesis/strategy/SerializingInstantiatorStrategy.java index fcaddc17f..3aa9e3a98 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/strategy/SerializingInstantiatorStrategy.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/SerializingInstantiatorStrategy.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,24 +60,24 @@ public class SerializingInstantiatorStrategy extends BaseInstantiatorStrategy { if(JVM_NAME.startsWith(HOTSPOT) || PlatformDescription.isThisJVM(OPENJDK)) { // Java 7 GAE was under a security manager so we use a degraded system if(isGoogleAppEngine() && PlatformDescription.SPECIFICATION_VERSION.equals("1.7")) { - return new ObjectInputStreamInstantiator(type); + return new ObjectInputStreamInstantiator(type); } - return new SunReflectionFactorySerializationInstantiator(type); + return new SunReflectionFactorySerializationInstantiator(type); } else if(JVM_NAME.startsWith(DALVIK)) { if(PlatformDescription.isAndroidOpenJDK()) { - return new ObjectStreamClassInstantiator(type); + return new ObjectStreamClassInstantiator(type); } - return new AndroidSerializationInstantiator(type); + return new AndroidSerializationInstantiator(type); } else if(JVM_NAME.startsWith(GNU)) { - return new GCJSerializationInstantiator(type); + return new GCJSerializationInstantiator(type); } else if(JVM_NAME.startsWith(PERC)) { - return new PercSerializationInstantiator(type); + return new PercSerializationInstantiator(type); } - return new SunReflectionFactorySerializationInstantiator(type); + return new SunReflectionFactorySerializationInstantiator(type); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/strategy/SingleInstantiatorStrategy.java b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/SingleInstantiatorStrategy.java similarity index 88% rename from fine-kryo/src/com/fr/third/org/objenesis/strategy/SingleInstantiatorStrategy.java rename to fine-objenesis/src/com/fr/third/org/objenesis/strategy/SingleInstantiatorStrategy.java index cda717e4b..6e0a55eba 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/strategy/SingleInstantiatorStrategy.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/SingleInstantiatorStrategy.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,8 +15,8 @@ */ package com.fr.third.org.objenesis.strategy; -import com.fr.third.org.objenesis.ObjenesisException; import com.fr.third.org.objenesis.instantiator.ObjectInstantiator; +import com.fr.third.org.objenesis.ObjenesisException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -59,7 +59,11 @@ public class SingleInstantiatorStrategy implements InstantiatorStrategy { public ObjectInstantiator newInstantiatorOf(Class type) { try { return (ObjectInstantiator) constructor.newInstance(type); - } catch (Exception e) { + } catch (InstantiationException e) { + throw new ObjenesisException(e); + } catch (IllegalAccessException e) { + throw new ObjenesisException(e); + } catch (InvocationTargetException e) { throw new ObjenesisException(e); } } diff --git a/fine-kryo/src/com/fr/third/org/objenesis/strategy/StdInstantiatorStrategy.java b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/StdInstantiatorStrategy.java similarity index 81% rename from fine-kryo/src/com/fr/third/org/objenesis/strategy/StdInstantiatorStrategy.java rename to fine-objenesis/src/com/fr/third/org/objenesis/strategy/StdInstantiatorStrategy.java index 3026dc207..e62d66591 100644 --- a/fine-kryo/src/com/fr/third/org/objenesis/strategy/StdInstantiatorStrategy.java +++ b/fine-objenesis/src/com/fr/third/org/objenesis/strategy/StdInstantiatorStrategy.java @@ -1,5 +1,5 @@ -/* - * Copyright 2006-2018 the original author or authors. +/** + * Copyright 2006-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,46 +53,49 @@ public class StdInstantiatorStrategy extends BaseInstantiatorStrategy { * @param type Class to instantiate * @return The ObjectInstantiator for the class */ - @Override public ObjectInstantiator newInstantiatorOf(Class type) { if(PlatformDescription.isThisJVM(HOTSPOT) || PlatformDescription.isThisJVM(OPENJDK)) { // Java 7 GAE was under a security manager so we use a degraded system if(PlatformDescription.isGoogleAppEngine() && PlatformDescription.SPECIFICATION_VERSION.equals("1.7")) { if(Serializable.class.isAssignableFrom(type)) { - return new ObjectInputStreamInstantiator(type); + return new ObjectInputStreamInstantiator(type); } - return new AccessibleInstantiator(type); + return new AccessibleInstantiator(type); } // The UnsafeFactoryInstantiator would also work. But according to benchmarks, it is 2.5 // times slower. So I prefer to use this one - return new SunReflectionFactoryInstantiator(type); + return new SunReflectionFactoryInstantiator(type); } else if(PlatformDescription.isThisJVM(DALVIK)) { if(PlatformDescription.isAndroidOpenJDK()) { // Starting at Android N which is based on OpenJDK - return new UnsafeFactoryInstantiator(type); + return new UnsafeFactoryInstantiator(type); } if(ANDROID_VERSION <= 10) { // Android 2.3 Gingerbread and lower - return new Android10Instantiator(type); + return new Android10Instantiator(type); } if(ANDROID_VERSION <= 17) { // Android 3.0 Honeycomb to 4.2 Jelly Bean - return new Android17Instantiator(type); + return new Android17Instantiator(type); } // Android 4.3 until Android N - return new Android18Instantiator(type); + return new Android18Instantiator(type); + } + else if(PlatformDescription.isThisJVM(JROCKIT)) { + // JRockit is compliant with HotSpot + return new SunReflectionFactoryInstantiator(type); } else if(PlatformDescription.isThisJVM(GNU)) { - return new GCJInstantiator(type); + return new GCJInstantiator(type); } else if(PlatformDescription.isThisJVM(PERC)) { - return new PercInstantiator(type); + return new PercInstantiator(type); } // Fallback instantiator, should work with most modern JVM - return new UnsafeFactoryInstantiator(type); + return new UnsafeFactoryInstantiator(type); } } diff --git a/fine-third-jdk11/src/com/fr/third/sun/misc/BASE64Decoder.java b/fine-third-jdk11/src/com/fr/third/sun/misc/BASE64Decoder.java new file mode 100644 index 000000000..8c261d23d --- /dev/null +++ b/fine-third-jdk11/src/com/fr/third/sun/misc/BASE64Decoder.java @@ -0,0 +1,96 @@ +package com.fr.third.sun.misc; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PushbackInputStream; + +/** + * @author zhouping + * @version 10.0 + * Created by zhouping on 2019/10/28 + */ +public class BASE64Decoder extends CharacterDecoder { + private static final char[] pem_array = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; + private static final byte[] pem_convert_array = new byte[256]; + byte[] decode_buffer = new byte[4]; + + public BASE64Decoder() { + } + + protected int bytesPerAtom() { + return 4; + } + + protected int bytesPerLine() { + return 72; + } + + protected void decodeAtom(PushbackInputStream var1, OutputStream var2, int var3) throws IOException { + byte var5 = -1; + byte var6 = -1; + byte var7 = -1; + byte var8 = -1; + if (var3 < 2) { + throw new CEFormatException("BASE64Decoder: Not enough bytes for an atom."); + } else { + int var4; + do { + var4 = var1.read(); + if (var4 == -1) { + throw new CEStreamExhausted(); + } + } while(var4 == 10 || var4 == 13); + + this.decode_buffer[0] = (byte)var4; + var4 = this.readFully(var1, this.decode_buffer, 1, var3 - 1); + if (var4 == -1) { + throw new CEStreamExhausted(); + } else { + if (var3 > 3 && this.decode_buffer[3] == 61) { + var3 = 3; + } + + if (var3 > 2 && this.decode_buffer[2] == 61) { + var3 = 2; + } + + switch(var3) { + case 4: + var8 = pem_convert_array[this.decode_buffer[3] & 255]; + case 3: + var7 = pem_convert_array[this.decode_buffer[2] & 255]; + case 2: + var6 = pem_convert_array[this.decode_buffer[1] & 255]; + var5 = pem_convert_array[this.decode_buffer[0] & 255]; + default: + switch(var3) { + case 2: + var2.write((byte)(var5 << 2 & 252 | var6 >>> 4 & 3)); + break; + case 3: + var2.write((byte)(var5 << 2 & 252 | var6 >>> 4 & 3)); + var2.write((byte)(var6 << 4 & 240 | var7 >>> 2 & 15)); + break; + case 4: + var2.write((byte)(var5 << 2 & 252 | var6 >>> 4 & 3)); + var2.write((byte)(var6 << 4 & 240 | var7 >>> 2 & 15)); + var2.write((byte)(var7 << 6 & 192 | var8 & 63)); + } + + } + } + } + } + + static { + int var0; + for(var0 = 0; var0 < 255; ++var0) { + pem_convert_array[var0] = -1; + } + + for(var0 = 0; var0 < pem_array.length; ++var0) { + pem_convert_array[pem_array[var0]] = (byte)var0; + } + + } +} \ No newline at end of file diff --git a/fine-third-jdk11/src/com/fr/third/sun/misc/BASE64Encoder.java b/fine-third-jdk11/src/com/fr/third/sun/misc/BASE64Encoder.java new file mode 100644 index 000000000..8942cc796 --- /dev/null +++ b/fine-third-jdk11/src/com/fr/third/sun/misc/BASE64Encoder.java @@ -0,0 +1,62 @@ +package com.fr.third.sun.misc; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * @author zhouping + * @version 10.0 + * Created by zhouping on 2019/10/28 + */ +public class BASE64Encoder extends CharacterEncoder { + + private static final char[] pem_array = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; + + public BASE64Encoder() { + + } + + protected int bytesPerAtom() { + + return 3; + } + + protected int bytesPerLine() { + + return 57; + } + + protected void encodeAtom(OutputStream var1, byte[] var2, int var3, int var4) throws IOException { + + byte var5; + if (var4 == 1) { + var5 = var2[var3]; + byte var6 = 0; + boolean var7 = false; + var1.write(pem_array[var5 >>> 2 & 63]); + var1.write(pem_array[(var5 << 4 & 48) + (var6 >>> 4 & 15)]); + var1.write(61); + var1.write(61); + } else { + byte var8; + if (var4 == 2) { + var5 = var2[var3]; + var8 = var2[var3 + 1]; + byte var9 = 0; + var1.write(pem_array[var5 >>> 2 & 63]); + var1.write(pem_array[(var5 << 4 & 48) + (var8 >>> 4 & 15)]); + var1.write(pem_array[(var8 << 2 & 60) + (var9 >>> 6 & 3)]); + var1.write(61); + } else { + var5 = var2[var3]; + var8 = var2[var3 + 1]; + byte var10 = var2[var3 + 2]; + var1.write(pem_array[var5 >>> 2 & 63]); + var1.write(pem_array[(var5 << 4 & 48) + (var8 >>> 4 & 15)]); + var1.write(pem_array[(var8 << 2 & 60) + (var10 >>> 6 & 3)]); + var1.write(pem_array[var10 & 63]); + } + } + + } +} \ No newline at end of file diff --git a/fine-third-jdk11/src/com/fr/third/sun/misc/CEFormatException.java b/fine-third-jdk11/src/com/fr/third/sun/misc/CEFormatException.java new file mode 100644 index 000000000..947c6bf8a --- /dev/null +++ b/fine-third-jdk11/src/com/fr/third/sun/misc/CEFormatException.java @@ -0,0 +1,16 @@ +package com.fr.third.sun.misc; + +import java.io.IOException; + +/** + * @author zhouping + * @version 10.0 + * Created by zhouping on 2019/10/28 + */ +public class CEFormatException extends IOException { + + public CEFormatException(String var1) { + + super(var1); + } +} \ No newline at end of file diff --git a/fine-third-jdk11/src/com/fr/third/sun/misc/CEStreamExhausted.java b/fine-third-jdk11/src/com/fr/third/sun/misc/CEStreamExhausted.java new file mode 100644 index 000000000..c02f142dd --- /dev/null +++ b/fine-third-jdk11/src/com/fr/third/sun/misc/CEStreamExhausted.java @@ -0,0 +1,15 @@ +package com.fr.third.sun.misc; + +import java.io.IOException; + +/** + * @author zhouping + * @version 10.0 + * Created by zhouping on 2019/10/28 + */ +public class CEStreamExhausted extends IOException { + + public CEStreamExhausted() { + + } +} \ No newline at end of file diff --git a/fine-third-jdk11/src/com/fr/third/sun/misc/CharacterDecoder.java b/fine-third-jdk11/src/com/fr/third/sun/misc/CharacterDecoder.java new file mode 100644 index 000000000..fe8d50066 --- /dev/null +++ b/fine-third-jdk11/src/com/fr/third/sun/misc/CharacterDecoder.java @@ -0,0 +1,107 @@ +package com.fr.third.sun.misc; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PushbackInputStream; +import java.nio.ByteBuffer; + +/** + * @author zhouping + * @version 10.0 + * Created by zhouping on 2019/10/28 + */ +public abstract class CharacterDecoder { + public CharacterDecoder() { + } + + protected abstract int bytesPerAtom(); + + protected abstract int bytesPerLine(); + + protected void decodeBufferPrefix(PushbackInputStream var1, OutputStream var2) throws IOException { + } + + protected void decodeBufferSuffix(PushbackInputStream var1, OutputStream var2) throws IOException { + } + + protected int decodeLinePrefix(PushbackInputStream var1, OutputStream var2) throws IOException { + return this.bytesPerLine(); + } + + protected void decodeLineSuffix(PushbackInputStream var1, OutputStream var2) throws IOException { + } + + protected void decodeAtom(PushbackInputStream var1, OutputStream var2, int var3) throws IOException { + throw new CEStreamExhausted(); + } + + protected int readFully(InputStream var1, byte[] var2, int var3, int var4) throws IOException { + for(int var5 = 0; var5 < var4; ++var5) { + int var6 = var1.read(); + if (var6 == -1) { + return var5 == 0 ? -1 : var5; + } + + var2[var5 + var3] = (byte)var6; + } + + return var4; + } + + public void decodeBuffer(InputStream var1, OutputStream var2) throws IOException { + int var4 = 0; + PushbackInputStream var5 = new PushbackInputStream(var1); + this.decodeBufferPrefix(var5, var2); + + while(true) { + try { + int var6 = this.decodeLinePrefix(var5, var2); + + int var3; + for(var3 = 0; var3 + this.bytesPerAtom() < var6; var3 += this.bytesPerAtom()) { + this.decodeAtom(var5, var2, this.bytesPerAtom()); + var4 += this.bytesPerAtom(); + } + + if (var3 + this.bytesPerAtom() == var6) { + this.decodeAtom(var5, var2, this.bytesPerAtom()); + var4 += this.bytesPerAtom(); + } else { + this.decodeAtom(var5, var2, var6 - var3); + var4 += var6 - var3; + } + + this.decodeLineSuffix(var5, var2); + } catch (CEStreamExhausted var8) { + this.decodeBufferSuffix(var5, var2); + return; + } + } + } + + public byte[] decodeBuffer(String var1) throws IOException { + byte[] var2 = new byte[var1.length()]; + var1.getBytes(0, var1.length(), var2, 0); + ByteArrayInputStream var3 = new ByteArrayInputStream(var2); + ByteArrayOutputStream var4 = new ByteArrayOutputStream(); + this.decodeBuffer(var3, var4); + return var4.toByteArray(); + } + + public byte[] decodeBuffer(InputStream var1) throws IOException { + ByteArrayOutputStream var2 = new ByteArrayOutputStream(); + this.decodeBuffer(var1, var2); + return var2.toByteArray(); + } + + public ByteBuffer decodeBufferToByteBuffer(String var1) throws IOException { + return ByteBuffer.wrap(this.decodeBuffer(var1)); + } + + public ByteBuffer decodeBufferToByteBuffer(InputStream var1) throws IOException { + return ByteBuffer.wrap(this.decodeBuffer(var1)); + } +} \ No newline at end of file diff --git a/fine-third-jdk11/src/com/fr/third/sun/misc/CharacterEncoder.java b/fine-third-jdk11/src/com/fr/third/sun/misc/CharacterEncoder.java new file mode 100644 index 000000000..41c77ede0 --- /dev/null +++ b/fine-third-jdk11/src/com/fr/third/sun/misc/CharacterEncoder.java @@ -0,0 +1,186 @@ +package com.fr.third.sun.misc; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.nio.ByteBuffer; + +/** + * @author zhouping + * @version 10.0 + * Created by zhouping on 2019/10/28 + */ +public abstract class CharacterEncoder { + protected PrintStream pStream; + + public CharacterEncoder() { + } + + protected abstract int bytesPerAtom(); + + protected abstract int bytesPerLine(); + + protected void encodeBufferPrefix(OutputStream var1) throws IOException { + this.pStream = new PrintStream(var1); + } + + protected void encodeBufferSuffix(OutputStream var1) throws IOException { + } + + protected void encodeLinePrefix(OutputStream var1, int var2) throws IOException { + } + + protected void encodeLineSuffix(OutputStream var1) throws IOException { + this.pStream.println(); + } + + protected abstract void encodeAtom(OutputStream var1, byte[] var2, int var3, int var4) throws IOException; + + protected int readFully(InputStream var1, byte[] var2) throws IOException { + for (int var3 = 0; var3 < var2.length; ++var3) { + int var4 = var1.read(); + if (var4 == -1) { + return var3; + } + + var2[var3] = (byte) var4; + } + + return var2.length; + } + + public void encode(InputStream var1, OutputStream var2) throws IOException { + byte[] var5 = new byte[this.bytesPerLine()]; + this.encodeBufferPrefix(var2); + + while (true) { + int var4 = this.readFully(var1, var5); + if (var4 == 0) { + break; + } + + this.encodeLinePrefix(var2, var4); + + for (int var3 = 0; var3 < var4; var3 += this.bytesPerAtom()) { + if (var3 + this.bytesPerAtom() <= var4) { + this.encodeAtom(var2, var5, var3, this.bytesPerAtom()); + } else { + this.encodeAtom(var2, var5, var3, var4 - var3); + } + } + + if (var4 < this.bytesPerLine()) { + break; + } + + this.encodeLineSuffix(var2); + } + + this.encodeBufferSuffix(var2); + } + + public void encode(byte[] var1, OutputStream var2) throws IOException { + ByteArrayInputStream var3 = new ByteArrayInputStream(var1); + this.encode((InputStream) var3, var2); + } + + public String encode(byte[] var1) { + ByteArrayOutputStream var2 = new ByteArrayOutputStream(); + ByteArrayInputStream var3 = new ByteArrayInputStream(var1); + String var4 = null; + + try { + this.encode((InputStream) var3, var2); + var4 = var2.toString("8859_1"); + return var4; + } catch (Exception var6) { + throw new Error("CharacterEncoder.encode internal error"); + } + } + + private byte[] getBytes(ByteBuffer var1) { + byte[] var2 = null; + if (var1.hasArray()) { + byte[] var3 = var1.array(); + if (var3.length == var1.capacity() && var3.length == var1.remaining()) { + var2 = var3; + var1.position(var1.limit()); + } + } + + if (var2 == null) { + var2 = new byte[var1.remaining()]; + var1.get(var2); + } + + return var2; + } + + public void encode(ByteBuffer var1, OutputStream var2) throws IOException { + byte[] var3 = this.getBytes(var1); + this.encode(var3, var2); + } + + public String encode(ByteBuffer var1) { + byte[] var2 = this.getBytes(var1); + return this.encode(var2); + } + + public void encodeBuffer(InputStream var1, OutputStream var2) throws IOException { + byte[] var5 = new byte[this.bytesPerLine()]; + this.encodeBufferPrefix(var2); + + int var4; + do { + var4 = this.readFully(var1, var5); + if (var4 == 0) { + break; + } + + this.encodeLinePrefix(var2, var4); + + for (int var3 = 0; var3 < var4; var3 += this.bytesPerAtom()) { + if (var3 + this.bytesPerAtom() <= var4) { + this.encodeAtom(var2, var5, var3, this.bytesPerAtom()); + } else { + this.encodeAtom(var2, var5, var3, var4 - var3); + } + } + + this.encodeLineSuffix(var2); + } while (var4 >= this.bytesPerLine()); + + this.encodeBufferSuffix(var2); + } + + public void encodeBuffer(byte[] var1, OutputStream var2) throws IOException { + ByteArrayInputStream var3 = new ByteArrayInputStream(var1); + this.encodeBuffer((InputStream) var3, var2); + } + + public String encodeBuffer(byte[] var1) { + ByteArrayOutputStream var2 = new ByteArrayOutputStream(); + ByteArrayInputStream var3 = new ByteArrayInputStream(var1); + + try { + this.encodeBuffer((InputStream) var3, var2); + } catch (Exception var5) { + throw new Error("CharacterEncoder.encodeBuffer internal error"); + } + + return var2.toString(); + } + + public void encodeBuffer(ByteBuffer var1, OutputStream var2) throws IOException { + byte[] var3 = this.getBytes(var1); + this.encodeBuffer(var3, var2); + } + + public String encodeBuffer(ByteBuffer var1) { + byte[] var2 = this.getBytes(var1); + return this.encodeBuffer(var2); + } +} \ No newline at end of file diff --git a/fine-third-jdk11/src/com/fr/third/sun/misc/Cleaner.java b/fine-third-jdk11/src/com/fr/third/sun/misc/Cleaner.java new file mode 100644 index 000000000..75b154185 --- /dev/null +++ b/fine-third-jdk11/src/com/fr/third/sun/misc/Cleaner.java @@ -0,0 +1,84 @@ +package com.fr.third.sun.misc; + +import java.lang.ref.PhantomReference; +import java.lang.ref.ReferenceQueue; +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * @author zhouping + * @version 10.0 + * Created by zhouping on 2019/10/29 + */ +public class Cleaner extends PhantomReference { + private static final ReferenceQueue dummyQueue = new ReferenceQueue(); + private static Cleaner first = null; + private Cleaner next = null; + private Cleaner prev = null; + private final Runnable thunk; + + private static synchronized Cleaner add(Cleaner var0) { + if (first != null) { + var0.next = first; + first.prev = var0; + } + + first = var0; + return var0; + } + + private static synchronized boolean remove(Cleaner var0) { + if (var0.next == var0) { + return false; + } else { + if (first == var0) { + if (var0.next != null) { + first = var0.next; + } else { + first = var0.prev; + } + } + + if (var0.next != null) { + var0.next.prev = var0.prev; + } + + if (var0.prev != null) { + var0.prev.next = var0.next; + } + + var0.next = var0; + var0.prev = var0; + return true; + } + } + + private Cleaner(Object var1, Runnable var2) { + super(var1, dummyQueue); + this.thunk = var2; + } + + public static Cleaner create(Object var0, Runnable var1) { + return var1 == null ? null : add(new Cleaner(var0, var1)); + } + + public void clean() { + if (remove(this)) { + try { + this.thunk.run(); + } catch (final Throwable var2) { + AccessController.doPrivileged(new PrivilegedAction() { + public Void run() { + if (System.err != null) { + (new Error("Cleaner terminated abnormally", var2)).printStackTrace(); + } + + System.exit(1); + return null; + } + }); + } + + } + } +}