Skip to content

Commit

Permalink
Upgrade to Paranamer 2.8
Browse files Browse the repository at this point in the history
- the upgrade fixes an incompatibility with Java 8 that required that
  for Java 8 JGiven had to use reflection based on the -parameters
  option of the javac compiler. With the new Paranamer version 2.8 this
  is not necessary anymore
  • Loading branch information
Jan Schäfer committed Aug 26, 2015
1 parent 93136a4 commit ca25e93
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 53 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ subprojects {
assertjVersion = '1.7.0'
slf4jVersion = '1.7.7'
cglibVersion = '2.2.2'
paranamerVersion = '2.7'
paranamerVersion = '2.8'
jansiVersion = '1.11'
gsonVersion = '2.3'
guavaVersion = '18.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.lang.String.format;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -22,23 +21,8 @@ public class ParameterNameUtil {

private static final Paranamer PARANAMER = new BytecodeReadingParanamer();

private static final Method GET_PARAMETERS_METHOD = getParametersMethod();
private static Method getNameMethod;

private static Method getParametersMethod() {
try {
/**
* This method only exist since Java 8, thus we have to use reflection to use it,
* to stay compatible with Java 6
*/
return Method.class.getMethod( "getParameters" );
} catch( NoSuchMethodException e ) {
return null;
}
}

/**
* @throws NullPointerException iif {@code constructorOrMethod} is {@code null}
/**
* @throws NullPointerException iif {@code constructorOrMethod} is {@code null}
*/
public static List<NamedArgument> mapArgumentsWithParameterNames( AccessibleObject constructorOrMethod, List<Object> arguments ) {
Preconditions.checkNotNull( constructorOrMethod, "constructorOrMethod must not be null." );
Expand All @@ -48,7 +32,7 @@ public static List<NamedArgument> mapArgumentsWithParameterNames( AccessibleObje
return Collections.emptyList();
}

List<String> names = getParameterNames( constructorOrMethod );
List<String> names = getParameterNamesUsingParanamer( constructorOrMethod );

List<NamedArgument> result = Lists.newArrayList();
if( names.size() == arguments.size() ) {
Expand All @@ -65,36 +49,6 @@ public static List<NamedArgument> mapArgumentsWithParameterNames( AccessibleObje
return result;
}

private static List<String> getParameterNames( AccessibleObject constructorOrMethod ) {
if( GET_PARAMETERS_METHOD != null ) {
return getParameterNamesUsingJava8( constructorOrMethod );
} else {
return getParameterNamesUsingParanamer( constructorOrMethod );
}
}

private static List<String> getParameterNamesUsingJava8( AccessibleObject constructorOrMethod ) {
try {
Object[] parameters = (Object[]) GET_PARAMETERS_METHOD.invoke( constructorOrMethod );
List<String> parameterNames = Lists.newArrayList();
for( Object parameter : parameters ) {
String name = (String) getNameMethod( parameter.getClass() ).invoke( parameter );
parameterNames.add( name );
}
return parameterNames;
} catch( Exception e ) {
log.warn( format( "Could not call method getParameters on '%s'", constructorOrMethod ), e );
return Collections.emptyList();
}
}

private static Method getNameMethod( Class<?> aClass ) throws NoSuchMethodException {
if( getNameMethod == null ) {
getNameMethod = aClass.getMethod( "getName" );
}
return getNameMethod;
}

private static List<String> getParameterNamesUsingParanamer( AccessibleObject constructorOrMethod ) {
try {
return Arrays.asList( PARANAMER.lookupParameterNames( constructorOrMethod ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import static java.lang.String.format;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -249,7 +254,7 @@ public static void makeAccessible( AccessibleObject object, String errorDescript
object.setAccessible( true );
} catch( SecurityException e ) {
log.debug( "Caught exception: ", e );
log.warn( "Could not make %s accessible, trying to access it nevertheless and hoping for the best.",
log.warn( "Could not make {} accessible, trying to access it nevertheless and hoping for the best.",
toReadableString( object ), errorDescription );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.tngtech.jgiven.junit.SimpleScenarioTest;
import com.tngtech.jgiven.report.model.StepModel;

public class Java8Test extends SimpleScenarioTest<LambdaSteps> {
public class Java8Test extends SimpleScenarioTest<LambdaSteps<?>> {

@Test
public void lambda_steps_work() {
Expand Down

0 comments on commit ca25e93

Please sign in to comment.