Skip to content

Commit

Permalink
It is safe to call String.format now
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Dec 28, 2018
1 parent c382f2e commit 80186ee
Showing 1 changed file with 2 additions and 29 deletions.
31 changes: 2 additions & 29 deletions java/client/src/com/thoughtworks/selenium/condition/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
// under the License.
package com.thoughtworks.selenium.condition;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* Simple predicate class, which also knows how to wait for the condition to be true. Used by
* Selenium tests.
Expand All @@ -45,39 +42,15 @@ public Condition() {

/**
* Creates an instance of Condition with the given {@code message} and {@code args}, which are in
* the {@link String#format(String, Object...)} modeal.
* the {@link String#format(String, Object...)} model.
* @param message message
* @param args args
*/
public Condition(String message, Object[] args) {
if (null == message) {
throw new NullPointerException("Condition names must not be null");
}
// this.message = String.format(message, args);
this.message = simulateStringDotFormatMethod(message, args);
}

private String simulateStringDotFormatMethod(String message, Object[] args) {
int vers = Integer.parseInt(System.getProperty("java.class.version").substring(0, 2));
if (vers >= 49) {
try {
Method format = String.class.getMethod("format", String.class, Object[].class);
return (String) format.invoke(null, new Object[] {message, args});
} catch (NoSuchMethodException | IllegalAccessException e) {
} catch (InvocationTargetException e) {
Throwable throwable = e.getCause();
if (throwable instanceof RuntimeException) {
throw (RuntimeException) throwable;
}
}
throw new RuntimeException("String.format(..) can't be that hard to call");
}
String msg = "";
msg = message;
for (int i = 0; i < args.length; i++) {
msg = msg + " " + args[i];
}
return msg;
this.message = String.format(message, args);
}

// drop these for var-args in another year.
Expand Down

0 comments on commit 80186ee

Please sign in to comment.