Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup code #1357

Merged
merged 4 commits into from
Jan 28, 2022
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
2 changes: 1 addition & 1 deletion framework/src/play/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ static boolean niceThrowable(org.apache.log4j.Level level, Throwable e, String m
} else {
errorOut.println(playException.getErrorTitle());
}
errorOut.println(playException.getErrorDescription().replaceAll("</?\\w+/?>", "").replace("\n", " "));
errorOut.println(playException.getErrorDescription().replaceAll("</?\\w+/?>", "").replace('\n', ' '));
} else {
sw.append(format(message, args));
}
Expand Down
4 changes: 2 additions & 2 deletions framework/src/play/classloading/ApplicationClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public byte[] enhance() {
if (System.getProperty("precompile") != null) {
try {
// emit bytecode to standard class layout as well
File f = Play.getFile("precompiled/java/" + name.replace(".", "/") + ".class");
File f = Play.getFile("precompiled/java/" + name.replace('.', '/') + ".class");
f.getParentFile().mkdirs();
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(this.enhancedByteCode);
Expand Down Expand Up @@ -361,7 +361,7 @@ public static VirtualFile getJava(String name) {
fileName = fileName.substring(0, fileName.indexOf("$"));
}
// the local variable fileOrDir is important!
String fileOrDir = fileName.replace(".", "/");
String fileOrDir = fileName.replace('.', '/');
fileName = fileOrDir + ".java";
for (VirtualFile path : Play.javaPath) {
// 1. check if there is a folder (without extension)
Expand Down
4 changes: 2 additions & 2 deletions framework/src/play/classloading/ApplicationClassloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Class<?> loadApplicationClass(String name) {

if (Play.usePrecompiled) {
try {
File file = Play.getFile("precompiled/java/" + name.replace(".", "/") + ".class");
File file = Play.getFile("precompiled/java/" + name.replace('.', '/') + ".class");
if (!file.exists()) {
return null;
}
Expand Down Expand Up @@ -216,7 +216,7 @@ private void loadPackage(String className) {
* Search for the byte code of the given class.
*/
byte[] getClassDefinition(String name) {
name = name.replace(".", "/") + ".class";
name = name.replace('.', '/') + ".class";
InputStream is = this.getResourceAsStream(name);
if (is == null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/classloading/ApplicationCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public void acceptResult(CompilationResult result) {
// If error
if (result.hasErrors()) {
for (IProblem problem : result.getErrors()) {
String className = new String(problem.getOriginatingFileName()).replace("/", ".");
String className = new String(problem.getOriginatingFileName()).replace('/', '.');
className = className.substring(0, className.length() - 5);
String message = problem.getMessage();
if (problem.getID() == IProblem.CannotImportPackage) {
Expand Down
16 changes: 10 additions & 6 deletions framework/src/play/classloading/BytecodeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.MessageDigest;
import java.util.regex.Pattern;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.io.FileUtils.writeByteArrayToFile;

/**
* Used to speed up compilation time
*/
public class BytecodeCache {

private static final Pattern REPLACED_CHARS = Pattern.compile("[/{}:]");

/**
* Delete the bytecode
* @param name Cache name
Expand All @@ -25,7 +29,7 @@ public static void deleteBytecode(String name) {
if (!Play.initialized || Play.tmpDir == null || Play.readOnlyTmp || !Play.configuration.getProperty("play.bytecodeCache", "true").equals("true")) {
return;
}
File f = cacheFile(name.replace("/", "_").replace("{", "_").replace("}", "_").replace(":", "_"));
File f = cacheFile(REPLACED_CHARS.matcher(name).replaceAll("_"));
if (f.exists()) {
f.delete();
}
Expand All @@ -45,7 +49,7 @@ public static byte[] getBytecode(String name, String source) {
if (!Play.initialized || Play.tmpDir == null || !Play.configuration.getProperty("play.bytecodeCache", "true").equals("true")) {
return null;
}
File f = cacheFile(name.replace("/", "_").replace("{", "_").replace("}", "_").replace(":", "_"));
File f = cacheFile(REPLACED_CHARS.matcher(name).replaceAll("_"));
if (f.exists()) {
FileInputStream fis = new FileInputStream(f);
// Read hash
Expand Down Expand Up @@ -90,16 +94,16 @@ public static void cacheBytecode(byte[] byteCode, String name, String source) {
if (!Play.initialized || Play.tmpDir == null || Play.readOnlyTmp || !Play.configuration.getProperty("play.bytecodeCache", "true").equals("true")) {
return;
}
File f = cacheFile(name.replace("/", "_").replace("{", "_").replace("}", "_").replace(":", "_"));
File f = cacheFile(REPLACED_CHARS.matcher(name).replaceAll("_"));
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(hash(source).getBytes("utf-8"));
fos.write(hash(source).getBytes(UTF_8));
fos.write(0);
fos.write(byteCode);
}

// emit bytecode to standard class layout as well
if (!name.contains("/") && !name.contains("{")) {
f = new File(Play.tmpDir, "classes/" + name.replace(".", "/") + ".class");
f = new File(Play.tmpDir, "classes/" + name.replace('.', '/') + ".class");
f.getParentFile().mkdirs();
writeByteArrayToFile(f, byteCode);
}
Expand All @@ -124,7 +128,7 @@ static String hash(String text) {
}
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update((Play.version + plugins + text).getBytes("utf-8"));
messageDigest.update((Play.version + plugins + text).getBytes(UTF_8));
byte[] digest = messageDigest.digest();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < digest.length; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/classloading/enhancers/Enhancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public InputStream openClassfile(String className) throws NotFoundException {

if (Play.usePrecompiled) {
try {
File file = Play.getFile("precompiled/java/" + className.replace(".", "/") + ".class");
File file = Play.getFile("precompiled/java/" + className.replace('.', '/') + ".class");
return new FileInputStream(file);
} catch (Exception e) {
Logger.error("Missing class %s", className);
Expand Down
9 changes: 2 additions & 7 deletions framework/src/play/data/parsing/ApacheMultipartParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package play.data.parsing;

import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static org.apache.commons.io.FileUtils.readFileToByteArray;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -634,13 +635,7 @@ private byte[] getBoundary(String contentType) {
if (boundaryStr == null) {
return null;
}
byte[] boundary;
try {
boundary = boundaryStr.getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException e) {
boundary = boundaryStr.getBytes();
}
return boundary;
return boundaryStr.getBytes(ISO_8859_1);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/i18n/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static String findClosestMatch(Collection<String> desiredLocales) {
ArrayList<String> cleanLocales = new ArrayList<>(desiredLocales.size());
//look for an exact match
for (String a : desiredLocales) {
a = a.replace("-", "_");
a = a.replace('-', '_');
cleanLocales.add(a);
for (String locale : Play.langs) {
if (locale.equalsIgnoreCase(a)) {
Expand Down
18 changes: 6 additions & 12 deletions framework/src/play/libs/Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import play.exceptions.UnexpectedException;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Codec utils
*/
Expand All @@ -32,11 +34,7 @@ public static String UUID() {
* @return The base64 encoded String
*/
public static String encodeBASE64(String value) {
try {
return new String(Base64.encodeBase64(value.getBytes("utf-8")));
} catch (UnsupportedEncodingException ex) {
throw new UnexpectedException(ex);
}
return new String(Base64.encodeBase64(value.getBytes(UTF_8)));
}

/**
Expand All @@ -58,11 +56,7 @@ public static String encodeBASE64(byte[] value) {
* @return decoded binary data
*/
public static byte[] decodeBASE64(String value) {
try {
return Base64.decodeBase64(value.getBytes("utf-8"));
} catch (UnsupportedEncodingException ex) {
throw new UnexpectedException(ex);
}
return Base64.decodeBase64(value.getBytes(UTF_8));
}

/**
Expand All @@ -76,7 +70,7 @@ public static String hexMD5(String value) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(value.getBytes("utf-8"));
messageDigest.update(value.getBytes(UTF_8));
byte[] digest = messageDigest.digest();
return byteToHexString(digest);
} catch (Exception ex) {
Expand All @@ -95,7 +89,7 @@ public static String hexSHA1(String value) {
try {
MessageDigest md;
md = MessageDigest.getInstance("SHA-1");
md.update(value.getBytes("utf-8"));
md.update(value.getBytes(UTF_8));
byte[] digest = md.digest();
return byteToHexString(digest);
} catch (Exception ex) {
Expand Down
4 changes: 3 additions & 1 deletion framework/src/play/libs/Crypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import play.Play;
import play.exceptions.UnexpectedException;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Cryptography utils
*/
Expand Down Expand Up @@ -71,7 +73,7 @@ public static String sign(String message, byte[] key) {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
mac.init(signingKey);
byte[] messageBytes = message.getBytes("utf-8");
byte[] messageBytes = message.getBytes(UTF_8);
byte[] result = mac.doFinal(messageBytes);
int len = result.length;
char[] hexChars = new char[len * 2];
Expand Down
6 changes: 3 additions & 3 deletions framework/src/play/mvc/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -852,13 +852,13 @@ protected static void render(Object... args) {
protected static String template() {
Request theRequest = Request.current();
String format = theRequest.format;
String templateName = theRequest.action.replace(".", "/") + "." + (format == null ? "html" : format);
String templateName = theRequest.action.replace('.', '/') + "." + (format == null ? "html" : format);
if (templateName.startsWith("@")) {
templateName = templateName.substring(1);
if (!templateName.contains(".")) {
templateName = theRequest.controller + "." + templateName;
}
templateName = templateName.replace(".", "/") + "." + (format == null ? "html" : format);
templateName = templateName.replace('.', '/') + "." + (format == null ? "html" : format);
}
return null == templateNameResolver ? templateName : templateNameResolver.resolveTemplateName(templateName);
}
Expand All @@ -879,7 +879,7 @@ protected static String template(String templateName) {
if (!templateName.contains(".")) {
templateName = theRequest.controller + "." + templateName;
}
templateName = templateName.replace(".", "/") + "." + (format == null ? "html" : format);
templateName = templateName.replace('.', '/') + "." + (format == null ? "html" : format);
}
return templateName;
}
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/mvc/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public static Future<Boolean> send(Object... args) {
templateName = templateName.substring("controllers.".length());
}
templateName = templateName.substring(0, templateName.indexOf("("));
templateName = templateName.replace(".", "/");
templateName = templateName.replace('.', '/');

// overrides Template name
if (args.length > 0 && args[0] instanceof String && LocalVariablesNamesTracer.getAllLocalVariableNames(args[0]).isEmpty()) {
Expand Down
8 changes: 5 additions & 3 deletions framework/src/play/templates/GroovyTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
import play.utils.HTML;
import play.utils.Java;

import static java.nio.charset.StandardCharsets.UTF_8;

public class GroovyTemplate extends BaseTemplate {

static final Map<String, SafeFormatter> safeFormatters = new HashMap<>();
Expand Down Expand Up @@ -187,13 +189,13 @@ public void call(GroovyClass gclass) {
sb.append("\n");
}
// Cache
BytecodeCache.cacheBytecode(sb.toString().getBytes("utf-8"), name, source);
BytecodeCache.cacheBytecode(sb.toString().getBytes(UTF_8), name, source);
compiledTemplate = tClassLoader.loadClass(groovyClassesForThisTemplate.get(0).getName());
if (System.getProperty("precompile") != null) {
try {
// emit bytecode to standard class layout as well
File f = Play.getFile("precompiled/templates/"
+ name.replaceAll("\\{(.*)\\}", "from_$1").replace(":", "_").replace("..", "parent"));
+ name.replaceAll("\\{(.*)\\}", "from_$1").replace(':', '_').replace("..", "parent"));
f.getParentFile().mkdirs();
FileUtils.write(f, sb.toString(), "utf-8");
} catch (Exception e) {
Expand Down Expand Up @@ -415,7 +417,7 @@ public Object getProperty(String property) {
}

public void invokeTag(Integer fromLine, String tag, Map<String, Object> attrs, Closure body) {
String templateName = tag.replace(".", "/");
String templateName = tag.replace('.', '/');
String callerExtension = (extension != null) ? extension : "tag";

BaseTemplate tagTemplate = null;
Expand Down
4 changes: 2 additions & 2 deletions framework/src/play/templates/TemplateLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static Template load(VirtualFile file) {
if (!templates.containsKey(key) || templates.get(key).compiledTemplate == null) {
if (Play.usePrecompiled) {
BaseTemplate template = new GroovyTemplate(
fileRelativePath.replaceAll("\\{(.*)\\}", "from_$1").replace(":", "_").replace("..", "parent"), "");
fileRelativePath.replaceAll("\\{(.*)\\}", "from_$1").replace(':', '_').replace("..", "parent"), "");
try {
template.loadPrecompiled();
templates.put(key, template);
Expand Down Expand Up @@ -187,7 +187,7 @@ public static Template load(String path) {
VirtualFile tf = vf.child(path);
boolean templateExists = tf.exists();
if (!templateExists && Play.usePrecompiled) {
String name = tf.relativePath().replaceAll("\\{(.*)\\}", "from_$1").replace(":", "_").replace("..", "parent");
String name = tf.relativePath().replaceAll("\\{(.*)\\}", "from_$1").replace(':', '_').replace("..", "parent");
templateExists = Play.getFile("precompiled/templates/" + name).exists();
}
if (templateExists) {
Expand Down
5 changes: 3 additions & 2 deletions framework/src/play/utils/OrderSafeProperties.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package play.utils;


import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;

Expand All @@ -10,6 +9,8 @@
import java.io.InputStream;
import java.util.*;

import static java.nio.charset.StandardCharsets.ISO_8859_1;

/**
* Custom impl of java.util.properties that preserves the key-order from the file
* and that reads the properties-file in utf-8
Expand Down Expand Up @@ -39,7 +40,7 @@ public void load(InputStream inputStream) throws IOException {
String escapedLine = StringEscapeUtils.escapeJava( line ) + "\n";
// remove escaped backslashes
escapedLine = escapedLine.replaceAll("\\\\\\\\","\\\\");
out.write( escapedLine.getBytes("iso-8859-1"));
out.write( escapedLine.getBytes(ISO_8859_1));
}

// read properties-file with regular java.util.Properties impl
Expand Down