Skip to content

Commit

Permalink
Use Java 11 language features where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Jun 25, 2022
1 parent 0128d8e commit 6f7cb7e
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 38 deletions.
Expand Up @@ -36,7 +36,6 @@
import org.codehaus.groovy.transform.GroovyASTTransformation;
import org.kohsuke.MetaInfServices;

import java.util.Collections;
import java.util.List;
import java.util.Set;

Expand All @@ -60,8 +59,8 @@ private void handleClasses(List<ClassNode> classNodes) {
}

// set of annotation class names to capture
private static final Set<String> CONSTRUCTOR_ANN = Collections.singleton(DataBoundConstructor.class.getName());
private static final Set<String> INJECTED_PARAMETER_ANN = Collections.singleton(InjectedParameter.class.getName());
private static final Set<String> CONSTRUCTOR_ANN = Set.of(DataBoundConstructor.class.getName());
private static final Set<String> INJECTED_PARAMETER_ANN = Set.of(InjectedParameter.class.getName());

private void handleMethods(List<MethodNode> methods) {
for (MethodNode m : methods)
Expand Down
8 changes: 2 additions & 6 deletions core/src/main/java/org/kohsuke/stapler/Stapler.java
Expand Up @@ -52,14 +52,14 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -671,11 +671,7 @@ private String getMimeType(String fileName) {
// File(String) does fs.normalize, which is really forgiving in fixing up
// malformed stuff. I couldn't make the other URL.toURI() or File(URI) work
// in all the cases that we test
try {
return new File(URLDecoder.decode(urlstr.substring(5),"UTF-8"));
} catch (UnsupportedEncodingException x) {
throw new AssertionError(x);
}
return new File(URLDecoder.decode(urlstr.substring(5), StandardCharsets.UTF_8));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/kohsuke/stapler/TokenList.java
Expand Up @@ -168,14 +168,14 @@ public static String decode(String s) {
}
} else {
if (baos.size()>0) {
buf.append(new String(baos.toByteArray(),StandardCharsets.UTF_8));
buf.append(baos.toString(StandardCharsets.UTF_8));
baos.reset();
}
buf.append(c);
}
}
if (baos.size()>0)
buf.append(new String(baos.toByteArray(),StandardCharsets.UTF_8));
buf.append(baos.toString(StandardCharsets.UTF_8));
return buf.toString();
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/kohsuke/stapler/bind/Bound.java
Expand Up @@ -30,7 +30,7 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

/**
* Handles to the object bound via {@link BoundObjectTable}.
Expand Down Expand Up @@ -71,13 +71,13 @@ public final String getProxyScript() {
for (Method m : getTarget().getClass().getMethods()) {
Collection<String> names;
if (m.getName().startsWith("js")) {
names = Collections.singleton(camelize(m.getName().substring(2)));
names = Set.of(camelize(m.getName().substring(2)));
} else {
JavaScriptMethod a = m.getAnnotation(JavaScriptMethod.class);
if (a!=null) {
names = Arrays.asList(a.name());
if (names.isEmpty())
names = Collections.singleton(m.getName());
names = Set.of(m.getName());
} else
continue;
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/kohsuke/stapler/export/Model.java
Expand Up @@ -29,7 +29,6 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -121,7 +120,7 @@ public class Model<T> {
* Gets all the exported properties.
*/
public List<Property> getProperties() {
return Collections.unmodifiableList(Arrays.asList(properties));
return List.of(properties);
}

