Skip to content

Commit

Permalink
address sonar issues (#8270)
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher authored Nov 2, 2022
1 parent 231cd46 commit e92bd52
Show file tree
Hide file tree
Showing 48 changed files with 226 additions and 153 deletions.
44 changes: 23 additions & 21 deletions context/src/main/java/io/micronaut/runtime/Micronaut.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
Expand All @@ -48,12 +47,12 @@
* @since 1.0
*/
public class Micronaut extends DefaultApplicationContextBuilder implements ApplicationContextBuilder {
private static final String MICRONAUT = " Micronaut";
private static final String MICRONAUT_PREFIX = " Micronaut";
private static final String BANNER_NAME = "micronaut-banner.txt";
private static final Logger LOG = LoggerFactory.getLogger(Micronaut.class);
private static final String SHUTDOWN_MONITOR_THREAD = "micronaut-shutdown-monitor-thread";

private Map<Class<? extends Throwable>, Function<Throwable, Integer>> exitHandlers = new LinkedHashMap<>();
private final Map<Class<? extends Throwable>, Function<Throwable, Integer>> exitHandlers = new LinkedHashMap<>();

/**
* The default constructor.
Expand All @@ -65,6 +64,7 @@ protected Micronaut() {
* @return Run this {@link Micronaut}
*/
@Override
@SuppressWarnings({"java:S1181", "java:S3776", "java:S1141"})
public @NonNull ApplicationContext start() {
long start = System.nanoTime();
printBanner();
Expand All @@ -74,24 +74,22 @@ protected Micronaut() {

applicationContext.start();

Optional<EmbeddedApplication> embeddedContainerBean = applicationContext.findBean(EmbeddedApplication.class);
EmbeddedApplication<?> embeddedApplication = applicationContext.findBean(EmbeddedApplication.class).orElse(null);

embeddedContainerBean.ifPresent((embeddedApplication -> {
if (embeddedApplication != null) {
try {
embeddedApplication.start();

boolean keepAlive = false;
if (embeddedApplication instanceof Described) {
boolean keepAlive;
if (embeddedApplication instanceof Described described) {
if (LOG.isInfoEnabled()) {
long took = elapsedMillis(start);
String desc = ((Described) embeddedApplication).getDescription();
String desc = described.getDescription();
LOG.info("Startup completed in {}ms. Server Running: {}", took, desc);
}
keepAlive = embeddedApplication.isServer();
} else {
if (embeddedApplication instanceof EmbeddedServer) {

final EmbeddedServer embeddedServer = (EmbeddedServer) embeddedApplication;
if (embeddedApplication instanceof EmbeddedServer embeddedServer) {
if (LOG.isInfoEnabled()) {
long took = elapsedMillis(start);
URL url = embeddedServer.getURL();
Expand Down Expand Up @@ -131,7 +129,7 @@ protected Micronaut() {
Thread.sleep(1000);
}
} catch (InterruptedException e) {
// ignore
Thread.currentThread().interrupt();
}
}, SHUTDOWN_MONITOR_THREAD).start();

Expand All @@ -142,6 +140,7 @@ protected Micronaut() {
break;
} catch (InterruptedException e) {
interrupted = true;
Thread.currentThread().interrupt();
}
}
if (interrupted) {
Expand All @@ -158,16 +157,18 @@ protected Micronaut() {

} catch (Throwable e) {
handleStartupException(applicationContext.getEnvironment(), e);
Thread.currentThread().interrupt();
}
}));
}

if (LOG.isInfoEnabled() && !embeddedContainerBean.isPresent()) {
if (LOG.isInfoEnabled() && embeddedApplication == null) {
LOG.info("No embedded container found. Running as CLI application");
}
return applicationContext;
} catch (Throwable e) {
handleStartupException(applicationContext.getEnvironment(), e);
return null;
Thread.currentThread().interrupt();
return applicationContext;
}
}

Expand Down Expand Up @@ -196,9 +197,9 @@ private static long elapsedMillis(long startNanos) {
* @param classes The application
* @return The classes
*/
public @NonNull Micronaut classes(@Nullable Class... classes) {
public @NonNull Micronaut classes(@Nullable Class<?>... classes) {
if (classes != null) {
for (Class aClass : classes) {
for (Class<?> aClass : classes) {
packages(aClass.getPackage().getName());
}
}
Expand Down Expand Up @@ -236,7 +237,7 @@ private static long elapsedMillis(long startNanos) {
}

@Override
public @NonNull Micronaut mainClass(Class mainClass) {
public @NonNull Micronaut mainClass(Class<?> mainClass) {
return (Micronaut) super.mainClass(mainClass);
}

Expand Down Expand Up @@ -305,7 +306,7 @@ public static ApplicationContext run(String... args) {
* @param args The arguments
* @return The {@link ApplicationContext}
*/
public static ApplicationContext run(Class cls, String... args) {
public static ApplicationContext run(Class<?> cls, String... args) {
return run(new Class[]{cls}, args);
}

Expand All @@ -316,7 +317,7 @@ public static ApplicationContext run(Class cls, String... args) {
* @param args The arguments
* @return The {@link ApplicationContext}
*/
public static ApplicationContext run(Class[] classes, String... args) {
public static ApplicationContext run(Class<?>[] classes, String... args) {
return new Micronaut()
.classes(classes)
.args(args)
Expand All @@ -342,6 +343,7 @@ protected void handleStartupException(Environment environment, Throwable excepti
throw new ApplicationStartupException("Error starting Micronaut server: " + exception.getMessage(), exception);
}

@SuppressWarnings("java:S106")
private void printBanner() {
if (!isBannerEnabled()) {
return;
Expand All @@ -354,7 +356,7 @@ private void printBanner() {
private void printMicronautVersion(@NonNull PrintStream out) {
String version = VersionUtils.getMicronautVersion();
version = (version != null) ? " (v" + version + ")" : "";
out.println(MICRONAUT + version + "\n");
out.println(MICRONAUT_PREFIX + version + "\n");
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public DefaultWatchThread start() {
}
} catch (InterruptedException | ClosedWatchServiceException e) {
// ignore
Thread.currentThread().interrupt();
}
}
}, "micronaut-filewatch-thread").start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class Publishers {
"io.reactivex.rxjava3.core.Observable"
);
for (String name : typeNames) {
Optional<Class> aClass = ClassUtils.forName(name, classLoader);
Optional<Class<?>> aClass = ClassUtils.forName(name, classLoader);
aClass.ifPresent(reactiveTypes::add);
}
for (String name : Arrays.asList(
Expand All @@ -93,15 +93,15 @@ public class Publishers {
"io.reactivex.rxjava3.core.Single",
"io.reactivex.rxjava3.core.Maybe"
)) {
Optional<Class> aClass = ClassUtils.forName(name, classLoader);
Optional<Class<?>> aClass = ClassUtils.forName(name, classLoader);
aClass.ifPresent(aClass1 -> {
singleTypes.add(aClass1);
reactiveTypes.add(aClass1);
});
}

for (String name : Arrays.asList("io.reactivex.Completable", "io.reactivex.rxjava3.core.Completable")) {
Optional<Class> aClass = ClassUtils.forName(name, classLoader);
Optional<Class<?>> aClass = ClassUtils.forName(name, classLoader);
aClass.ifPresent(aClass1 -> {
completableTypes.add(aClass1);
reactiveTypes.add(aClass1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ default <T> Optional<T> getDefaultValue(@NonNull Class<? extends Annotation> ann
* @see AnnotationSource#isAnnotationPresent(Class)
*/
@Override
@SuppressWarnings("java:S2583")
default boolean isAnnotationPresent(@NonNull Class<? extends Annotation> annotationClass) {
//noinspection ConstantConditions
if (annotationClass == null) {
Expand All @@ -416,6 +417,7 @@ default boolean isAnnotationPresent(@NonNull Class<? extends Annotation> annotat
* @see AnnotationSource#isAnnotationPresent(Class)
*/
@Override
@SuppressWarnings("java:S2583")
default boolean isDeclaredAnnotationPresent(@NonNull Class<? extends Annotation> annotationClass) {
//noinspection ConstantConditions
if (annotationClass == null) {
Expand All @@ -429,6 +431,7 @@ default boolean isDeclaredAnnotationPresent(@NonNull Class<? extends Annotation>
* @see AnnotationSource#isAnnotationPresent(String)
*/
@Override
@SuppressWarnings("java:S2583")
default boolean isAnnotationPresent(@NonNull String annotationName) {
//noinspection ConstantConditions
if (annotationName == null) {
Expand All @@ -441,6 +444,7 @@ default boolean isAnnotationPresent(@NonNull String annotationName) {
* @see AnnotationSource#isAnnotationPresent(String)
*/
@Override
@SuppressWarnings("java:S2583")
default boolean isDeclaredAnnotationPresent(@NonNull String annotationName) {
//noinspection ConstantConditions
if (annotationName == null) {
Expand Down Expand Up @@ -578,8 +582,8 @@ default Optional<Class<? extends Annotation>> getDeclaredAnnotationTypeByStereot
*/
default Optional<Class<? extends Annotation>> getAnnotationType(@NonNull String name, @NonNull ClassLoader classLoader) {
ArgumentUtils.requireNonNull("name", name);
final Optional<Class> aClass = ClassUtils.forName(name, classLoader);
Class clazz = aClass.orElse(null);
final Optional<Class<?>> aClass = ClassUtils.forName(name, classLoader);
Class<?> clazz = aClass.orElse(null);
if (clazz != null && Annotation.class.isAssignableFrom(clazz)) {
//noinspection unchecked
return (Optional) aClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ default boolean isPresentAndSatisfied() {
*
* @return The value
*/
@SuppressWarnings({"java:S3655", "OptionalGetWithoutIsPresent"})
default T get() {
return getValue().get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ private void registerDefaultConverters() {
if (classLoader == null) {
classLoader = DefaultMutableConversionService.class.getClassLoader();
}
return ClassUtils.forName(object.toString(), classLoader);
//noinspection rawtypes
return (Optional) ClassUtils.forName(object.toString(), classLoader);
});

// AnnotationClassValue -> Class
Expand Down
21 changes: 2 additions & 19 deletions core/src/main/java/io/micronaut/core/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.ProviderNotFoundException;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -141,23 +139,8 @@ private static Path loadNestedJarUri(List<Closeable> toClose, String jarUri) thr
// This check makes our class loading resilient to that
return jarPath;
}
FileSystem zipfs;
try {
// can't use newFileSystem(Path) here (without CL) because it doesn't exist on java 8
// the CL cast is necessary because since java 13 there is a newFileSystem(Path, Map)
zipfs = FileSystems.newFileSystem(jarPath, (ClassLoader) null);
toClose.add(0, zipfs);
} catch (ProviderNotFoundException e) {
// java versions earlier than 11 do not support nested zipfs and will fail with this
// exception. Try to extract the file instead. This is not efficient, but what else can
// we do?
Path tmp = Files.createTempFile("micronaut-IOUtils-nested-zip", ".zip");
toClose.add(0, () -> Files.deleteIfExists(tmp));
Files.copy(jarPath, tmp, StandardCopyOption.REPLACE_EXISTING);

zipfs = FileSystems.newFileSystem(tmp, (ClassLoader) null);
toClose.add(0, zipfs);
}
FileSystem zipfs = FileSystems.newFileSystem(jarPath, (ClassLoader) null);
toClose.add(0, zipfs);
return zipfs.getPath(jarUri.substring(sep + 1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Optional<ServiceDefinition<S>> firstOr(String alternative, ClassLoader cl
return Optional.of(i.next());
}

@SuppressWarnings("unchecked") Class<S> alternativeClass = ClassUtils.forName(alternative, classLoader)
@SuppressWarnings("unchecked") Class<S> alternativeClass = (Class<S>) ClassUtils.forName(alternative, classLoader)
.orElse(null);
if (alternativeClass != null) {
return Optional.of(createService(alternative, alternativeClass));
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/io/micronaut/core/reflect/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public static Optional<Class> getPrimitiveType(String primitiveType) {
* @param classLoader The classloader. If null will fallback to attempt the thread context loader, otherwise the system loader
* @return An optional of the class
*/
public static Optional<Class> forName(String name, @Nullable ClassLoader classLoader) {
public static Optional<Class<?>> forName(String name, @Nullable ClassLoader classLoader) {
try {
if (MISSING_TYPES.contains(name)) {
return Optional.empty();
Expand All @@ -285,7 +285,7 @@ public static Optional<Class> forName(String name, @Nullable ClassLoader classLo
classLoader = ClassLoader.getSystemClassLoader();
}

Optional<Class> commonType = Optional.ofNullable(COMMON_CLASS_MAP.get(name));
Optional<Class<?>> commonType = Optional.ofNullable(COMMON_CLASS_MAP.get(name));
if (commonType.isPresent()) {
return commonType;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected ObjectInputStream createObjectInput(InputStream inputStream, Class<?>
return new ObjectInputStream(inputStream) {
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
Optional<Class> aClass = ClassUtils.forName(desc.getName(), requiredType.getClassLoader());
Optional<Class<?>> aClass = ClassUtils.forName(desc.getName(), requiredType.getClassLoader());
if (aClass.isPresent()) {
return aClass.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void validateArguments(
@NonNull Argument<?>[] arguments,
@NonNull Object[] values) {
int requiredCount = arguments.length;
@SuppressWarnings("ConstantConditions") int actualCount = values == null ? 0 : values.length;
@SuppressWarnings("ConstantConditions") int actualCount = ArrayUtils.isEmpty(values) ? 0 : values.length;
if (requiredCount != actualCount) {
throw new IllegalArgumentException("Wrong number of arguments to " + (described instanceof Executable ? "method" : "constructor") + ": " + described.getDescription());
}
Expand Down
Loading

0 comments on commit e92bd52

Please sign in to comment.