Skip to content
Permalink
Browse files

[ci maven-central-release] Fixes #979 Generalise getting current java…

… version to fix be able to use JavaAgent with latest java (#985)

Fixes #979 Generalise getting current java version to fix be able to use Javaagent with the latest java
  • Loading branch information...
thekingnothing committed Apr 21, 2019
1 parent 91c2f1b commit 73c569f2a3a0552c54d88e020f0bb8686433ee81
@@ -0,0 +1,232 @@
package org.powermock.utils;

public enum JavaVersion {

/**
* The Java version reported by Android. This is not an official Java version number.
*/
JAVA_0_9(1.5f, "0.9"),

/**
* Java 1.1.
*/
JAVA_1_1(1.1f, "1.1"),

/**
* Java 1.2.
*/
JAVA_1_2(1.2f, "1.2"),

/**
* Java 1.3.
*/
JAVA_1_3(1.3f, "1.3"),

/**
* Java 1.4.
*/
JAVA_1_4(1.4f, "1.4"),

/**
* Java 1.5.
*/
JAVA_1_5(1.5f, "1.5"),

/**
* Java 1.6.
*/
JAVA_1_6(1.6f, "1.6"),

/**
* Java 1.7.
*/
JAVA_1_7(1.7f, "1.7"),

/**
* Java 1.8.
*/
JAVA_1_8(1.8f, "1.8"),

/**
* Java 1.9.
*
* @deprecated As of release 3.5, replaced by {@link #JAVA_9}
*/
@Deprecated
JAVA_1_9(9.0f, "9"),

/**
* Java 9
*
* @since 3.5
*/
JAVA_9(9.0f, "9"),

/**
* Java 10
*
* @since 3.7
*/
JAVA_10(10.0f, "10"),

/**
* Java 11
*
* @since 3.8
*/
JAVA_11(11.0f, "11"),

/**
* Java 12
*
* @since 3.9
*/
JAVA_12(12.0f, "12"),

/**
* Java 13
*
* @since 3.9
*/
JAVA_13(13.0f, "13"),

/**
* The most recent java version. Mainly introduced to avoid to break when a new version of Java is used.
*/
JAVA_RECENT(maxVersion(), Float.toString(maxVersion()));

/**
* The float value.
*/
private final float value;

/**
* The standard name.
*/
private final String name;

/**
* Constructor.
*
* @param value the float value
* @param name the standard name, not null
*/
JavaVersion(final float value, final String name) {
this.value = value;
this.name = name;
}

//-----------------------------------------------------------------------
/**
* <p>Whether this version of Java is at least the version of Java passed in.</p>
*
* <p>For example:<br>
* {@code myVersion.atLeast(JavaVersion.JAVA_1_4)}<p>
*
* @param requiredVersion the version to check against, not null
* @return true if this version is equal to or greater than the specified version
*/
public boolean atLeast(final JavaVersion requiredVersion) {
return this.value >= requiredVersion.value;
}

/**
* Transforms the given string with a Java version number to the
* corresponding constant of this enumeration class. This method is used
* internally.
*
* @param nom the Java version as string
* @return the corresponding enumeration constant or <b>null</b> if the
* version is unknown
*/
static JavaVersion get(final String nom) {
if ("0.9".equals(nom)) {
return JAVA_0_9;
} else if ("1.1".equals(nom)) {
return JAVA_1_1;
} else if ("1.2".equals(nom)) {
return JAVA_1_2;
} else if ("1.3".equals(nom)) {
return JAVA_1_3;
} else if ("1.4".equals(nom)) {
return JAVA_1_4;
} else if ("1.5".equals(nom)) {
return JAVA_1_5;
} else if ("1.6".equals(nom)) {
return JAVA_1_6;
} else if ("1.7".equals(nom)) {
return JAVA_1_7;
} else if ("1.8".equals(nom)) {
return JAVA_1_8;
} else if ("9".equals(nom)) {
return JAVA_9;
} else if ("10".equals(nom)) {
return JAVA_10;
} else if ("11".equals(nom)) {
return JAVA_11;
} else if ("12".equals(nom)) {
return JAVA_12;
} else if ("13".equals(nom)) {
return JAVA_13;
}
if (nom == null) {
return null;
}
final float v = toFloatVersion(nom);
if ((v - 1.) < 1.) { // then we need to check decimals > .9
final int firstComma = Math.max(nom.indexOf('.'), nom.indexOf(','));
final int end = Math.max(nom.length(), nom.indexOf(',', firstComma));
if (Float.parseFloat(nom.substring(firstComma + 1, end)) > .9f) {
return JAVA_RECENT;
}
} else if (v > 10) {
return JAVA_RECENT;
}
return null;
}

//-----------------------------------------------------------------------
/**
* <p>The string value is overridden to return the standard name.</p>
*
* <p>For example, <code>"1.5"</code>.</p>
*
* @return the name, not null
*/
@Override
public String toString() {
return name;
}

/**
* Gets the Java Version from the system or 99.0 if the {@code java.specification.version} system property is not set.
*
* @return the value of {@code java.specification.version} system property or 99.0 if it is not set.
*/
private static float maxVersion() {
final float v = toFloatVersion(System.getProperty("java.specification.version", "99.0"));
if (v > 0) {
return v;
}
return 99f;
}

/**
* Parses a float value from a String.
*
* @param value the String to parse.
* @return the float value represented by the string or -1 if the given String can not be parsed.
*/
private static float toFloatVersion(final String value) {
final int defaultReturnValue = -1;
if (value.contains(".")) {
final String[] toParse = value.split("\\.");
if (toParse.length >= 2) {
return NumberUtils.toFloat(toParse[0] + '.' + toParse[1], defaultReturnValue);
}
} else {
return NumberUtils.toFloat(value, defaultReturnValue);
}
return defaultReturnValue;
}
}
@@ -0,0 +1,14 @@
package org.powermock.utils;

public class NumberUtils {
public static float toFloat(final String str, final float defaultValue) {
if (str == null) {
return defaultValue;
}
try {
return Float.parseFloat(str);
} catch (final NumberFormatException nfe) {
return defaultValue;
}
}
}
@@ -0,0 +1,16 @@
package org.powermock.utils;

import org.junit.Test;

import static org.assertj.core.api.Java6Assertions.assertThat;


public class JavaVersionTest {

@Test
public void should_return_true_for_current_version_that_if_high_than16() {
assertThat(JavaVersion.JAVA_RECENT.atLeast(JavaVersion.JAVA_1_6))
.isTrue();
}

}
@@ -0,0 +1,32 @@
package org.powermock.utils;

import org.assertj.core.data.Offset;
import org.junit.Test;

import static org.assertj.core.api.Java6Assertions.assertThat;

public class NumberUtilsTest {

@Test
public void should_parse_and_return_float() {
assertThat(NumberUtils.toFloat("133.1", 0.0f))
.as("String parsed to float")
.isEqualTo(133.1f, Offset.offset(0.01f));

}

@Test
public void should_return_default_value_when_cannot_parse() {
assertThat(NumberUtils.toFloat("11,1", 0.0f))
.as("String parsed to float")
.isEqualTo(0.0f, Offset.offset(0.01f));
}

@Test
public void should_return_default_value_when_string_is_null() {
assertThat(NumberUtils.toFloat(null, 0.0f))
.as("String parsed to float")
.isEqualTo(0.0f, Offset.offset(0.01f));
}

}
@@ -23,6 +23,8 @@
*/
package org.powermock.modules.agent;

import org.powermock.utils.JavaVersion;

import java.io.IOException;
import java.lang.instrument.Instrumentation;

@@ -37,17 +39,14 @@
{

static final String javaSpecVersion = System.getProperty("java.specification.version");
static final boolean jdk6OrLater = "1.6".equals(javaSpecVersion) || "1.7".equals(javaSpecVersion) || "1.8".equals(javaSpecVersion)
|| "9".equals(javaSpecVersion);
static final boolean jdk6OrLater = JavaVersion.JAVA_RECENT.atLeast(JavaVersion.JAVA_1_6);

private static final PowerMockClassTransformer classTransformer = new PowerMockClassTransformer();

private static Instrumentation instrumentation;

private PowerMockAgent() {}

public static boolean isJava6OrLater() { return jdk6OrLater; }

/**
* This method must only be called by the JVM, to provide the instrumentation object.
* In order for this to occur, the JVM must be started with "-javaagent:powermock-module-javaagent-nnn.jar" as a command line parameter

0 comments on commit 73c569f

Please sign in to comment.
You can’t perform that action at this time.