Skip to content

Commit

Permalink
Use the JVM Name, not the Vendor for the constant (close #32)
Browse files Browse the repository at this point in the history
  • Loading branch information
henri-tremblay committed Jul 7, 2015
1 parent f692dd5 commit ae4ea6b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
16 changes: 12 additions & 4 deletions main/src/org/objenesis/strategy/PlatformDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package org.objenesis.strategy;

import java.lang.reflect.Field;

import org.objenesis.ObjenesisException;

import java.lang.reflect.Field;

/**
* List of constants describing the currently used platform.
*
Expand All @@ -32,8 +32,16 @@ public final class PlatformDescription {
/** JVM_NAME prefix for GCJ */
public static final String GNU = "GNU libgcj";

/** JVM_NAME prefix for Sun Java HotSpot */
public static final String SUN = "Java HotSpot";
/** JVM_NAME prefix for Java HotSpot */
public static final String HOTSPOT = "Java HotSpot";

/**
* JVM_NAME prefix for Java HotSpot
*
* @deprecated Use {@link #HOTSPOT} instead
*/
@Deprecated
public static final String SUN = HOTSPOT;

/** JVM_NAME prefix for the OpenJDK */
public static final String OPENJDK = "OpenJDK";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
*/
package org.objenesis.strategy;

import static org.objenesis.strategy.PlatformDescription.*;

import java.io.NotSerializableException;
import java.io.Serializable;

import org.objenesis.ObjenesisException;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.android.AndroidSerializationInstantiator;
import org.objenesis.instantiator.basic.ObjectStreamClassInstantiator;
import org.objenesis.instantiator.gcj.GCJSerializationInstantiator;
import org.objenesis.instantiator.perc.PercSerializationInstantiator;

import java.io.NotSerializableException;
import java.io.Serializable;

import static org.objenesis.strategy.PlatformDescription.*;

/**
* Guess the best serializing instantiator for a given class. The returned instantiator will
* instantiate classes like the genuine java serialization framework (the constructor of the first
Expand Down Expand Up @@ -55,7 +55,7 @@ public <T> ObjectInstantiator<T> newInstantiatorOf(Class<T> type) {
if(!Serializable.class.isAssignableFrom(type)) {
throw new ObjenesisException(new NotSerializableException(type+" not serializable"));
}
if(JVM_NAME.startsWith(SUN) || PlatformDescription.isThisJVM(OPENJDK)) {
if(JVM_NAME.startsWith(HOTSPOT) || PlatformDescription.isThisJVM(OPENJDK)) {
return new ObjectStreamClassInstantiator<T>(type);
}
else if(JVM_NAME.startsWith(DALVIK)) {
Expand Down
6 changes: 3 additions & 3 deletions main/src/org/objenesis/strategy/StdInstantiatorStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.objenesis.strategy;

import static org.objenesis.strategy.PlatformDescription.*;

import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.android.Android10Instantiator;
import org.objenesis.instantiator.android.Android17Instantiator;
Expand All @@ -27,6 +25,8 @@
import org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator;
import org.objenesis.instantiator.sun.UnsafeFactoryInstantiator;

import static org.objenesis.strategy.PlatformDescription.*;

/**
* Guess the best instantiator for a given class. The instantiator will instantiate the class
* without calling any constructor. Currently, the selection doesn't depend on the class. It relies
Expand All @@ -52,7 +52,7 @@ public class StdInstantiatorStrategy extends BaseInstantiatorStrategy {
*/
public <T> ObjectInstantiator<T> newInstantiatorOf(Class<T> type) {

if(PlatformDescription.isThisJVM(SUN) || PlatformDescription.isThisJVM(OPENJDK)) {
if(PlatformDescription.isThisJVM(HOTSPOT) || PlatformDescription.isThisJVM(OPENJDK)) {
// 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<T>(type);
Expand Down
9 changes: 7 additions & 2 deletions main/test/org/objenesis/strategy/PlatformDescriptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package org.objenesis.strategy;

import static org.junit.Assert.*;
import org.junit.Test;

import java.lang.reflect.Method;

import org.junit.Test;
import static org.junit.Assert.*;

/**
* Currently the test just check nothing is crashing. A more complex test should play with class
Expand All @@ -29,6 +29,11 @@
*/
public class PlatformDescriptionTest {

@Test
public void isJvmName() {
PlatformDescription.isThisJVM(PlatformDescription.HOTSPOT);
}

@Test
public void test() {
if(!PlatformDescription.isThisJVM(PlatformDescription.DALVIK)) {
Expand Down

0 comments on commit ae4ea6b

Please sign in to comment.