Skip to content

Commit 0a78f70

Browse files
authored
Miscellaneous code cleanup (#286)
1 parent 37ecb8b commit 0a78f70

File tree

94 files changed

+224
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+224
-348
lines changed

core/src/main/java/org/kohsuke/stapler/AcceptHeader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ of this software and associated documentation files (the "Software"), to deal
5757
* @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1">definition of Accept header</a>
5858
*/
5959
public final class AcceptHeader {
60-
private final List<Atom> atoms = new ArrayList<Atom>();
60+
private final List<Atom> atoms = new ArrayList<>();
6161
private final String ranges;
6262

6363
/**
@@ -78,7 +78,7 @@ public AcceptHeader(String ranges) {
7878
protected static class Atom {
7979
private final String major;
8080
private final String minor;
81-
private final Map<String, String> params = new HashMap<String, String>();
81+
private final Map<String, String> params = new HashMap<>();
8282

8383
private final float q;
8484

core/src/main/java/org/kohsuke/stapler/AnnotationHandler.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
import javax.servlet.ServletException;
2929
import java.lang.annotation.Annotation;
30-
import java.util.HashMap;
31-
import java.util.Map;
3230
import java.util.concurrent.ConcurrentHashMap;
3331
import java.util.concurrent.ConcurrentMap;
3432

@@ -75,9 +73,7 @@ static Object handle(StaplerRequest request, Annotation[] annotations, String pa
7573
if (ip!=null) {
7674
try {
7775
h = ip.value().newInstance();
78-
} catch (InstantiationException e) {
79-
throw new ServletException("Failed to instantiate parameter injector for "+at,e);
80-
} catch (IllegalAccessException e) {
76+
} catch (InstantiationException | IllegalAccessException e) {
8177
throw new ServletException("Failed to instantiate parameter injector for "+at,e);
8278
}
8379
} else {
@@ -94,7 +90,7 @@ static Object handle(StaplerRequest request, Annotation[] annotations, String pa
9490
return null; // probably we should report an error
9591
}
9692

97-
private static final ConcurrentMap<Class<? extends Annotation>,AnnotationHandler> HANDLERS = new ConcurrentHashMap<Class<? extends Annotation>, AnnotationHandler>();
93+
private static final ConcurrentMap<Class<? extends Annotation>,AnnotationHandler> HANDLERS = new ConcurrentHashMap<>();
9894

9995
private static final AnnotationHandler NOT_HANDLER = new AnnotationHandler() {
10096
@Override

core/src/main/java/org/kohsuke/stapler/ClassDescriptor.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,18 @@ public ClassDescriptor(Class clazz, Class... wrappers) {
8484
this.fields = clazz.getFields();
8585

8686
// instance methods
87-
List<MethodMirror> methods = new ArrayList<MethodMirror>();
88-
findMethods(clazz,clazz,methods,new HashSet<Class>());
87+
List<MethodMirror> methods = new ArrayList<>();
88+
findMethods(clazz,clazz,methods,new HashSet<>());
8989

9090
// organize them into groups
9191
Map<Signature,List<Method>> groups = new LinkedHashMap<>();
9292
for (MethodMirror m : methods) {
93-
List<Method> v = groups.get(m.sig);
94-
if (v==null) groups.put(m.sig, v=new ArrayList<Method>());
93+
List<Method> v = groups.computeIfAbsent(m.sig, unused -> new ArrayList<>());
9594
v.add(m.method);
9695
}
9796

9897
// build functions from groups
99-
List<Function> functions = new ArrayList<Function>();
98+
List<Function> functions = new ArrayList<>();
10099
for (List<Method> m : groups.values()) {
101100
if (m.size()==1) {
102101
Method one = m.get(0);
@@ -332,7 +331,7 @@ private static String[] loadParametersFromAsm(final Method m) throws IOException
332331
URL clazz = c.getClassLoader().getResource(c.getName().replace('.', '/') + ".class");
333332
if (clazz==null) return null;
334333

335-
final TreeMap<Integer,String> localVars = new TreeMap<Integer,String>();
334+
final TreeMap<Integer,String> localVars = new TreeMap<>();
336335
ClassReader r = new ClassReader(clazz.openStream());
337336
r.accept(new ClassVisitor(Opcodes.ASM9) {
338337
final String md = Type.getMethodDescriptor(m);
@@ -370,9 +369,8 @@ private static String[] loadParametersFromAsm(final Constructor m) throws IOExce
370369
URL clazz = c.getClassLoader().getResource(c.getName().replace('.', '/') + ".class");
371370
if (clazz==null) return null;
372371

373-
final TreeMap<Integer,String> localVars = new TreeMap<Integer,String>();
374-
InputStream is = clazz.openStream();
375-
try {
372+
final TreeMap<Integer,String> localVars = new TreeMap<>();
373+
try (InputStream is = clazz.openStream()) {
376374
ClassReader r = new ClassReader(is);
377375
r.accept(new ClassVisitor(Opcodes.ASM9) {
378376
final String md = getConstructorDescriptor(m);
@@ -388,8 +386,6 @@ public MethodVisitor visitMethod(int access, String methodName, String desc, Str
388386
return null; // ignore this method
389387
}
390388
}, 0);
391-
} finally {
392-
is.close();
393389
}
394390

395391
// Indexes may not be sequential, but first set of local variables are method params

core/src/main/java/org/kohsuke/stapler/EvaluationTrace.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @author Kohsuke Kawaguchi
4141
*/
4242
public class EvaluationTrace {
43-
private final List<String> traces = new ArrayList<String>();
43+
private final List<String> traces = new ArrayList<>();
4444

4545
private static final Logger LOGGER = Logger.getLogger(EvaluationTrace.class.getName());
4646

core/src/main/java/org/kohsuke/stapler/Facet.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public static List<Facet> discover(ClassLoader cl) {
8282
}
8383

8484
public static <T> List<T> discoverExtensions(Class<T> type, ClassLoader... cls) {
85-
List<T> r = new ArrayList<T>();
86-
Set<String> classNames = new HashSet<String>();
85+
List<T> r = new ArrayList<>();
86+
Set<String> classNames = new HashSet<>();
8787

8888
for (ClassLoader cl : cls) {
8989
ClassLoaders classLoaders = new ClassLoaders();

core/src/main/java/org/kohsuke/stapler/ForwardToView.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
public class ForwardToView extends RuntimeException implements HttpResponse {
4242
@SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "As an HttpResponse, this is not actually serialized.")
4343
private final DispatcherFactory factory;
44-
private final Map<String,Object> attributes = new HashMap<String, Object>();
44+
private final Map<String,Object> attributes = new HashMap<>();
4545

4646
private interface DispatcherFactory {
4747
RequestDispatcher get(StaplerRequest req) throws IOException;

core/src/main/java/org/kohsuke/stapler/FunctionList.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public FunctionList(Collection<Function> functions) {
4343
}
4444

4545
/* internal */ FunctionList filter(Filter f) {
46-
List<Function> r = new ArrayList<Function>();
46+
List<Function> r = new ArrayList<>();
4747
for (Function m : functions)
4848
if (f.keep(m))
4949
r.add(m);
@@ -63,7 +63,7 @@ public int size() {
6363
* Compute set unions of two lists.
6464
*/
6565
public FunctionList union(FunctionList that) {
66-
Set<Function> combined = new LinkedHashSet<Function>();
66+
Set<Function> combined = new LinkedHashSet<>();
6767
combined.addAll(Arrays.asList(this.functions));
6868
combined.addAll(Arrays.asList(that.functions));
6969
return new FunctionList(combined);

core/src/main/java/org/kohsuke/stapler/HttpResponseRenderer.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,7 @@ protected boolean handleHttpResponse(StaplerRequest req, StaplerResponse rsp, Ob
122122
HttpResponse r = (HttpResponse) response;
123123
try {
124124
r.generateResponse(req,rsp,node);
125-
} catch (IOException e) {
126-
if (!handleHttpResponse(req,rsp,node,e))
127-
throw e;
128-
} catch (RuntimeException e) {
129-
if (!handleHttpResponse(req,rsp,node,e))
130-
throw e;
131-
} catch (ServletException e) {
125+
} catch (IOException | ServletException | RuntimeException e) {
132126
if (!handleHttpResponse(req,rsp,node,e))
133127
throw e;
134128
}

core/src/main/java/org/kohsuke/stapler/MetaClass.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ private List<Function> minus(FunctionList a, FunctionList b){
575575
*/
576576
public SingleLinkedList<MethodRef> getPostConstructMethods() {
577577
if (postConstructMethods ==null) {
578-
SingleLinkedList<MethodRef> l = baseClass==null ? SingleLinkedList.<MethodRef>empty() : baseClass.getPostConstructMethods();
578+
SingleLinkedList<MethodRef> l = baseClass==null ? SingleLinkedList.empty() : baseClass.getPostConstructMethods();
579579

580580
for (MethodRef mr : klass.getDeclaredMethods()) {
581581
if (mr.hasAnnotation(PostConstruct.class)) {

core/src/main/java/org/kohsuke/stapler/MetaClassLoader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static MetaClassLoader get(ClassLoader cl) {
7272
*
7373
* Note that this permanently holds a strong reference to its key, i.e. is a memory leak.
7474
*/
75-
private static final Map<ClassLoader,MetaClassLoader> classMap = new HashMap<ClassLoader,MetaClassLoader>();
75+
private static final Map<ClassLoader,MetaClassLoader> classMap = new HashMap<>();
7676

7777
static {
7878
debugLoader = createDebugLoader();

core/src/main/java/org/kohsuke/stapler/ReflectionUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static Object getVmDefaultValueFor(Class<?> type) {
4242
return defaultPrimitiveValue.get(type);
4343
}
4444

45-
private static final Map<Class,Object> defaultPrimitiveValue = new HashMap<Class, Object>();
45+
private static final Map<Class,Object> defaultPrimitiveValue = new HashMap<>();
4646
static {
4747
defaultPrimitiveValue.put(boolean.class,false);
4848
defaultPrimitiveValue.put(int.class,0);
@@ -59,7 +59,7 @@ public static Annotation[] union(Annotation[] a, Annotation[] b) {
5959
if (b.length==0) return a;
6060

6161
// slow path
62-
List<Annotation> combined = new ArrayList<Annotation>(a.length+b.length);
62+
List<Annotation> combined = new ArrayList<>(a.length+b.length);
6363
combined.addAll(Arrays.asList(a));
6464

6565
OUTER:
@@ -74,7 +74,7 @@ public static Annotation[] union(Annotation[] a, Annotation[] b) {
7474
combined.add(x);
7575
}
7676

77-
return combined.toArray(new Annotation[combined.size()]);
77+
return combined.toArray(new Annotation[0]);
7878
}
7979

8080
}

core/src/main/java/org/kohsuke/stapler/RequestImpl.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public RequestImpl(Stapler stapler, HttpServletRequest request, List<AncestorImp
135135
super(request);
136136
this.stapler = stapler;
137137
this.ancestors = ancestors;
138-
this.ancestorsView = Collections.<Ancestor>unmodifiableList(ancestors);
138+
this.ancestorsView = Collections.unmodifiableList(ancestors);
139139
this.tokens = tokens;
140140
this.originalRequestURI = request.getRequestURI();
141141
}
@@ -206,7 +206,7 @@ public Enumeration getParameterNames() {
206206
return super.getParameterNames();
207207
}
208208

209-
List<String> paramNames = Collections.<String>list(super.getParameterNames());
209+
List<String> paramNames = Collections.list(super.getParameterNames());
210210
paramNames.addAll(data.keySet());
211211

212212
return Collections.enumeration(paramNames);
@@ -347,9 +347,7 @@ public boolean checkIfModified(long lastModified, StaplerResponse rsp, long expi
347347
rsp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
348348
return true;
349349
}
350-
} catch (NumberFormatException e) {
351-
// just ignore and serve the content
352-
} catch (ParseException e) {
350+
} catch (NumberFormatException | ParseException e) {
353351
// just ignore and serve the content
354352
}
355353
}
@@ -406,7 +404,7 @@ public void bindParameters(Object bean, String prefix) {
406404

407405
public <T>
408406
List<T> bindParametersToList(Class<T> type, String prefix) {
409-
List<T> r = new ArrayList<T>();
407+
List<T> r = new ArrayList<>();
410408

411409
int len = Integer.MAX_VALUE;
412410

@@ -518,7 +516,7 @@ public void bindJSON(Object bean, JSONObject src) {
518516
}
519517

520518
public <T> List<T> bindJSONToList(Class<T> type, Object src) {
521-
ArrayList<T> r = new ArrayList<T>();
519+
ArrayList<T> r = new ArrayList<>();
522520
if (src instanceof JSONObject) {
523521
JSONObject j = (JSONObject) src;
524522
r.add(bindJSON(type,j));
@@ -858,9 +856,7 @@ private <T> T injectSetters(T r, JSONObject j, Collection<String> exclusions) {
858856

859857
// only invoking public methods for security reasons
860858
wm.invoke(r, bindJSON(wm.getGenericParameterTypes()[0], pt[0], j.get(key)));
861-
} catch (IllegalAccessException e) {
862-
LOGGER.log(WARNING, "Cannot access property " + key + " of " + r.getClass(), e);
863-
} catch (InvocationTargetException e) {
859+
} catch (IllegalAccessException | InvocationTargetException e) {
864860
LOGGER.log(WARNING, "Cannot access property " + key + " of " + r.getClass(), e);
865861
}
866862
}
@@ -975,8 +971,8 @@ private static void copyProperty(Object bean, String name, Object value) throws
975971
private void parseMultipartFormData() throws ServletException {
976972
if(parsedFormData!=null) return;
977973

978-
parsedFormData = new HashMap<String,FileItem>();
979-
parsedFormDataFormFields = new HashMap<String, String>();
974+
parsedFormData = new HashMap<>();
975+
parsedFormDataFormFields = new HashMap<>();
980976
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
981977
try {
982978
for( FileItem fi : (List<FileItem>)upload.parseRequest(this) ) {

core/src/main/java/org/kohsuke/stapler/ResponseImpl.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.io.Writer;
3333
import java.net.HttpURLConnection;
3434
import java.net.URL;
35-
import java.net.URLEncoder;
3635
import java.nio.charset.StandardCharsets;
3736
import java.util.Enumeration;
3837
import java.util.List;
@@ -403,7 +402,7 @@ private void copyAndClose(InputStream in, OutputStream out) throws IOException {
403402
StringBuilder out = new StringBuilder(s.length());
404403

405404
ByteArrayOutputStream buf = new ByteArrayOutputStream();
406-
OutputStreamWriter w = new OutputStreamWriter(buf,"UTF-8");
405+
OutputStreamWriter w = new OutputStreamWriter(buf,StandardCharsets.UTF_8);
407406

408407
for (int i = 0; i < s.length(); i++) {
409408
int c = (int) s.charAt(i);

core/src/main/java/org/kohsuke/stapler/SingleLinkedList.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public SingleLinkedList(T head, SingleLinkedList<T> tail) {
2323
* Creates a new list by adding a new element as the head.
2424
*/
2525
public SingleLinkedList<T> grow(T item) {
26-
return new SingleLinkedList<T>(item,this);
26+
return new SingleLinkedList<>(item,this);
2727
}
2828

2929
@Override

core/src/main/java/org/kohsuke/stapler/Stapler.java

+11-15
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public void buildResourcePaths() {
148148
return;
149149
}
150150

151-
Map<String,URL> paths = new HashMap<String,URL>();
152-
Stack<String> q = new Stack<String>();
151+
Map<String,URL> paths = new HashMap<>();
152+
Stack<String> q = new Stack<>();
153153
q.push("/");
154154
while (!q.isEmpty()) {
155155
String dir = q.pop();
@@ -576,7 +576,7 @@ boolean serveStaticResource(HttpServletRequest req, StaplerResponse rsp, InputSt
576576
// Need to duplicate this logic from ResponseImpl.getCompressedOutputStream,
577577
// since we want to set content length if we are not using encoding.
578578
String acceptEncoding = req.getHeader("Accept-Encoding");
579-
if (acceptEncoding != null && acceptEncoding.indexOf("gzip") != -1) {
579+
if (acceptEncoding != null && acceptEncoding.contains("gzip")) {
580580
// with gzip compression, Content-Length header needs to indicate the # of bytes after compression,
581581
// so we can't compute it upfront.
582582
out = rsp.getCompressedOutputStream(req);
@@ -682,7 +682,7 @@ private String getMimeType(String fileName) {
682682
* Performs stapler processing on the given root object and request URL.
683683
*/
684684
public void invoke(HttpServletRequest req, HttpServletResponse rsp, Object root, String url) throws IOException, ServletException {
685-
RequestImpl sreq = new RequestImpl(this, req, new ArrayList<AncestorImpl>(), new TokenList(url));
685+
RequestImpl sreq = new RequestImpl(this, req, new ArrayList<>(), new TokenList(url));
686686
RequestImpl oreq = CURRENT_REQUEST.get();
687687
CURRENT_REQUEST.set(sreq);
688688

@@ -1019,24 +1019,22 @@ public static Stapler getCurrent() {
10191019
* HTTP date format. Notice that {@link SimpleDateFormat} is thread unsafe.
10201020
*/
10211021
static final ThreadLocal<SimpleDateFormat> HTTP_DATE_FORMAT =
1022-
new ThreadLocal<SimpleDateFormat>() {
1023-
protected @Override SimpleDateFormat initialValue() {
1022+
ThreadLocal.withInitial(() -> {
10241023
// RFC1945 section 3.3 Date/Time Formats states that timezones must be in GMT
10251024
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
10261025
format.setTimeZone(TimeZone.getTimeZone("GMT"));
10271026
return format;
1028-
}
1029-
};
1027+
});
10301028

1031-
/*package*/ static ThreadLocal<RequestImpl> CURRENT_REQUEST = new ThreadLocal<RequestImpl>();
1032-
/*package*/ static ThreadLocal<ResponseImpl> CURRENT_RESPONSE = new ThreadLocal<ResponseImpl>();
1029+
/*package*/ static ThreadLocal<RequestImpl> CURRENT_REQUEST = new ThreadLocal<>();
1030+
/*package*/ static ThreadLocal<ResponseImpl> CURRENT_RESPONSE = new ThreadLocal<>();
10331031

10341032
private static final Logger LOGGER = Logger.getLogger(Stapler.class.getName());
10351033

10361034
/**
10371035
* Extensions that look like text files.
10381036
*/
1039-
private static final Set<String> TEXT_FILES = new HashSet<String>(Arrays.asList(
1037+
private static final Set<String> TEXT_FILES = new HashSet<>(Arrays.asList(
10401038
"css","js","html","txt","java","htm","c","cpp","h","rb","pl","py","xml","json"
10411039
));
10421040

@@ -1052,7 +1050,7 @@ public static Stapler getCurrent() {
10521050
* which is a security risk. Fix that by normalizing them.
10531051
*/
10541052
static String canonicalPath(String path) {
1055-
List<String> r = new ArrayList<String>(Arrays.asList(path.split("/+")));
1053+
List<String> r = new ArrayList<>(Arrays.asList(path.split("/+")));
10561054
for (int i=0; i<r.size(); ) {
10571055
if (r.get(i).length()==0 || r.get(i).equals(".")) {
10581056
// empty token occurs for example, "".split("/+") is [""]
@@ -1146,9 +1144,7 @@ public FileItem convert(Class type, Object value) {
11461144
if(value==null) return null;
11471145
try {
11481146
return Stapler.getCurrentRequest().getFileItem(value.toString());
1149-
} catch (ServletException e) {
1150-
throw new ConversionException(e);
1151-
} catch (IOException e) {
1147+
} catch (ServletException | IOException e) {
11521148
throw new ConversionException(e);
11531149
}
11541150
}

core/src/main/java/org/kohsuke/stapler/StaticViewFacet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
// @MetaInfServices - this facet needs to be manually configured
2323
public class StaticViewFacet extends Facet {
24-
private final List<String> allowedExtensions = new ArrayList<String>();
24+
private final List<String> allowedExtensions = new ArrayList<>();
2525

2626
public StaticViewFacet(String... allowedExtensions) {
2727
this(Arrays.asList(allowedExtensions));

core/src/main/java/org/kohsuke/stapler/TearOffSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public final <T> T loadTearOff(Class<T> t) {
7474

7575
public synchronized <T> void setTearOff(Class<T> type, T instance) {
7676
Map<Class,Object> m = tearOffs;
77-
Map<Class,Object> r = m!=null ? new HashMap<Class, Object>(tearOffs) : new HashMap<Class,Object>();
77+
Map<Class,Object> r = m!=null ? new HashMap<>(tearOffs) : new HashMap<>();
7878
r.put(type,instance);
7979
tearOffs = r;
8080
}

0 commit comments

Comments
 (0)