Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions exercises/concept/lasagna/src/test/java/utils/Lasagna.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ public String getTargetClassName() {
}

public int expectedMinutesInOven() {
return invokeMethod("expectedMinutesInOven", new Class[]{});
return invokeMethod("expectedMinutesInOven", Integer.class, new Class[]{});
}

public int remainingMinutesInOven(int actualMinutes) {
return invokeMethod("remainingMinutesInOven", new Class[]{int.class}, actualMinutes);
return invokeMethod("remainingMinutesInOven", Integer.class, new Class[]{int.class}, actualMinutes);
}

public int preparationTimeInMinutes(int amountLayers) {
return invokeMethod("preparationTimeInMinutes", new Class[]{int.class}, amountLayers);
return invokeMethod("preparationTimeInMinutes", Integer.class, new Class[]{int.class}, amountLayers);
}

public int totalTimeInMinutes(int amountLayers, int actualMinutes) {
return invokeMethod("totalTimeInMinutes", new Class[]{int.class, int.class}, amountLayers, actualMinutes);
return invokeMethod("totalTimeInMinutes", Integer.class, new Class[]{int.class, int.class},
amountLayers, actualMinutes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,28 @@ public boolean isMethodReturnType(Class<?> returnType, String name, Class<?>...
/**
* Invokes a method from the target instance
* @param methodName The name of the method
* @param returnType The class representing the expected return type
* @param parameterTypes The list of parameter types
* @param parameterValues The list with values for the method parameters
* @param <T> The result type we expect the method to be
* @return The value returned by the method
*/
protected <T> T invokeMethod(String methodName, Class<?>[] parameterTypes, Object... parameterValues) {
protected <T> T invokeMethod(String methodName, Class<T> returnType, Class<?>[] parameterTypes,
Object... parameterValues) {
if (target == null) {
return null;
}
try {
// getDeclaredMethod is used to get protected/private methods
Method method = target.getClass().getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
return (T) method.invoke(target, parameterValues);
return returnType.cast(method.invoke(target, parameterValues));
} catch (NoSuchMethodException e) {
try {
// try getting it from parent class, but only public methods will work
Method method = target.getClass().getMethod(methodName, parameterTypes);
method.setAccessible(true);
return (T) method.invoke(target, parameterValues);
return returnType.cast(method.invoke(target, parameterValues));
} catch (Exception ex) {
return null;
}
Expand Down Expand Up @@ -381,7 +383,7 @@ public int hashCode() {
* @return The result of 'toString' from the target instance
*/
public String toString() {
return invokeMethod("toString", new Class[]{ });
return invokeMethod("toString", String.class, new Class[]{ });
}

/**
Expand All @@ -390,14 +392,14 @@ public String toString() {
* @param <T> The type we are expecting it to be
* @return The value of the property (if it exists)
*/
protected <T> T getPropertyValue(String propertyName) {
protected <T> T getPropertyValue(String propertyName, Class<T> propertyType) {
if (target == null || !hasProperty(propertyName)) {
return null;
}
try {
Field field = target.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
return (T) field.get(target);
return propertyType.cast(field.get(target));
} catch (Exception e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@ public String getTargetClassName() {
}

public String describe(Character character) {
return invokeMethod("describe", new Class[] { Character.class }, character);
return invokeMethod("describe", String.class, new Class[] { Character.class }, character);
}

public String describe(Destination character) {
return invokeMethod("describe", new Class[] { Destination.class }, character);
return invokeMethod("describe", String.class, new Class[] { Destination.class }, character);
}

public String describe(TravelMethod character) {
return invokeMethod("describe", new Class[] { TravelMethod.class }, character);
return invokeMethod("describe", String.class, new Class[] { TravelMethod.class }, character);
}

public String describe(Character character, Destination destination, TravelMethod travelMethod) {
return invokeMethod("describe", new Class[] { Character.class, Destination.class, TravelMethod.class },
return invokeMethod("describe", String.class,
new Class[] { Character.class, Destination.class, TravelMethod.class },
character, destination, travelMethod);
}

public String describe(Character character, Destination destination) {
return invokeMethod("describe", new Class[] { Character.class, Destination.class }, character, destination);
return invokeMethod("describe", String.class, new Class[] { Character.class, Destination.class },
character, destination);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ public boolean isMethodReturnType(Class<?> returnType, String name, Class<?>...
* Invokes a method from the target instance
*
* @param methodName The name of the method
* @param returnType The class representing the expected return type
* @param parameterTypes The list of parameter types
* @param parameterValues The list with values for the method parameters
* @param <T> The result type we expect the method to be
* @return The value returned by the method
*/
protected <T> T invokeMethod(String methodName, Class<?>[] parameterTypes, Object... parameterValues) {
protected <T> T invokeMethod(String methodName, Class<T> returnType, Class<?>[] parameterTypes,
Object... parameterValues) {
if (target == null) {
throw new UnsupportedOperationException();
}
Expand All @@ -131,12 +133,12 @@ protected <T> T invokeMethod(String methodName, Class<?>[] parameterTypes, Objec
// getDeclaredMethod is used to get protected/private methods
Method method = target.getClass().getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
return (T) method.invoke(target, parameterValues);
return returnType.cast(method.invoke(target, parameterValues));
} catch (NoSuchMethodException e) {
// try getting it from parent class, but only public methods will work
Method method = target.getClass().getMethod(methodName, parameterTypes);
method.setAccessible(true);
return (T) method.invoke(target, parameterValues);
return returnType.cast(method.invoke(target, parameterValues));
}
} catch (Exception e) {
throw new UnsupportedOperationException(e);
Expand Down Expand Up @@ -360,7 +362,6 @@ public boolean isConstructorPublic(Class<?>... parameterTypes) {

/**
* Proxy for the 'equals' method
*
* @param obj The ReflexionProxy object you want to compare against
* @return True if both targets are equal, false otherwise
*/
Expand All @@ -379,7 +380,6 @@ public boolean equals(Object obj) {

/**
* Proxy for the 'hashCode' method
*
* @return The hashCode from the target class
*/
public int hashCode() {
Expand All @@ -397,36 +397,33 @@ public int hashCode() {

/**
* Proxy for the 'toString' method from the target class
*
* @return The result of 'toString' from the target instance
*/
public String toString() {
return invokeMethod("toString", new Class[]{});
return invokeMethod("toString", String.class, new Class[]{ });
}

/**
* Gets a property value from the target instance (if it exists)
*
* @param propertyName The name of the property
* @param <T> The type we are expecting it to be
* @param <T> The type we are expecting it to be
* @return The value of the property (if it exists)
*/
protected <T> T getPropertyValue(String propertyName) {
protected <T> T getPropertyValue(String propertyName, Class<T> propertyType) {
if (target == null || !hasProperty(propertyName)) {
return null;
}
try {
Field field = target.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
return (T) field.get(target);
return propertyType.cast(field.get(target));
} catch (Exception e) {
return null;
}
}

/**
* Checks if the target class is abstract
*
* @return True if the target class exists and is abstract, false otherwise
*/
public boolean isAbstract() {
Expand All @@ -439,7 +436,6 @@ public boolean isAbstract() {

/**
* Checks if the target class extends another
*
* @param className The fully qualified name of the class it should extend
* @return True if the target class extends the specified one, false otherwise
*/
Expand All @@ -458,7 +454,6 @@ public boolean extendsClass(String className) {

/**
* Checks if the target class is an interface
*
* @return True if the target class exists and is an interface, false otherwise
*/
public boolean isInterface() {
Expand All @@ -471,8 +466,7 @@ public boolean isInterface() {

/**
* Checks if a method is abstract
*
* @param name The name of the method
* @param name The name of the method
* @param parameterTypes The list of method parameter types
* @return True if the method exists and is abstract, false otherwise
*/
Expand All @@ -491,8 +485,7 @@ public boolean isMethodAbstract(String name, Class<?>... parameterTypes) {

/**
* Checks if a method is protected
*
* @param name The name of the method
* @param name The name of the method
* @param parameterTypes The list of method parameter types
* @return True if the method exists and is protected, false otherwise
*/
Expand All @@ -511,3 +504,4 @@ public boolean isMethodProtected(String name, Class<?>... parameterTypes) {

//endregion
}

Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,28 @@ public boolean isMethodReturnType(Class<?> returnType, String name, Class<?>...
/**
* Invokes a method from the target instance
* @param methodName The name of the method
* @param returnType The class representing the expected return type
* @param parameterTypes The list of parameter types
* @param parameterValues The list with values for the method parameters
* @param <T> The result type we expect the method to be
* @return The value returned by the method
*/
protected <T> T invokeMethod(String methodName, Class<?>[] parameterTypes, Object... parameterValues) {
protected <T> T invokeMethod(String methodName, Class<T> returnType, Class<?>[] parameterTypes,
Object... parameterValues) {
if (target == null) {
return null;
}
try {
// getDeclaredMethod is used to get protected/private methods
Method method = target.getClass().getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
return (T) method.invoke(target, parameterValues);
return returnType.cast(method.invoke(target, parameterValues));
} catch (NoSuchMethodException e) {
try {
// try getting it from parent class, but only public methods will work
Method method = target.getClass().getMethod(methodName, parameterTypes);
method.setAccessible(true);
return (T) method.invoke(target, parameterValues);
return returnType.cast(method.invoke(target, parameterValues));
} catch (Exception ex) {
return null;
}
Expand Down Expand Up @@ -379,23 +381,24 @@ public int hashCode() {
* @return The result of 'toString' from the target instance
*/
public String toString() {
return invokeMethod("toString", new Class[]{ });
return invokeMethod("toString", String.class, new Class[]{ });
}

/**
* Gets a property value from the target instance (if it exists)
* @param propertyName The name of the property
* @param propertyType The class representing the property's type
* @param <T> The type we are expecting it to be
* @return The value of the property (if it exists)
*/
protected <T> T getPropertyValue(String propertyName) {
protected <T> T getPropertyValue(String propertyName, Class<T> propertyType) {
if (target == null || !hasProperty(propertyName)) {
return null;
}
try {
Field field = target.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
return (T) field.get(target);
return propertyType.cast(field.get(target));
} catch (Exception e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ public String getTargetClassName() {
}

public String toString() {
return invokeMethod("toString", new Class[0]);
return invokeMethod("toString", String.class, new Class[0]);
}

boolean isVulnerable() {
return invokeMethod("isVulnerable", new Class[0]);
return invokeMethod("isVulnerable", Boolean.class, new Class[0]);
}

int getDamagePoints(Fighter target) {
return invokeMethod("getDamagePoints", new Class[]{Fighter.class}, target);
return invokeMethod("getDamagePoints", Integer.class, new Class[]{Fighter.class}, target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ public String getTargetClassName() {
}

public String toString() {
return invokeMethod("toString", new Class[0]);
return invokeMethod("toString", String.class, new Class[0]);
}

boolean isVulnerable() {
return invokeMethod("isVulnerable", new Class[0]);
return invokeMethod("isVulnerable", Boolean.class, new Class[0]);
}

int getDamagePoints(Fighter target) {
return invokeMethod("getDamagePoints", new Class[]{Fighter.class}, target);
return invokeMethod("getDamagePoints", Integer.class, new Class[]{Fighter.class}, target);
}

void prepareSpell() {
invokeMethod("prepareSpell", new Class[0]);
invokeMethod("prepareSpell", Void.class, new Class[0]);
}
}
Loading