This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects

Draft: Add java linter - PMD

Closed Daniel Rainer requested to merge pmd into development
Files
58
package at.ac.uibk.gitsearch.aop.logging;
import io.github.jhipster.config.JHipsterConstants;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
@@ -13,8 +13,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import java.util.Arrays;
/**
* Aspect for logging execution of service and repository Spring components.
*
@@ -22,7 +20,6 @@ import java.util.Arrays;
*/
@Aspect
public class LoggingAspect {
private final Environment env;
public LoggingAspect(Environment env) {
@@ -32,20 +29,22 @@ public class LoggingAspect {
/**
* Pointcut that matches all repositories, services and Web REST endpoints.
*/
@Pointcut("within(@org.springframework.stereotype.Repository *)" +
" || within(@org.springframework.stereotype.Service *)" +
" || within(@org.springframework.web.bind.annotation.RestController *)")
public void springBeanPointcut() {
@Pointcut("within(@org.springframework.stereotype.Repository *)"
+ " || within(@org.springframework.stereotype.Service *)"
+ " || within(@org.springframework.web.bind.annotation.RestController *)")
public void
springBeanPointcut() {
// Method is empty as this is just a Pointcut, the implementations are in the advices.
}
/**
* Pointcut that matches all Spring beans in the application's main packages.
*/
@Pointcut("within(at.ac.uibk.gitsearch.repository..*)"+
" || within(at.ac.uibk.gitsearch.service..*)"+
" || within(at.ac.uibk.gitsearch.web.rest..*)")
public void applicationPackagePointcut() {
@Pointcut("within(at.ac.uibk.gitsearch.repository..*)"
+ " || within(at.ac.uibk.gitsearch.service..*)"
+ " || within(at.ac.uibk.gitsearch.web.rest..*)")
public void
applicationPackagePointcut() {
// Method is empty as this is just a Pointcut, the implementations are in the advices.
}
@@ -65,24 +64,17 @@ public class LoggingAspect {
* @param joinPoint join point for advice.
* @param e exception.
*/
@AfterThrowing(pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
@AfterThrowing(
pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e")
public void
logAfterThrowing(JoinPoint joinPoint, Throwable e) {
if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) {
logger(joinPoint)
.error(
"Exception in {}() with cause = \'{}\' and exception = \'{}\'",
joinPoint.getSignature().getName(),
e.getCause() != null ? e.getCause() : "NULL",
e.getMessage(),
e
);
logger(joinPoint).error("Exception in {}() with cause = \'{}\' and exception = \'{}\'",
joinPoint.getSignature().getName(), e.getCause() == null ? "NULL" : e.getCause(),
e.getMessage(), e);
} else {
logger(joinPoint)
.error(
"Exception in {}() with cause = {}",
joinPoint.getSignature().getName(),
e.getCause() != null ? e.getCause() : "NULL"
);
logger(joinPoint).error("Exception in {}() with cause = {}",
joinPoint.getSignature().getName(), e.getCause() == null ? "NULL" : e.getCause());
}
}
@@ -97,16 +89,19 @@ public class LoggingAspect {
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
Logger log = logger(joinPoint);
if (log.isDebugEnabled()) {
log.debug("Enter: {}() with argument[s] = {}", joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
log.debug("Enter: {}() with argument[s] = {}", joinPoint.getSignature().getName(),
Arrays.toString(joinPoint.getArgs()));
}
try {
Object result = joinPoint.proceed();
if (log.isDebugEnabled()) {
log.debug("Exit: {}() with result = {}", joinPoint.getSignature().getName(), result);
log.debug(
"Exit: {}() with result = {}", joinPoint.getSignature().getName(), result);
}
return result;
} catch (IllegalArgumentException e) {
log.error("Illegal argument: {} in {}()", Arrays.toString(joinPoint.getArgs()), joinPoint.getSignature().getName());
log.error("Illegal argument: {} in {}()", Arrays.toString(joinPoint.getArgs()),
joinPoint.getSignature().getName());
throw e;
}
}