Skip to content

Commit

Permalink
Miscellaneous code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Jun 13, 2024
1 parent f48d59f commit 407cd5c
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 51 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/kohsuke/stapler/AttributeKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void remove(HttpServletRequest req) {
}

private ServletContext getContext(HttpServletRequest req) {
return ((StaplerRequest) req).getServletContext();
return req.getServletContext();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private String correctTypeName(Type[] argumentTypes, int i) {
braces = braces + "[";
s = s.substring(0, s.length() - 2);
}
if (!braces.equals("")) {
if (!braces.isEmpty()) {
if (primitives.containsKey(s)) {
s = braces + primitives.get(s);
} else {
Expand Down Expand Up @@ -1036,10 +1036,9 @@ private static Type getType(final char[] buf, final int off) {
++len;
}
if (buf[off + len] == 'L') {
++len;
while (buf[off + len] != ';') {
do {
++len;
}
} while (buf[off + len] != ';');
}
return new Type(ARRAY, buf, off, len + 1);
// case 'L':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public S findScript(String name) throws E {
S s;
if (sr == null) { // never before computed
s = null;
} else if (!sr.isPresent()) { // cached as null
} else if (sr.isEmpty()) { // cached as null
return null;
} else { // cached as non-null; may or may not still have value
s = sr.get().get();
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/kohsuke/stapler/RequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public Object bindJSON(Type type, Class erasure, Object json) {
@Override
public void bindJSON(Object bean, JSONObject src) {
try {
for (String key : (Set<String>) src.keySet()) {
for (String key : src.keySet()) {
TypePair type = getPropertyType(bean, key);
if (type == null) {
continue;
Expand Down Expand Up @@ -992,7 +992,7 @@ private Object bindResolve(Object o, JSONObject src) {
private <T> T injectSetters(T r, JSONObject j, Collection<String> exclusions) {
// try to assign rest of the properties
OUTER:
for (String key : (Set<String>) j.keySet()) {
for (String key : j.keySet()) {
if (!exclusions.contains(key)) {
try {
// try field injection first
Expand Down
18 changes: 7 additions & 11 deletions core/src/main/java/org/kohsuke/stapler/export/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
Expand Down Expand Up @@ -177,18 +178,13 @@ public boolean test(@Nullable String name) {

// load
Properties p = new Properties();
InputStream is = type.getClassLoader()
.getResourceAsStream(type.getName().replace('$', '/').replace('.', '/') + ".javadoc");
if (is != null) {
try {
try {
p.load(is);
} finally {
is.close();
}
} catch (IOException e) {
throw new RuntimeException("Unable to load javadoc for " + type, e);
try (InputStream is = type.getClassLoader()
.getResourceAsStream(type.getName().replace('$', '/').replace('.', '/') + ".javadoc")) {
if (is != null) {
p.load(is);
}
} catch (IOException e) {
throw new UncheckedIOException("Unable to load javadoc for " + type, e);
}
javadoc = p;
return javadoc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,7 @@ public static long getGzipStreamSize(File file) {
if (!isGzipStream(file)) {
return file.length();
}
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(file, "r");
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
if (raf.length() <= 4) {
raf.close();
return file.length();
Expand All @@ -535,14 +533,6 @@ public static long getGzipStreamSize(File file) {
return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
} catch (IOException ex) {
return file.length();
} finally {
if (raf != null) {
try {
raf.close();
} catch (IOException e) {
// ignore
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;

/**
* {@link OutputStream} that writes to {@link Writer}
Expand Down Expand Up @@ -136,14 +134,5 @@ private void decode(boolean last) throws IOException {
}
}

private static final Charset DEFAULT_CHARSET = getDefaultCharset();

private static Charset getDefaultCharset() {
try {
String encoding = System.getProperty("file.encoding");
return Charset.forName(encoding);
} catch (UnsupportedCharsetException e) {
return StandardCharsets.UTF_8;
}
}
private static final Charset DEFAULT_CHARSET = Charset.defaultCharset();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import org.apache.commons.io.output.NullOutputStream;
import org.junit.Ignore;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand Down Expand Up @@ -86,7 +85,7 @@ public void writeLogToWithLargeFile() throws Exception {

LargeText t = new LargeText(path.toFile(), StandardCharsets.US_ASCII, true);

try (OutputStream os = NullOutputStream.NULL_OUTPUT_STREAM) {
try (OutputStream os = OutputStream.nullOutputStream()) {
long writenCount = t.writeLogTo(0, os);

assertEquals(size, writenCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void run(JellyBuilder builder) {
.filter(method -> method.getName().equals("gettext"))
.map(CachedMethod::find)
.map(method -> new AliasMetaMethod(method.getDeclaringClass(), method.getCachedMethod(), "_"))
.forEach(method -> metaClass.addMetaMethod(method));
.forEach(metaClass::addMetaMethod);
metaClass.initialize();
gcs.setMetaClass(metaClass);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public void testFoo() throws IOException, JellyTagException {
MetaClassLoader mcl = webApp.getMetaClass(Foo.class).classLoader;
GroovyClassLoaderTearOff t = mcl.loadTearOff(GroovyClassLoaderTearOff.class);

Files.write(tmp, "context.setVariable('x',1)".getBytes(StandardCharsets.UTF_8));
Files.writeString(tmp, "context.setVariable('x',1)");

JellyContext context = new JellyContext();
XMLOutput w = XMLOutput.createXMLOutput(System.out);
t.parse(tmp.toUri().toURL()).run(context, w);
assertEquals(1, context.getVariable("x"));

// reload different content in the same URL, make sure new class gets loaded
Files.write(tmp, "context.setVariable('x',2)".getBytes(StandardCharsets.UTF_8));
Files.writeString(tmp, "context.setVariable('x',2)");
t.parse(tmp.toUri().toURL()).run(context, w);
assertEquals(2, context.getVariable("x"));
} finally {
Expand All @@ -51,7 +51,7 @@ public void testGettext() throws Exception {
MetaClassLoader mcl = webApp.getMetaClass(Foo.class).classLoader;
GroovyClassLoaderTearOff t = mcl.loadTearOff(GroovyClassLoaderTearOff.class);

Files.write(tmp, "output.write(_('localizable'))".getBytes(StandardCharsets.UTF_8));
Files.writeString(tmp, "output.write(_('localizable'))");
Files.writeString(
tmp.resolveSibling(tmp.getFileName().toString().replaceFirst("[.]groovy$", ".properties")),
"localizable=Localizable",
Expand All @@ -78,10 +78,9 @@ public void testTimeZone() throws IOException, JellyTagException {
MetaClassLoader mcl = webApp.getMetaClass(Foo.class).classLoader;
GroovyClassLoaderTearOff t = mcl.loadTearOff(GroovyClassLoaderTearOff.class);

Files.write(
Files.writeString(
tmp,
"def tz = java.util.TimeZone.getDefault()\ncontext.setVariable('x', (tz.rawOffset + tz.DSTSavings) / 3600000)"
.getBytes(StandardCharsets.UTF_8));
"def tz = java.util.TimeZone.getDefault()\ncontext.setVariable('x', (tz.rawOffset + tz.DSTSavings) / 3600000)");

JellyContext context = new JellyContext();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down

0 comments on commit 407cd5c

Please sign in to comment.