/**
Expand Down
Expand Up @@ -12,7 +12,7 @@
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementScanner6;
import javax.lang.model.util.ElementScanner9;
import java.io.IOException;
import java.util.HashSet;
import java.util.Properties;
Expand All @@ -31,7 +31,7 @@ public class ConstructorProcessor extends AbstractProcessorImpl {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
try {
ElementScanner6<Void, Void> scanner = new ElementScanner6<Void, Void>() {
ElementScanner9<Void, Void> scanner = new ElementScanner9<Void, Void>() {
Set<Element> enclosingElementsWritten = new HashSet<>();
boolean messagePrinted;

Expand Down
Expand Up @@ -122,7 +122,7 @@ public int getContentLength() {

@Override
public Enumeration getParameterNames() {
return Collections.enumeration(Collections.singletonList("p1"));
return Collections.enumeration(List.of("p1"));
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions core/src/test/java/org/kohsuke/stapler/export/ModelTest.java
Expand Up @@ -27,7 +27,6 @@
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.junit.Assert;
Expand All @@ -53,7 +52,7 @@ public static class Impl implements GenericInterface<Integer> {
@Override
@Exported
public List<Integer> get() {
return Collections.singletonList(42);
return List.of(42);
}
}

Expand Down
Expand Up @@ -4,9 +4,9 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

Expand Down Expand Up @@ -35,7 +35,7 @@ public String getName(){

@Exported
public Collection<Job> getJobs(){
return Collections.singleton(new Job());
return Set.of(new Job());
}
}

Expand Down Expand Up @@ -65,12 +65,12 @@ public String getName() {
// @Exported(visibility = 2)
@Override
public Iterator<ParameterValue> iterator() {
return Collections.singleton(new ParameterValue()).iterator();
return Set.of(new ParameterValue()).iterator();
}

@Exported(visibility = 2)
public List<ParameterValue> getParameters(){
return Collections.singletonList(new ParameterValue());
return List.of(new ParameterValue());
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/org/kohsuke/stapler/lang/KlassTest.java
Expand Up @@ -56,7 +56,7 @@ private static final class FooClass {

public Object doDynamic(String token) {
// Just return something potentially routable
return new Integer(0);
return Integer.valueOf(0);
}
}
}
Expand Up @@ -40,7 +40,6 @@
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
Expand Down Expand Up @@ -99,7 +98,7 @@ public boolean handleIndexRequest(RequestImpl req, ResponseImpl rsp, Object node
handleIndexRequest(nodeMetaClass.loadTearOff(GroovyServerPageTearOff.class), scriptExecutor, req, rsp, node);
}

private static final Set<Class<GroovyClassTearOff>> TEAROFF_TYPES = Collections.singleton(GroovyClassTearOff.class);
private static final Set<Class<GroovyClassTearOff>> TEAROFF_TYPES = Set.of(GroovyClassTearOff.class);

private static final Set<String> EXTENSION = Collections.singleton(".groovy");
private static final Set<String> EXTENSION = Set.of(".groovy");
}
Expand Up @@ -54,7 +54,7 @@ public void testGettext() throws Exception {
GroovyClassLoaderTearOff t = mcl.getTearOff(GroovyClassLoaderTearOff.class);

Files.write(tmp, "output.write(_('localizable'))".getBytes(StandardCharsets.UTF_8));
Files.write(tmp.resolveSibling(tmp.getFileName().toString().replaceFirst("[.]groovy$", ".properties")), "localizable=Localizable".getBytes(StandardCharsets.ISO_8859_1));
Files.writeString(tmp.resolveSibling(tmp.getFileName().toString().replaceFirst("[.]groovy$", ".properties")), "localizable=Localizable", StandardCharsets.ISO_8859_1);

JellyContext context = new JellyContext();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down
Expand Up @@ -33,7 +33,6 @@
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic.Kind;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -50,7 +49,7 @@ private static class MissingViews extends HashSet<String> {}

@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton("*");
return Set.of("*");
}

@Override
Expand Down
Expand Up @@ -33,8 +33,6 @@

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;

/**
* Expression of the form "%messageName(arg1,arg2,...)" that represents
Expand Down Expand Up @@ -86,7 +84,7 @@ public InternationalizedStringExpression(ResourceBundle resourceBundle, String t
}

public List<Expression> getArguments() {
return Collections.unmodifiableList(Arrays.asList(arguments));
return List.of(arguments);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions jelly/src/main/java/org/kohsuke/stapler/jelly/JellyFacet.java
Expand Up @@ -41,7 +41,6 @@
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
Expand Down Expand Up @@ -131,7 +130,7 @@ public static void setExpressionFactory( ServletContextEvent event, ExpressionFa
@SuppressFBWarnings(value = "MS_SHOULD_BE_FINAL", justification = "Legacy switch.")
public static boolean TRACE = Boolean.getBoolean("stapler.jelly.trace");

private static final Set<Class<JellyClassTearOff>> TEAROFF_TYPES = Collections.singleton(JellyClassTearOff.class);
private static final Set<Class<JellyClassTearOff>> TEAROFF_TYPES = Set.of(JellyClassTearOff.class);

private static final Set<String> EXTENSION = Collections.singleton(".jelly");
private static final Set<String> EXTENSION = Set.of(".jelly");
}

0 comments on commit 6f7cb7e

Please sign in to comment.