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

Take advantage of Java 5 language features #389

Merged
merged 3 commits into from
Oct 15, 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 @@ -59,6 +59,7 @@ public interface ByteArrayReceiver {
public final void setup(final Channel channel, final CommandReceiver receiver) {
this.channel = channel;
setup(new ByteArrayReceiver() {
@Override
public void handle(byte[] payload) {
try {
Command cmd = Command.readFrom(channel, payload);
Expand All @@ -68,6 +69,7 @@ public void handle(byte[] payload) {
}
}

@Override
public void terminate(IOException e) {
receiver.terminate(e);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/hudson/remoting/AsyncFutureImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,22 @@ public AsyncFutureImpl(Throwable value) {
set(value);
}

@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}

@Override
public synchronized boolean isCancelled() {
return cancelled;
}

@Override
public synchronized boolean isDone() {
return completed;
}

@Override
public synchronized V get() throws InterruptedException, ExecutionException {
while(!completed)
wait();
Expand All @@ -80,6 +84,7 @@ public synchronized V get() throws InterruptedException, ExecutionException {
return value;
}

@Override
@CheckForNull
public synchronized V get(@Nonnegative long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
// The accuracy of wait(long) operation is milliseconds anyway, but ok.
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/hudson/remoting/AtmostOneThreadExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public AtmostOneThreadExecutor() {
this(new DaemonThreadFactory());
}

@Override
public void shutdown() {
synchronized (q) {
shutdown = true;
Expand All @@ -54,6 +55,7 @@ private boolean isAlive() {
return worker!=null && worker.isAlive();
}

@Override
public List<Runnable> shutdownNow() {
synchronized (q) {
shutdown = true;
Expand All @@ -63,14 +65,17 @@ public List<Runnable> shutdownNow() {
}
}

@Override
public boolean isShutdown() {
return shutdown;
}

@Override
public boolean isTerminated() {
return shutdown && !isAlive();
}

@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
synchronized (q) {
long now = System.nanoTime();
Expand All @@ -83,6 +88,7 @@ public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedE
return isTerminated();
}

@Override
public void execute(Runnable command) {
synchronized (q) {
if (isShutdown()) {
Expand All @@ -98,6 +104,7 @@ public void execute(Runnable command) {
}

private class Worker implements Runnable {
@Override
public void run() {
while (true) {
Runnable task;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/hudson/remoting/BinarySafeStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static InputStream wrap(InputStream in) {
final byte[] qualtet = new byte[4];
int input = 0;

@Override
public int read() throws IOException {
if(remaining==0) {
remaining = _read(triplet,0,3);
Expand All @@ -87,6 +88,7 @@ public int read() throws IOException {
return ((int) triplet[3 - remaining--]) & 0xFF;
}

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

Expand Down Expand Up @@ -209,6 +211,7 @@ private int _read(byte b[], int off, int len) throws IOException {
return totalRead;
}

@Override
public int available() throws IOException {
// roughly speaking we got 3/4 of the underlying available bytes
return super.available()*3/4;
Expand All @@ -228,6 +231,7 @@ public static OutputStream wrap(OutputStream out) {
private int remaining=0;
private final byte[] out = new byte[4];

@Override
public void write(int b) throws IOException {
if(remaining==2) {
_write(triplet[0],triplet[1],(byte)b);
Expand All @@ -237,6 +241,7 @@ public void write(int b) throws IOException {
}
}

@Override
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) {
Expand Down Expand Up @@ -272,6 +277,7 @@ private void _write(byte a, byte b, byte c) throws IOException {
super.out.write(out,0,4);
}

@Override
public void flush() throws IOException {
int a = triplet[0];
int b = triplet[1];
Expand Down
1 change: 1 addition & 0 deletions src/main/java/hudson/remoting/CallableDecoratorList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ <V> java.util.concurrent.Callable<V> wrapCallable(java.util.concurrent.Callable<

private <V> java.util.concurrent.Callable<V> applyDecorator(final java.util.concurrent.Callable<V> inner, final CallableDecorator filter) {
return new java.util.concurrent.Callable<V>() {
@Override
public V call() throws Exception {
return filter.call(inner);
}
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/hudson/remoting/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -158,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<?,?>> pendingCalls = new Hashtable<Integer,Request<?,?>>();
/*package*/ final Map<Integer,Request<? extends Serializable,? extends Throwable>> pendingCalls = new Hashtable<Integer,Request<? extends Serializable,? extends Throwable>>();

/**
* Remembers last I/O ID issued from locally to the other side, per thread.
Expand Down Expand Up @@ -593,6 +594,7 @@ protected Channel(@Nonnull ChannelBuilder settings, @Nonnull CommandTransport tr
this.properties.putAll(settings.getProperties());

transport.setup(this, new CommandReceiver() {
@Override
public void handle(Command cmd) {
commandsReceived++;
long receivedAt = System.currentTimeMillis();
Expand All @@ -615,6 +617,7 @@ public void handle(Command cmd) {
}
}

@Override
public void terminate(IOException e) {
Channel.this.terminate(e);
}
Expand Down Expand Up @@ -983,6 +986,7 @@ public void setJarCache(@Nonnull JarCache jarCache) {
/**
* {@inheritDoc}
*/
@Override
public <V,T extends Throwable>
V call(Callable<V,T> callable) throws IOException, T, InterruptedException {
if (isClosingOrClosed()) {
Expand Down Expand Up @@ -1017,6 +1021,7 @@ V call(Callable<V,T> callable) throws IOException, T, InterruptedException {
/**
* {@inheritDoc}
*/
@Override
public <V,T extends Throwable>
Future<V> callAsync(final Callable<V,T> callable) throws IOException {
if (isClosingOrClosed()) {
Expand Down Expand Up @@ -1173,6 +1178,7 @@ public void removeLocalExecutionInterceptor(CallableFilter filter) {
* @throws InterruptedException
* If the current thread is interrupted while waiting for the completion.
*/
@Override
public synchronized void join() throws InterruptedException {
while(inClosed==null || outClosed==null)
// not that I really encountered any situation where this happens, but
Expand Down Expand Up @@ -1267,6 +1273,7 @@ private static final class SetMaximumBytecodeLevel implements Callable<Void,Runt
SetMaximumBytecodeLevel(short level) {
this.level = level;
}
@Override
public Void call() throws RuntimeException {
Channel.currentOrFail().maximumBytecodeLevel = level;
return null;
Expand All @@ -1287,6 +1294,7 @@ public void checkRoles(RoleChecker checker) throws SecurityException {
* If the current thread is interrupted while waiting for the completion.
* @since 1.299
*/
@Override
public synchronized void join(long timeout) throws InterruptedException {
long now = System.nanoTime();
long end = now + TimeUnit.MILLISECONDS.toNanos(timeout);
Expand All @@ -1308,6 +1316,7 @@ private CloseCommand(Channel channel, @CheckForNull Throwable cause) {
super(channel, cause);
}

@Override
protected void execute(Channel channel) {
try {
channel.close();
Expand Down Expand Up @@ -1442,6 +1451,7 @@ public void dumpDiagnostics(@Nonnull PrintWriter w) throws IOException {
/**
* {@inheritDoc}
*/
@Override
public void close() throws IOException {
close(null);
}
Expand Down Expand Up @@ -1535,6 +1545,7 @@ public <T> T getProperty(ChannelProperty<T> key) {
* to wait for the set by the other side of the channel (via {@link #waitForRemoteProperty(Object)}.
* If we don't abort after the channel shutdown, this method will block forever.
*/
@Override
@Nonnull
public Object waitForProperty(@Nonnull Object key) throws InterruptedException {

Expand Down Expand Up @@ -1743,13 +1754,15 @@ public void syncIO() throws IOException, InterruptedException {
// callAsync(new IOSyncer());
// }

@Override
public void syncLocalIO() throws InterruptedException {
Thread t = Thread.currentThread();
String old = t.getName();
t.setName("I/O sync: "+old);
try {
// no one waits for the completion of this Runnable, so not using I/O ID
pipeWriter.submit(0,new Runnable() {
@Override
public void run() {
// noop
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/hudson/remoting/ClassFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public boolean isBlacklisted(@Nonnull String name) {
* @param c a loaded class
* @return false by default; override to return true to blacklist this class
*/
public boolean isBlacklisted(@Nonnull Class c) {
public boolean isBlacklisted(@Nonnull Class<?> c) {
return false;
}

Expand All @@ -74,7 +74,7 @@ public final String check(String name) {
* @return the same {@code c}
* @throws SecurityException if it is blacklisted
*/
public final Class check(Class c) {
public final Class<?> check(Class<?> c) {
if (isBlacklisted(c)) {
throw new SecurityException("Rejected: " + c.getName() + "; see https://jenkins.io/redirect/class-filter/");
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public final Class check(Class c) {
*/
public static final ClassFilter DEFAULT = new ClassFilter() {
@Override
public boolean isBlacklisted(Class c) {
public boolean isBlacklisted(Class<?> c) {
return CURRENT_DEFAULT.isBlacklisted(c);
}
@Override
Expand Down Expand Up @@ -300,7 +300,7 @@ public boolean isBlacklisted(String name) {
}

@Override
public boolean isBlacklisted(Class c) {
public boolean isBlacklisted(Class<?> c) {
AnonymousClassWarnings.check(c);
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/hudson/remoting/ClassicCommandTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public Capability getRemoteCapability() throws IOException {
return remoteCapability;
}

@Override
public final void write(Command cmd, boolean last) throws IOException {
cmd.writeTo(channel,oos);
// TODO notifyWrite using CountingOutputStream
Expand All @@ -62,10 +63,12 @@ public final void write(Command cmd, boolean last) throws IOException {
oos.reset();
}

@Override
public void closeWrite() throws IOException {
oos.close();
}

@Override
public final Command read() throws IOException, ClassNotFoundException {
try {
Command cmd = Command.readFromObjectStream(channel, ois);
Expand Down Expand Up @@ -95,6 +98,7 @@ private StreamCorruptedException diagnoseStreamCorruption(Exception e) throws St
return rawIn.analyzeCrash(e,(channel!=null ? channel : this).toString());
}

@Override
public void closeRead() throws IOException {
ois.close();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/hudson/remoting/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ private Source(@CheckForNull Throwable cause) {
super(cause);
}

@Override
public String toString() {
return "Command "+Command.this.toString()+" created at";
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/hudson/remoting/DaemonThreadFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public class DaemonThreadFactory implements ThreadFactory {
private static final Logger LOGGER = Logger.getLogger(DaemonThreadFactory.class.getName());

@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
Expand Down
Loading