Skip to content

Commit

Permalink
Use SunReflectionFactorySerializationInstantiator for Java 9 (close #45)
Browse files Browse the repository at this point in the history
  • Loading branch information
henri-tremblay committed Jan 12, 2017
1 parent 67fea5f commit 274228d
Show file tree
Hide file tree
Showing 26 changed files with 211 additions and 39 deletions.
18 changes: 13 additions & 5 deletions gae/src/main/java/org/objenesis/gae/JspWriterListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
*/
package org.objenesis.gae;

import org.objenesis.tck.search.SearchWorkingInstantiatorListener;

import javax.servlet.jsp.JspWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;
import org.objenesis.tck.search.SearchWorkingInstantiatorListener;

import javax.servlet.jsp.JspWriter;

/**
* @author Henri Tremblay
*/
Expand All @@ -37,7 +40,7 @@ public JspWriterListener(JspWriter out) {

public void instantiatorSupported(Class<?> c) {
try {
writer.println(String.format(PATTERN, c.getSimpleName(), "Working!"));
writer.println(String.format(PATTERN, c.getSimpleName() + " (" + getTypology(c) + ")", "Working!"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -47,9 +50,14 @@ public void instantiatorUnsupported(Class<?> c, Throwable t) {
ByteArrayOutputStream b = new ByteArrayOutputStream();
t.printStackTrace(new PrintStream(b));
try {
writer.println(String.format(PATTERN, c.getSimpleName(), "KO - " + b.toString()));
writer.println(String.format(PATTERN, c.getSimpleName() + " (" + getTypology(c) + ")", "KO - " + b.toString()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private Typology getTypology(Class<?> c) {
Instantiator instantiatorAnn = c.getAnnotation(Instantiator.class);
return instantiatorAnn == null ? Typology.UNKNOWN : instantiatorAnn.value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* Instantiator for Android API level 10 and lover which creates objects without driving their
* constructors, using internal methods on the Dalvik implementation of
* {@link java.io.ObjectInputStream}.
*
*
* @author Piotr 'Qertoip' Włodarek
*/
@Instantiator(Typology.STANDARD)
public class Android10Instantiator<T> implements ObjectInstantiator<T> {
private final Class<T> type;
private final Method newStaticMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* Instantiator for Android API level 11 to 17 which creates objects without driving their
* constructors, using internal methods on the Dalvik implementation of {@link ObjectStreamClass}.
*
*
* @author Ian Parkinson (Google Inc.)
*/
@Instantiator(Typology.STANDARD)
public class Android17Instantiator<T> implements ObjectInstantiator<T> {
private final Class<T> type;
private final Method newInstanceMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* Instantiator for Android API leve 18 and higher. Same as the version 17 but the
* <code>newInstance</code> now takes a long in parameter
*
*
* @author Henri Tremblay
*/
@Instantiator(Typology.STANDARD)
public class Android18Instantiator<T> implements ObjectInstantiator<T> {
private final Class<T> type;
private final Method newInstanceMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* {@link ObjectInstantiator} for Android which creates objects using the constructor from the first
* non-serializable parent class constructor, using internal methods on the Dalvik implementation of
* {@link ObjectStreamClass}.
*
*
* @author Ian Parkinson (Google Inc.)
*/
@Instantiator(Typology.SERIALIZATION)
public class AndroidSerializationInstantiator<T> implements ObjectInstantiator<T> {
private final Class<T> type;
private final ObjectStreamClass objectStreamClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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 org.objenesis.instantiator.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Denote that the class in an instantiator of a given type
*
* @author Henri Tremblay
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Instantiator {

/**
* @return type of instantiator
*/
Typology value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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 org.objenesis.instantiator.annotations;

/**
* Possible types of instantiator
* @author Henri Tremblay
*/
public enum Typology {
/**
* Mark an instantiator used for standard instantiation (not calling a constructor).
*/
STANDARD,

/**
* Mark an instantiator used for serialization.
*/
SERIALIZATION,

/**
* Mark an instantiator that doesn't behave as a {@link Standard} or {@link Serialization} (calls a constructor, fails
* all the time, etc.)
*/
NOT_COMPLIANT,

/**
* No type specified
*/
UNKNOWN
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
*/
package org.objenesis.instantiator.basic;

import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* Instantiates a class by grabbing the no-args constructor, making it accessible and then calling
* Constructor.newInstance(). Although this still requires no-arg constructors, it can call
* non-public constructors (if the security manager allows it).
*
*
* @author Joe Walnes
* @see org.objenesis.instantiator.ObjectInstantiator
*/
@Instantiator(Typology.NOT_COMPLIANT)
public class AccessibleInstantiator<T> extends ConstructorInstantiator<T> {

public AccessibleInstantiator(Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* Instantiates a class by grabbing the no args constructor and calling Constructor.newInstance().
* This can deal with default public constructors, but that's about it.
*
*
* @author Joe Walnes
* @param <T> Type instantiated
* @see ObjectInstantiator
*/
@Instantiator(Typology.NOT_COMPLIANT)
public class ConstructorInstantiator<T> implements ObjectInstantiator<T> {

protected Constructor<T> constructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* The instantiator that always throws an exception. Mainly used for tests
*
*
* @author Henri Tremblay
*/
@Instantiator(Typology.NOT_COMPLIANT)
public class FailingInstantiator<T> implements ObjectInstantiator<T> {

public FailingInstantiator(Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* The simplest instantiator - simply calls Class.newInstance(). This can deal with default public
* constructors, but that's about it.
*
*
* @author Joe Walnes
* @see ObjectInstantiator
*/
@Instantiator(Typology.NOT_COMPLIANT)
public class NewInstanceInstantiator<T> implements ObjectInstantiator<T> {

private final Class<T> type;
Expand All @@ -36,7 +39,7 @@ public NewInstanceInstantiator(Class<T> type) {
public T newInstance() {
try {
return type.newInstance();
}
}
catch(Exception e) {
throw new ObjenesisException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
package org.objenesis.instantiator.basic;

import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* The instantiator that always return a null instance
*
*
* @author Henri Tremblay
*/
@Instantiator(Typology.NOT_COMPLIANT)
public class NullInstantiator<T> implements ObjectInstantiator<T> {

public NullInstantiator(Class<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@
*/
package org.objenesis.instantiator.basic;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamConstants;
import java.io.Serializable;

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;

import java.io.*;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* Instantiates a class by using a dummy input stream that always feeds data for an empty object of
Expand All @@ -30,6 +40,7 @@
* @author Leonardo Mesquita
* @see org.objenesis.instantiator.ObjectInstantiator
*/
@Instantiator(Typology.SERIALIZATION)
public class ObjectInputStreamInstantiator<T> implements ObjectInstantiator<T> {
private static class MockStream extends InputStream {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

/**
* Instantiates a class by using reflection to make a call to private method
* ObjectStreamClass.newInstance, present in many JVM implementations. This instantiator will create
* classes in a way compatible with serialization, calling the first non-serializable superclass'
* no-arg constructor.
*
*
* @author Leonardo Mesquita
* @see ObjectInstantiator
* @see java.io.Serializable
*/
@Instantiator(Typology.SERIALIZATION)
public class ObjectStreamClassInstantiator<T> implements ObjectInstantiator<T> {

private static Method newInstanceMethod;
Expand All @@ -46,7 +49,7 @@ private static void initialize() {
}
catch(NoSuchMethodException e) {
throw new ObjenesisException(e);
}
}
}
}

Expand All @@ -59,14 +62,14 @@ public ObjectStreamClassInstantiator(Class<T> type) {

@SuppressWarnings("unchecked")
public T newInstance() {

try {
return (T) newInstanceMethod.invoke(objStreamClass);
}
catch(Exception e) {
throw new ObjenesisException(e);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.annotations.Instantiator;
import org.objenesis.instantiator.annotations.Typology;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
Expand All @@ -32,6 +34,7 @@
*
* @author Henri Tremblay
*/
@Instantiator(Typology.STANDARD)
public class ProxyingInstantiator<T> implements ObjectInstantiator<T> {

private static final int INDEX_CLASS_THIS = 1;
Expand Down

0 comments on commit 274228d

Please sign in to comment.