Skip to content

Commit

Permalink
#302 clean up more
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 12, 2022
1 parent fe56bd6 commit 5b0c933
Show file tree
Hide file tree
Showing 29 changed files with 144 additions and 167 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ resource downloading in case of failure. You can implement a full

```java
public class MyResource {
@RetryOnFailure
public String load(URL url) {
return url.openConnection().getContent();
}
@RetryOnFailure
public String load(final URL url) {
return url.openConnection().getContent();
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/aspects/Immutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Array {
@interface Array {
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/aspects/Loggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Quiet {
@interface Quiet {
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/aspects/Timeable.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
* @checkstyle MagicNumber (2 lines)
* @return The limit
*/
int limit() default DEFAULT_LIMIT;
int limit() default Timeable.DEFAULT_LIMIT;

/**
* Time unit for the limit.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/aspects/UnitedThrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ class None extends Throwable {
*
* @return The type of exception.
*/
Class<? extends Throwable> value() default None.class;
Class<? extends Throwable> value() default UnitedThrow.None.class;
}
10 changes: 5 additions & 5 deletions src/main/java/com/jcabi/aspects/aj/ImmutabilityChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void check(final Class<?> type)
}
if (!type.isInterface()
&& !Modifier.isFinal(type.getModifiers())) {
throw new Violation(
throw new ImmutabilityChecker.Violation(
String.format(
"Class '%s' is not final",
type.getName()
Expand Down Expand Up @@ -149,7 +149,7 @@ private void fields(final Class<?> type)
continue;
}
if (!Modifier.isFinal(field.getModifiers())) {
throw new Violation(
throw new ImmutabilityChecker.Violation(
String.format(
"field '%s' is not final in %s",
field, type.getName()
Expand All @@ -160,8 +160,8 @@ private void fields(final Class<?> type)
if (field.getType().isArray()) {
this.checkArray(field);
}
} catch (final Violation ex) {
throw new Violation(
} catch (final ImmutabilityChecker.Violation ex) {
throw new ImmutabilityChecker.Violation(
String.format(
"field '%s' is mutable",
field
Expand All @@ -175,7 +175,7 @@ private void fields(final Class<?> type)
/**
* This array field immutable?
* @param field The field to check
* @throws Violation If it is mutable.
* @throws ImmutabilityChecker.Violation If it is mutable.
*/
private void checkArray(final Field field)
throws ImmutabilityChecker.Violation {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/jcabi/aspects/aj/ImprovedJoinPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public JoinPoint.StaticPart getStaticPart() {
*/
public Object targetize() {
final Object target;
final Method method = MethodSignature.class
.cast(this.joinpoint.getSignature()).getMethod();
final Method method = ((MethodSignature) this.joinpoint.getSignature()).getMethod();
if (Modifier.isStatic(method.getModifiers())) {
target = method.getDeclaringClass();
} else {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/jcabi/aspects/aj/MethodAsyncRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public final class MethodAsyncRunner {
* Execute method asynchronously.
*
* <p>This aspect should be used only on {@code void} or
* {@link java.util.concurrent.Future} returning methods.
* {@link Future} returning methods.
*
* <p>Try NOT to change the signature of this method, in order to keep
* it backward compatible.
Expand All @@ -77,8 +77,8 @@ public final class MethodAsyncRunner {
@Around("execution(@com.jcabi.aspects.Async * * (..))")
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public Object wrap(final ProceedingJoinPoint point) {
final Class<?> returned = MethodSignature.class
.cast(point.getSignature()).getMethod().getReturnType();
final Class<?> returned = ((MethodSignature) point.getSignature()).getMethod()
.getReturnType();
if (!Future.class.isAssignableFrom(returned)
&& !returned.equals(Void.TYPE)) {
// @checkstyle LineLength (3 lines)
Expand All @@ -93,11 +93,11 @@ public Object wrap(final ProceedingJoinPoint point) {
final Future<?> result = this.executor.submit(
// @checkstyle AnonInnerLength (23 lines)
() -> {
Object returned1 = null;
Object ret = null;
try {
final Object result1 = point.proceed();
if (result1 instanceof Future) {
returned1 = ((Future<?>) result1).get();
final Object res = point.proceed();
if (res instanceof Future) {
ret = ((Future<?>) res).get();
}
// @checkstyle IllegalCatch (1 line)
} catch (final Throwable ex) {
Expand All @@ -109,7 +109,7 @@ public Object wrap(final ProceedingJoinPoint point) {
ex
);
}
return returned1;
return ret;
}
);
Object res = null;
Expand Down
35 changes: 14 additions & 21 deletions src/main/java/com/jcabi/aspects/aj/MethodCacher.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,13 @@ public MethodCacher() {
public Object cache(final ProceedingJoinPoint point) throws Throwable {
final MethodCacher.Key key = new MethodCacher.Key(point);
MethodCacher.Tunnel tunnel;
final Method method = MethodSignature.class
.cast(point.getSignature())
final Method method = ((MethodSignature) point.getSignature())
.getMethod();
final Cacheable annot = method.getAnnotation(Cacheable.class);
synchronized (this.tunnels) {
for (final Class<?> before : annot.before()) {
final boolean flag = Boolean.class.cast(
before.getMethod("flushBefore").invoke(method.getClass())
);
final boolean flag = (Boolean) before.getMethod("flushBefore")
.invoke(method.getClass());
if (flag) {
this.preflush(point);
}
Expand All @@ -161,9 +159,8 @@ public Object cache(final ProceedingJoinPoint point) throws Throwable {
this.updatekeys.offer(key);
}
for (final Class<?> after : annot.after()) {
final boolean flag = Boolean.class.cast(
after.getMethod("flushAfter").invoke(method.getClass())
);
final boolean flag = (Boolean) after.getMethod("flushAfter")
.invoke(method.getClass());
if (flag) {
this.postflush(point);
}
Expand All @@ -178,7 +175,7 @@ public Object cache(final ProceedingJoinPoint point) throws Throwable {
* @return Boolean
*/
private static boolean isCreateTunnel(final MethodCacher.Tunnel tunnel) {
return tunnel == null || (tunnel.expired() && !tunnel.asyncUpdate());
return tunnel == null || tunnel.expired() && !tunnel.asyncUpdate();
}

/**
Expand Down Expand Up @@ -249,8 +246,7 @@ private void flush(final JoinPoint point, final String when) {
continue;
}
final MethodCacher.Tunnel removed = this.tunnels.remove(key);
final Method method = MethodSignature.class
.cast(point.getSignature())
final Method method = ((MethodSignature) point.getSignature())
.getMethod();
if (LogHelper.enabled(
key.getLevel(), method.getDeclaringClass()
Expand Down Expand Up @@ -383,8 +379,8 @@ public String toString() {
* Public ctor.
* @return MethodCacher.Tunnel
*/
public Tunnel copy() {
return new Tunnel(
public MethodCacher.Tunnel copy() {
return new MethodCacher.Tunnel(
this.point, this.key, this.asynchupdate
);
}
Expand All @@ -400,8 +396,7 @@ public synchronized Object through() throws Throwable {
if (!this.executed) {
final long start = System.currentTimeMillis();
this.cached = this.point.proceed();
final Method method = MethodSignature.class
.cast(this.point.getSignature())
final Method method = ((MethodSignature) this.point.getSignature())
.getMethod();
final Cacheable annot = method.getAnnotation(Cacheable.class);
final String suffix;
Expand All @@ -413,7 +408,7 @@ public synchronized Object through() throws Throwable {
suffix = "invalid immediately";
} else {
final long msec = annot.unit().toMillis(
(long) annot.lifetime()
annot.lifetime()
);
this.lifetime = start + msec;
suffix = Logger.format("valid for %[ms]s", msec);
Expand Down Expand Up @@ -495,8 +490,7 @@ private static final class Key {
* @param point Joint point
*/
Key(final JoinPoint point) {
this.method = MethodSignature.class
.cast(point.getSignature()).getMethod();
this.method = ((MethodSignature) point.getSignature()).getMethod();
this.target = MethodCacher.Key.targetize(point);
this.arguments = point.getArgs();
if (this.method.isAnnotationPresent(Loggable.class)) {
Expand All @@ -522,7 +516,7 @@ public boolean equals(final Object obj) {
if (this == obj) {
equals = true;
} else if (obj instanceof MethodCacher.Key) {
final MethodCacher.Key key = MethodCacher.Key.class.cast(obj);
final MethodCacher.Key key = (Key) obj;
equals = key.method.equals(this.method)
&& this.target.equals(key.target)
&& Arrays.deepEquals(key.arguments, this.arguments);
Expand Down Expand Up @@ -570,8 +564,7 @@ public boolean sameTarget(final JoinPoint point) {
*/
private static Object targetize(final JoinPoint point) {
final Object tgt;
final Method method = MethodSignature.class
.cast(point.getSignature()).getMethod();
final Method method = ((MethodSignature) point.getSignature()).getMethod();
if (Modifier.isStatic(method.getModifiers())) {
tgt = method.getDeclaringClass();
} else {
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/jcabi/aspects/aj/MethodInterrupter.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public MethodInterrupter() {
new VerboseRunnable(
this::interrupt
),
1, 1, TimeUnit.SECONDS
1L, 1L, TimeUnit.SECONDS
);
}

Expand Down Expand Up @@ -160,15 +160,14 @@ private static final class Call implements
this.thread = Thread.currentThread();
this.start = System.currentTimeMillis();
this.point = pnt;
final Method method = MethodSignature.class
.cast(pnt.getSignature())
final Method method = ((MethodSignature) pnt.getSignature())
.getMethod();
final Timeable annt = method.getAnnotation(Timeable.class);
this.deadline = this.start + annt.unit().toMillis(annt.limit());
}

@Override
public int compareTo(final Call obj) {
public int compareTo(final MethodInterrupter.Call obj) {
final int compare;
if (this.deadline > obj.deadline) {
compare = 1;
Expand Down Expand Up @@ -196,8 +195,7 @@ public boolean interrupted() {
final boolean dead;
if (this.thread.isAlive()) {
this.thread.interrupt();
final Method method = MethodSignature.class
.cast(this.point.getSignature())
final Method method = ((MethodSignature) this.point.getSignature())
.getMethod();
Logger.warn(
method.getDeclaringClass(),
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/com/jcabi/aspects/aj/MethodLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public final class MethodLogger {
/**
* Currently running methods.
*/
private final transient Set<Marker> running;
private final transient Set<MethodLogger.Marker> running;

/**
* Public ctor.
Expand All @@ -88,7 +88,7 @@ public MethodLogger() {
new FutureTask<Void>(
new VerboseRunnable(
() -> {
for (final Marker marker
for (final MethodLogger.Marker marker
: this.running) {
marker.monitor();
}
Expand All @@ -101,7 +101,7 @@ protected void done() {
monitor.shutdown();
}
},
1, 1, TimeUnit.SECONDS
1L, 1L, TimeUnit.SECONDS
);
}

Expand All @@ -127,7 +127,7 @@ protected void done() {
)
public Object wrapClass(final ProceedingJoinPoint point) throws Throwable {
final Method method =
MethodSignature.class.cast(point.getSignature()).getMethod();
((MethodSignature) point.getSignature()).getMethod();
final Object output;
if (method.isAnnotationPresent(Loggable.class)) {
output = point.proceed();
Expand Down Expand Up @@ -160,7 +160,7 @@ public Object wrapClass(final ProceedingJoinPoint point) throws Throwable {
@SuppressWarnings("PMD.AvoidCatchingThrowable")
public Object wrapMethod(final ProceedingJoinPoint point) throws Throwable {
final Method method =
MethodSignature.class.cast(point.getSignature()).getMethod();
((MethodSignature) point.getSignature()).getMethod();
return this.wrap(point, method, method.getAnnotation(Loggable.class));
}

Expand Down Expand Up @@ -350,8 +350,8 @@ private static boolean contains(final Class<? extends Throwable>[] array,
private static boolean instanceOf(final Class<?> child,
final Class<?> parent) {
boolean instance = child.equals(parent)
|| (child.getSuperclass() != null
&& MethodLogger.instanceOf(child.getSuperclass(), parent));
|| child.getSuperclass() != null
&& MethodLogger.instanceOf(child.getSuperclass(), parent);
if (!instance) {
for (final Class<?> iface : child.getInterfaces()) {
instance = MethodLogger.instanceOf(iface, parent);
Expand Down Expand Up @@ -450,9 +450,7 @@ public void monitor() {
);
final int cycle = (int) ((age - threshold) / threshold);
if (cycle > this.logged.get()) {
final Method method = MethodSignature.class.cast(
this.point.getSignature()
).getMethod();
final Method method = ((MethodSignature) this.point.getSignature()).getMethod();
Logger.warn(
method.getDeclaringClass(),
"%s: takes more than %[ms]s, %[ms]s already, thread=%s/%s",
Expand Down Expand Up @@ -481,12 +479,12 @@ public int hashCode() {

@Override
public boolean equals(final Object obj) {
return obj == this || MethodLogger.Marker.class.cast(obj)
return obj == this || ((MethodLogger.Marker) obj)
.point.equals(this.point);
}

@Override
public int compareTo(final Marker marker) {
public int compareTo(final MethodLogger.Marker marker) {
int cmp = 0;
if (this.started < marker.started) {
cmp = 1;
Expand Down

0 comments on commit 5b0c933

Please sign in to comment.