Java
Unused imports
a simple script for removing unused imports
https://gist.github.com/rodrigosetti/4734557
Thread dumps
have linux process with id [pid] dump its threads to the console
kill -3 [pid]
jstack [id]
lazy method, simply force all java processes to make thread dumps (not for production)
pkill -3 java
thread dump analysis
http://fastthread.io/
Manifest
specifying a main class
Main-Class: com.example.Type
Print JavaBean properties
public static void dump(Object obj) {
Map<String, Object> map = new HashMap<>();
for (Method method : obj.getClass().getMethods()) {
try {
if (method.getName().startsWith("get") && method.getParameters().length == 0) {
method.setAccessible(true);
Object result = method.invoke(obj, new Object[] {});
map.put(method.getName(), result);
}
} catch (Exception e) {
System.out.println("## " + method.getName() + " ##");
e.printStackTrace();
System.out.println();
}
}
map.forEach((key, value) -> System.out.println(key + " => " + value));
}
dealing with warnings
ignoring heap pollution warning
@SafeVarargs
ignoring compiler warnings
@SuppressWarnings("[value]")
valid values
https://stackoverflow.com/questions/1205995/what-is-the-list-of-valid-suppresswarnings-warning-names-in-java
java.util.logging
Since Java 7 it is possible to configure the output pattern for log messages with the SimpleFormatter.
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tl:%1$tM:%1$tS %4$s %2$s: %5$s %6$s%n
The digits in the property value above refer to parameters provided to the formatter.
0. format - the java.util.Formatter format string specified in the java.util.logging.SimpleFormatter.format property or the default format.
1. date - a Date object representing event time of the log record.
2. source - a string representing the caller, if available; otherwise, the logger's name.
3. logger - the logger's name.
4. level - the log level.
5. message - the formatted log message. It uses java.text formatting and does not use the java.util.Formatter format argument.
6. thrown - a string representing the throwable associated with the log record and its backtrace beginning with a newline character.
Pattern Syntax:
http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
programmatically loading a config file:
final LogManager logManager = LogManager.getLogManager();
try (final InputStream is = Main.class.getResourceAsStream("/logging.properties")) {
logManager.readConfiguration(is);
}
Sample logging.properties:
handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.
3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.FileHandler.prefix = manager.
4host-manager.org.apache.juli.FileHandler.level = FINE
4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.FileHandler.prefix = host-manager.
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tl:%1$tM:%1$tS %4$s %2$s: %5$s %6$s%n
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.FileHandler
############################################################
# Application specific properties.
############################################################
org.quartz.level = WARNING
org.springframework.level = WARNING
# custom code loggins
com.example.level = WARNING
com.example.startup.level = INFO