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

Format code with Spotless #545

Merged
merged 1 commit into from
May 17, 2024
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
63 changes: 40 additions & 23 deletions core/src/main/java/org/kohsuke/stapler/AbstractTearOff.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* ClassLoader tear-off.
* @author Kohsuke Kawaguchi
*/
public abstract class AbstractTearOff<CLT,S,E extends Exception> extends CachingScriptLoader<S,E> {
public abstract class AbstractTearOff<CLT, S, E extends Exception> extends CachingScriptLoader<S, E> {

private static final Logger LOGGER = Logger.getLogger(AbstractTearOff.class.getName());

Expand All @@ -54,19 +54,22 @@ public abstract class AbstractTearOff<CLT,S,E extends Exception> extends Caching
private static final class ExpirableCacheHit<S> {
private final long timestamp;
private final Reference<S> script;

ExpirableCacheHit(long timestamp, S script) {
this.timestamp = timestamp;
this.script = new SoftReference<>(script);
}
}

private final Map<String, ExpirableCacheHit<S>> cachedScripts = new ConcurrentHashMap<>();

protected AbstractTearOff(MetaClass owner, Class<CLT> cltClass) {
this.owner = owner;
if(owner.classLoader!=null)
if (owner.classLoader != null) {
classLoader = owner.classLoader.loadTearOff(cltClass);
else
} else {
classLoader = null;
}
}

protected final WebApp getWebApp() {
Expand All @@ -93,23 +96,26 @@ protected boolean hasAllowedExtension(String name) {
* from its base types.
*/
public S resolveScript(String name) throws E {
if (name.lastIndexOf('.')<=name.lastIndexOf('/')) // no file extension provided
if (name.lastIndexOf('.') <= name.lastIndexOf('/')) { // no file extension provided
name += getDefaultScriptExtension();
if (!hasAllowedExtension(name))
}
if (!hasAllowedExtension(name)) {
// for multiple Facets to co-exist peacefully, we need to be able to determine
// which Facet is responsible for a given view just from the file name
return null;
}

URL res = getResource(name);
if(res==null) {
if (res == null) {
// look for 'defaults' file
int dot = name.lastIndexOf('.');
// foo/bar.groovy -> foo/bar.default.groovy
// but don't do foo.bar/test -> foo.default.bar/test
// as of 2010/9, this behaviour is considered deprecated, but left here for backward compatibility.
// we need a better way to refer to the resource of the same name in the base type.
if(name.lastIndexOf('/')<dot)
if (name.lastIndexOf('/') < dot) {
res = getResource(name.substring(0, dot) + ".default" + name.substring(dot));
}
}
if (res != null) {
if (MetaClass.NO_CACHE) {
Expand All @@ -131,7 +137,9 @@ public S resolveScript(String name) throws E {
try {
script = parseScript(res);
} finally {
LOGGER.log(Level.FINE, "cache miss; took {0}ms to parse {1}", new Object[] {(System.nanoTime() - start) / 1_000_000, res});
LOGGER.log(Level.FINE, "cache miss; took {0}ms to parse {1}", new Object[] {
(System.nanoTime() - start) / 1_000_000, res
});
}
} else {
LOGGER.log(Level.FINE, "cache miss on {0}", res);
Expand Down Expand Up @@ -166,7 +174,10 @@ public S resolveScript(String name) throws E {
try {
return parseScript(res);
} finally {
LOGGER.log(Level.FINE, "standard CachingScriptLoader logic applies to {0} parsed in {1}ms", new Object[] {res, (System.nanoTime() - start) / 1_000_000});
LOGGER.log(
Level.FINE,
"standard CachingScriptLoader logic applies to {0} parsed in {1}ms",
new Object[] {res, (System.nanoTime() - start) / 1_000_000});
}
} else {
return parseScript(res);
Expand All @@ -178,21 +189,24 @@ public S resolveScript(String name) throws E {
}

private static final Pattern JAR_URL = Pattern.compile("jar:(file:.+)!/.*");
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "Files are read from approved plugins, not from user input.")

@SuppressFBWarnings(
value = "PATH_TRAVERSAL_IN",
justification = "Files are read from approved plugins, not from user input.")
private static File fileOf(URL res) {
try {
switch (res.getProtocol()) {
case "file":
return new File(res.toURI());
case "jar":
Matcher m = JAR_URL.matcher(res.toString());
if (m.matches()) {
return new File(new URI(m.group(1)));
} else {
case "file":
return new File(res.toURI());
case "jar":
Matcher m = JAR_URL.matcher(res.toString());
if (m.matches()) {
return new File(new URI(m.group(1)));
} else {
return null;
}
default:
return null;
}
default:
return null;
}
} catch (URISyntaxException | IllegalArgumentException x) {
return null; // caching is a best effort
Expand All @@ -202,11 +216,14 @@ private static File fileOf(URL res) {
@Override
protected final S loadScript(String name) throws E {
S s = resolveScript(name);
if (s!=null) return s;
if (s != null) {
return s;
}

// not found on this class, delegate to the parent
if(owner.baseClass!=null)
return ((AbstractTearOff<CLT,S,E>)owner.baseClass.loadTearOff(getClass())).findScript(name);
if (owner.baseClass != null) {
return ((AbstractTearOff<CLT, S, E>) owner.baseClass.loadTearOff(getClass())).findScript(name);
}

return null;
}
Expand Down
34 changes: 21 additions & 13 deletions core/src/main/java/org/kohsuke/stapler/AcceptHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public final class AcceptHeader {
*/
public AcceptHeader(String ranges) {
this.ranges = ranges;
for (String r : StringUtils.split(ranges, ','))
for (String r : StringUtils.split(ranges, ',')) {
atoms.add(new Atom(r));
}
}

/**
Expand All @@ -83,9 +84,10 @@ protected static class Atom {

@Override
public String toString() {
StringBuilder s = new StringBuilder(major +'/'+ minor);
for (Map.Entry<String, String> k : params.entrySet())
StringBuilder s = new StringBuilder(major + '/' + minor);
for (Map.Entry<String, String> k : params.entrySet()) {
s.append(";").append(k.getKey()).append("=").append(k.getValue());
}
return s.toString();
}

Expand All @@ -98,22 +100,25 @@ protected Atom(String range) {
for (int i = 1; i < parts.length; ++i) {
String p = parts[i];
String[] subParts = StringUtils.split(p, '=');
if (subParts.length == 2)
if (subParts.length == 2) {
params.put(subParts[0].trim(), subParts[1].trim());
}
}
String fullType = parts[0].trim();

// Java URLConnection class sends an Accept header that includes a
// single "*" - Turn it into a legal wildcard.
if (fullType.equals("*"))
if (fullType.equals("*")) {
fullType = "*/*";
}
String[] types = StringUtils.split(fullType, "/");
major = types[0].trim();
minor = types[1].trim();

float q = NumberUtils.toFloat(params.get("q"), 1);
if (q < 0 || q > 1)
if (q < 0 || q > 1) {
q = 1;
}
this.q = q;

params.remove("q"); // normalize this away as this gets in the fitting
Expand All @@ -127,8 +132,9 @@ protected Atom(String range) {
* than "text/*", which still fits better than "* /*"
*/
private int fit(Atom that) {
if (!wildcardMatch(that.major, this.major) || !wildcardMatch(that.minor, this.minor))
if (!wildcardMatch(that.major, this.major) || !wildcardMatch(that.minor, this.minor)) {
return -1;
}

int fitness;
fitness = this.major.equals(that.major) ? 10000 : 0;
Expand Down Expand Up @@ -159,7 +165,7 @@ private static boolean wildcardMatch(String a, String b) {
Atom best = null;
for (Atom a : atoms) {
int f = a.fit(target);
if (f>bestFitness) {
if (f > bestFitness) {
best = a;
bestFitness = f;
}
Expand Down Expand Up @@ -194,15 +200,17 @@ public String select(Iterable<String> supported) {

for (String s : supported) {
Atom a = match(s);
if (a!= null && a.q > bestQ) {
if (a != null && a.q > bestQ) {
bestQ = a.q;
best = s;
}
}

if (best==null)
throw HttpResponses.error(HttpServletResponse.SC_NOT_ACCEPTABLE,
"Requested MIME types '" + ranges + "' didn't match any of the available options "+supported);
if (best == null) {
throw HttpResponses.error(
HttpServletResponse.SC_NOT_ACCEPTABLE,
"Requested MIME types '" + ranges + "' didn't match any of the available options " + supported);
}
return best;
}

Expand All @@ -212,7 +220,7 @@ public String select(String... supported) {

@Override
public String toString() {
return super.toString()+"["+ranges+"]";
return super.toString() + "[" + ranges + "]";
}

// this performs databinding for @Header parameter injection
Expand Down
39 changes: 24 additions & 15 deletions core/src/main/java/org/kohsuke/stapler/AncestorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@ public Object getObject() {
@Override
public String getUrl() {
StringBuilder buf = new StringBuilder(contextPath);
for( int i=0; i<index; i++ ) {
for (int i = 0; i < index; i++) {
buf.append('/');
buf.append(tokens[i]);
}

return buf.toString();
}

@Override
public String getRestOfUrl() {
StringBuilder buf = new StringBuilder();
for( int i=index; i<tokens.length; i++ ) {
if (buf.length()>0) buf.append('/');
for (int i = index; i < tokens.length; i++) {
if (buf.length() > 0) {
buf.append('/');
}
buf.append(tokens[i]);
}

Expand All @@ -90,8 +92,9 @@ public String getFullUrl() {
buf.append(req.getScheme());
buf.append("://");
buf.append(req.getServerName());
if(req.getServerPort()!=80)
if (req.getServerPort() != 80) {
buf.append(':').append(req.getServerPort());
}
buf.append(getUrl());

return buf.toString();
Expand All @@ -100,33 +103,39 @@ public String getFullUrl() {
@Override
public String getRelativePath() {
StringBuilder buf = new StringBuilder();
for( int i=index+(endsWithSlash?0:1); i<tokens.length; i++ ) {
if(buf.length()>0) buf.append('/');
for (int i = index + (endsWithSlash ? 0 : 1); i < tokens.length; i++) {
if (buf.length() > 0) {
buf.append('/');
}
buf.append("..");
}
if(buf.length()==0) buf.append('.');
if (buf.length() == 0) {
buf.append('.');
}
return buf.toString();
}

@Override
public String getNextToken(int n) {
return tokens[index+n];
return tokens[index + n];
}

@Override
public Ancestor getPrev() {
if(listIndex==0)
if (listIndex == 0) {
return null;
else
return owner.get(listIndex-1);
} else {
return owner.get(listIndex - 1);
}
}

@Override
public Ancestor getNext() {
if(listIndex==owner.size()-1)
if (listIndex == owner.size() - 1) {
return null;
else
return owner.get(listIndex+1);
} else {
return owner.get(listIndex + 1);
}
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/kohsuke/stapler/AncestorInPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
public @interface AncestorInPath {
class HandlerImpl extends AnnotationHandler<AncestorInPath> {
@Override
public Object parse(StaplerRequest request, AncestorInPath a, Class type, String parameterName) throws ServletException {
public Object parse(StaplerRequest request, AncestorInPath a, Class type, String parameterName)
throws ServletException {
return request.findAncestorObject(type);
}
}
Expand Down
18 changes: 11 additions & 7 deletions core/src/main/java/org/kohsuke/stapler/AnnotationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,22 @@ public abstract class AnnotationHandler<T extends Annotation> {
*/
protected final Object convert(Class targetType, String value) {
Converter converter = Stapler.lookupConverter(targetType);
if (converter==null)
throw new IllegalArgumentException("Unable to convert to "+targetType);
if (converter == null) {
throw new IllegalArgumentException("Unable to convert to " + targetType);
}

return converter.convert(targetType,value);
return converter.convert(targetType, value);
}

static Object handle(StaplerRequest request, Annotation[] annotations, String parameterName, Class targetType) throws ServletException {
static Object handle(StaplerRequest request, Annotation[] annotations, String parameterName, Class targetType)
throws ServletException {
for (Annotation a : annotations) {
Class<? extends Annotation> at = a.annotationType();
AnnotationHandler h = HANDLERS.get(at);
if (h==NOT_HANDLER)
if (h == NOT_HANDLER) {
continue;
return h.parse(request,a,targetType,parameterName);
}
return h.parse(request, a, targetType, parameterName);
}

return null; // probably we should report an error
Expand All @@ -91,7 +94,8 @@ protected AnnotationHandler computeValue(Class<?> at) {

private static final AnnotationHandler NOT_HANDLER = new AnnotationHandler() {
@Override
public Object parse(StaplerRequest request, Annotation a, Class type, String parameterName) throws ServletException {
public Object parse(StaplerRequest request, Annotation a, Class type, String parameterName)
throws ServletException {
return null;
}
};
Expand Down
Loading