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

[Hacktoberfest] Code cleanup #413

Merged
merged 20 commits into from
Oct 16, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,8 @@ public final synchronized void setup(final Channel channel, final CommandReceive
@Override
public final void write(Command cmd, boolean last) throws IOException {
ByteBufferQueueOutputStream bqos = new ByteBufferQueueOutputStream(sendStaging);
ObjectOutputStream oos = AnonymousClassWarnings.checkingObjectOutputStream(bqos);
try {
try (ObjectOutputStream oos = AnonymousClassWarnings.checkingObjectOutputStream(bqos)) {
cmd.writeTo(channel, oos);
} finally {
oos.close();
}
long remaining = sendStaging.remaining();
channel.notifyWrite(cmd, remaining);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/remoting/AtmostOneThreadExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class AtmostOneThreadExecutor extends AbstractExecutorService {
*/
private Thread worker;

private final LinkedList<Runnable> q = new LinkedList<Runnable>();
private final LinkedList<Runnable> q = new LinkedList<>();

private boolean shutdown;

Expand Down Expand Up @@ -59,7 +59,7 @@ private boolean isAlive() {
public List<Runnable> shutdownNow() {
synchronized (q) {
shutdown = true;
List<Runnable> r = new ArrayList<Runnable>(q);
List<Runnable> r = new ArrayList<>(q);
q.clear();
return r;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/remoting/BinarySafeStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public int read() throws IOException {
}

@Override
public int read(byte b[], int off, int len) throws IOException {
public int read(byte[] b, int off, int len) throws IOException {
if(remaining==-1) return -1; // EOF

if(len<4) {
Expand Down Expand Up @@ -131,7 +131,7 @@ public int read(byte b[], int off, int len) throws IOException {
* The same as {@link #read(byte[], int, int)} but the buffer must be
* longer than off+4,
*/
private int _read(byte b[], int off, int len) throws IOException {
private int _read(byte[] b, int off, int len) throws IOException {
assert remaining==0;
assert b.length>=off+4;

Expand Down Expand Up @@ -242,7 +242,7 @@ public void write(int b) throws IOException {
}

@Override
public void write(byte b[], int off, int len) throws IOException {
public void write(byte[] b, int off, int len) throws IOException {
// if there's anything left in triplet from the last write, try to write them first
if(remaining>0) {
while(len>0 && remaining<3) {
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/hudson/remoting/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public class Channel implements VirtualChannel, IChannel, Closeable {
* Requests that are sent to the remote side for execution, yet we are waiting locally until
* we hear back their responses.
*/
/*package*/ final Map<Integer,Request<? extends Serializable,? extends Throwable>> pendingCalls = new Hashtable<Integer,Request<? extends Serializable,? extends Throwable>>();
/*package*/ final Map<Integer,Request<? extends Serializable,? extends Throwable>> pendingCalls = new Hashtable<>();

/**
* Remembers last I/O ID issued from locally to the other side, per thread.
Expand All @@ -171,7 +171,7 @@ public class Channel implements VirtualChannel, IChannel, Closeable {
* Records the {@link Request}s being executed on this channel, sent by the remote peer.
*/
/*package*/ final Map<Integer,Request<?,?>> executingCalls =
Collections.synchronizedMap(new Hashtable<Integer,Request<?,?>>());
Collections.synchronizedMap(new Hashtable<>());

/**
* {@link ClassLoader}s that are proxies of the remote classloaders.
Expand Down Expand Up @@ -200,7 +200,7 @@ public class Channel implements VirtualChannel, IChannel, Closeable {
* per OID, it will only result in a temporary spike in the effective window size,
* and therefore should be OK.
*/
private final WeakHashMap<PipeWindow.Key, WeakReference<PipeWindow>> pipeWindows = new WeakHashMap<PipeWindow.Key, WeakReference<PipeWindow>>();
private final WeakHashMap<PipeWindow.Key, WeakReference<PipeWindow>> pipeWindows = new WeakHashMap<>();
/**
* There are cases where complex object cycles can cause a closed channel to fail to be garbage collected,
* these typically arrise when an {@link #export(Class, Object)} is {@link #setProperty(Object, Object)}
Expand Down Expand Up @@ -537,7 +537,6 @@ public Channel(String name, ExecutorService exec, CommandTransport transport, bo
* See {@link #Channel(String, ExecutorService, Mode, InputStream, OutputStream, OutputStream, boolean, ClassLoader)}
* @param restricted
* See {@link #Channel(String, ExecutorService, Mode, InputStream, OutputStream, OutputStream, boolean, ClassLoader)}
* @param jarCache
*
* @since 2.24
* @deprecated as of 2.38
Expand Down Expand Up @@ -977,7 +976,7 @@ public void setJarCache(@Nonnull JarCache jarCache) {
w = new Real(k, PIPE_WINDOW_SIZE);
else
w = new PipeWindow.Fake();
pipeWindows.put(k,new WeakReference<PipeWindow>(w));
pipeWindows.put(k, new WeakReference<>(w));
return w;
}
}
Expand All @@ -997,7 +996,7 @@ V call(Callable<V,T> callable) throws IOException, T, InterruptedException {

UserRequest<V,T> request=null;
try {
request = new UserRequest<V, T>(this, callable);
request = new UserRequest<>(this, callable);
UserRequest.ResponseToUserRequest<V, T> r = request.call(this);
return r.retrieve(this, UserRequest.getClassLoader(callable));

Expand Down Expand Up @@ -1135,7 +1134,7 @@ public boolean removeListener(Listener l) {
}

/**
* Adds a {@link CallableFilter} that gets a chance to decorate every {@link Callable}s that run locally
* Adds a {@link CallableDecorator} that gets a chance to decorate every {@link Callable}s that run locally
* sent by the other peer.
*
* This is useful to tweak the environment those closures are run, such as setting up the thread context
Expand All @@ -1146,7 +1145,7 @@ public void addLocalExecutionInterceptor(CallableDecorator decorator) {
}

/**
* Removes the filter introduced by {@link #addLocalExecutionInterceptor(CallableFilter)}.
* Removes the filter introduced by {@link #addLocalExecutionInterceptor(CallableDecorator)}.
*/
public void removeLocalExecutionInterceptor(CallableDecorator decorator) {
decorators.remove(decorator);
Expand Down Expand Up @@ -1993,7 +1992,7 @@ void notifyJar(File jar) {
/**
* Remembers the current "channel" associated for this thread.
*/
private static final ThreadLocal<Channel> CURRENT = new ThreadLocal<Channel>();
private static final ThreadLocal<Channel> CURRENT = new ThreadLocal<>();

private static final Logger logger = Logger.getLogger(Channel.class.getName());

Expand All @@ -2017,7 +2016,7 @@ void notifyJar(File jar) {
/**
* Keep track of active channels in the system for diagnostics purposes.
*/
private static final Map<Channel,Ref> ACTIVE_CHANNELS = Collections.synchronizedMap(new WeakHashMap<Channel, Ref>());
private static final Map<Channel,Ref> ACTIVE_CHANNELS = Collections.synchronizedMap(new WeakHashMap<>());

static final Class<?> jarLoaderProxy;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/remoting/ChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public class ChannelBuilder {
private OutputStream header;
@CheckForNull
private JarCache jarCache;
private List<CallableDecorator> decorators = new ArrayList<CallableDecorator>();
private List<CallableDecorator> decorators = new ArrayList<>();
private boolean arbitraryCallableAllowed = true;
private boolean remoteClassLoadingAllowed = true;
private final Hashtable<Object,Object> properties = new Hashtable<Object,Object>();
private final Hashtable<Object,Object> properties = new Hashtable<>();
private ClassFilter filter = ClassFilter.DEFAULT;

/**
Expand Down Expand Up @@ -273,7 +273,7 @@ public ChannelBuilder withRoles(final Collection<? extends Role> actual) {
@Override
public void check(RoleSensitive subject, @Nonnull Collection<Role> expected) {
if (!actual.containsAll(expected)) {
Collection<Role> c = new ArrayList<Role>(expected);
Collection<Role> c = new ArrayList<>(expected);
c.removeAll(actual);
throw new SecurityException("Unexpected role: " + c);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/remoting/ClassFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public boolean isBlacklisted(String name) {

/**
* Changes the effective value of {@link #DEFAULT}.
* @param filter a new default to set; may or may not delegate to {@link STANDARD}
* @param filter a new default to set; may or may not delegate to {@link #STANDARD}
* @since 3.16
*/
public static void setDefault(@Nonnull ClassFilter filter) {
Expand Down Expand Up @@ -216,7 +216,7 @@ private static List<String> loadPatternOverride() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(prop), Charset.defaultCharset()));
ArrayList<String> patterns = new ArrayList<String>();
ArrayList<String> patterns = new ArrayList<>();
for (String line = br.readLine(); line != null; line = br.readLine()) {
try {
Pattern.compile(line);
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/hudson/remoting/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,12 @@ public void setKeepAlive(boolean keepAlive) {
public void setCandidateCertificates(List<X509Certificate> candidateCertificates) {
this.candidateCertificates = candidateCertificates == null
? null
: new ArrayList<X509Certificate>(candidateCertificates);
: new ArrayList<>(candidateCertificates);
}

public void addCandidateCertificate(X509Certificate certificate) {
if (candidateCertificates == null) {
candidateCertificates = new ArrayList<X509Certificate>();
candidateCertificates = new ArrayList<>();
}
candidateCertificates.add(certificate);
}
Expand All @@ -472,8 +472,7 @@ public void run() {
}
// Create the engine
try {
IOHub hub = IOHub.create(executor);
try {
try (IOHub hub = IOHub.create(executor)) {
SSLContext context;
// prepare our SSLContext
try {
Expand Down Expand Up @@ -517,8 +516,6 @@ public void run() {
return;
}
innerRun(hub, context, executor);
} finally {
hub.close();
}
} catch (IOException e) {
events.error(e);
Expand Down Expand Up @@ -867,7 +864,7 @@ public static Engine current() {
return CURRENT.get();
}

private static final ThreadLocal<Engine> CURRENT = new ThreadLocal<Engine>();
private static final ThreadLocal<Engine> CURRENT = new ThreadLocal<>();

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

Expand All @@ -879,7 +876,7 @@ static KeyStore getCacertsKeyStore()
new PrivilegedExceptionAction<Map<String, String>>() {
@Override
public Map<String, String> run() throws Exception {
Map<String, String> result = new HashMap<String, String>();
Map<String, String> result = new HashMap<>();
result.put("trustStore", System.getProperty("javax.net.ssl.trustStore"));
result.put("javaHome", System.getProperty("java.home"));
result.put("trustStoreType",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/remoting/EngineListenerSplitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @since 2.36
*/
public class EngineListenerSplitter implements EngineListener {
protected final List<EngineListener> listeners = new CopyOnWriteArrayList<EngineListener>();
protected final List<EngineListener> listeners = new CopyOnWriteArrayList<>();

public void add(EngineListener el) {
listeners.add(el);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/hudson/remoting/ExportTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@
* @author Kohsuke Kawaguchi
*/
final class ExportTable {
private final Map<Integer,Entry<?>> table = new HashMap<Integer,Entry<?>>();
private final Map<Object,Entry<?>> reverse = new HashMap<Object,Entry<?>>();
private final Map<Integer,Entry<?>> table = new HashMap<>();
private final Map<Object,Entry<?>> reverse = new HashMap<>();
/**
* {@link ExportList}s which are actively recording the current
* export operation.
*/
private final ThreadLocal<ExportList> lists = new ThreadLocal<ExportList>();
private final ThreadLocal<ExportList> lists = new ThreadLocal<>();

/**
* For diagnosing problems like JENKINS-20707 where we seem to be unexporting too eagerly,
* record most recent unexported objects up to {@link #UNEXPORT_LOG_SIZE}
*
* New entries are added to the end, and older ones are removed from the beginning.
*/
private final List<Entry<?>> unexportLog = new LinkedList<Entry<?>>();
private final List<Entry<?>> unexportLog = new LinkedList<>();

/**
* Information about one exported object.
Expand Down Expand Up @@ -362,7 +362,7 @@ synchronized <T> int export(@Nonnull Class<T> clazz, @CheckForNull T t, boolean

Entry<T> e = (Entry<T>) reverse.get(t);
if (e == null) {
e = new Entry<T>(t, clazz);
e = new Entry<>(t, clazz);
} else {
e.addInterface(clazz);
}
Expand Down Expand Up @@ -432,7 +432,7 @@ synchronized Class<?>[] type(int id) throws ExecutionException {
void abort(@CheckForNull Throwable e) {
List<Entry<?>> values;
synchronized (this) {
values = new ArrayList<Entry<?>>(table.values());
values = new ArrayList<>(table.values());
}
for (Entry<?> v : values) {
if (v.object instanceof ErrorPropagatingOutputStream) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/remoting/ExportedClassLoaderTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
* @author Kohsuke Kawaguchi
*/
final class ExportedClassLoaderTable {
private final Map<Integer, WeakReference<ClassLoader>> table = new HashMap<Integer, WeakReference<ClassLoader>>();
private final WeakHashMap<ClassLoader,Integer> reverse = new WeakHashMap<ClassLoader,Integer>();
private final Map<Integer, WeakReference<ClassLoader>> table = new HashMap<>();
private final WeakHashMap<ClassLoader,Integer> reverse = new WeakHashMap<>();

// id==0 is reserved for bootstrap classloader
private int iota = 1;
Expand All @@ -48,7 +48,7 @@ public synchronized int intern(ClassLoader cl) {
Integer id = reverse.get(cl);
if(id==null) {
id = iota++;
table.put(id,new WeakReference<ClassLoader>(cl));
table.put(id, new WeakReference<>(cl));
reverse.put(cl,id);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/remoting/FastPipedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public void connect(FastPipedOutputStream source) throws IOException {
if(this.source != null) {
throw new IOException("Pipe already connected");
}
this.source = new WeakReference<FastPipedOutputStream>(source);
source.sink = new WeakReference<FastPipedInputStream>(this);
this.source = new WeakReference<>(source);
source.sink = new WeakReference<>(this);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/remoting/FastPipedOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public void connect(FastPipedInputStream sink) throws IOException {
if(this.sink != null) {
throw new IOException("Pipe already connected");
}
this.sink = new WeakReference<FastPipedInputStream>(sink);
sink.source = new WeakReference<FastPipedOutputStream>(this);
this.sink = new WeakReference<>(sink);
sink.source = new WeakReference<>(this);
}

@Override
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/hudson/remoting/FileSystemJarCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class FileSystemJarCache extends JarCacheSupport {
/**
* We've reported these checksums as present on this side.
*/
private final Set<Checksum> notified = Collections.synchronizedSet(new HashSet<Checksum>());
private final Set<Checksum> notified = Collections.synchronizedSet(new HashSet<>());

/**
* Cache of computer checksums for cached jars.
Expand Down Expand Up @@ -111,12 +111,9 @@ protected URL retrieve(Channel channel, long sum1, long sum2) throws IOException
try {
File tmp = createTempJar(target);
try {
RemoteOutputStream o = new RemoteOutputStream(new FileOutputStream(tmp));
try {
LOGGER.log(Level.FINE, String.format("Retrieving jar file %16X%16X",sum1,sum2));
try (RemoteOutputStream o = new RemoteOutputStream(new FileOutputStream(tmp))) {
LOGGER.log(Level.FINE, String.format("Retrieving jar file %16X%16X", sum1, sum2));
getJarLoader(channel).writeJarTo(sum1, sum2, o);
} finally {
o.close();
}

// Verify the checksum of the download.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
final class ImportedClassLoaderTable {
final Channel channel;
final Map<IClassLoader,ClassLoader> classLoaders = new Hashtable<IClassLoader,ClassLoader>();
final Map<IClassLoader,ClassLoader> classLoaders = new Hashtable<>();

ImportedClassLoaderTable(Channel channel) {
this.channel = channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public V call() throws Exception {
}

private <T> Collection<Callable<T>> wrap(Collection<? extends Callable<T>> callables) {
List<Callable<T>> r = new ArrayList<Callable<T>>();
List<Callable<T>> r = new ArrayList<>();
for (Callable<T> c : callables) {
r.add(wrap(c));
}
Expand Down
Loading