use of com.palantir.logsafe.Safe in project conjure-java by palantir.
the class ParamTypesResolver method getParameterType.
@SuppressWarnings("CyclomaticComplexity")
public Optional<ParameterType> getParameterType(VariableElement variableElement) {
List<AnnotationMirror> paramAnnotationMirrors = new ArrayList<>();
for (AnnotationMirror annotationMirror : variableElement.getAnnotationMirrors()) {
TypeElement annotationTypeElement = MoreElements.asType(annotationMirror.getAnnotationType().asElement());
if (SUPPORTED_ANNOTATIONS.contains(annotationTypeElement.getQualifiedName().toString())) {
paramAnnotationMirrors.add(annotationMirror);
}
}
List<AnnotationReflector> annotationReflectors = paramAnnotationMirrors.stream().map(ImmutableAnnotationReflector::of).collect(Collectors.toList());
boolean isSafe = annotationReflectors.stream().anyMatch(annotation -> annotation.isAnnotation(Safe.class));
boolean isUnsafe = annotationReflectors.stream().anyMatch(annotation -> annotation.isAnnotation(Unsafe.class));
SafeLoggingAnnotation safeLoggable = isUnsafe ? SafeLoggingAnnotation.UNSAFE : isSafe ? SafeLoggingAnnotation.SAFE : SafeLoggingAnnotation.UNKNOWN;
List<AnnotationReflector> otherAnnotationReflectors = annotationReflectors.stream().filter(annotation -> !annotation.isAnnotation(Safe.class) && !annotation.isAnnotation(Unsafe.class)).collect(Collectors.toList());
if (otherAnnotationReflectors.isEmpty()) {
if (!safeLoggable.equals(SafeLoggingAnnotation.UNKNOWN)) {
context.reportError("Parameter type cannot be annotated with safe logging annotations", variableElement, SafeArg.of("type", variableElement.asType()));
return Optional.empty();
}
if (context.isSameTypes(variableElement.asType(), AuthHeader.class)) {
return Optional.of(ParameterTypes.authHeader(variableElement.getSimpleName().toString()));
} else if (context.isSameTypes(variableElement.asType(), HttpServerExchange.class)) {
return Optional.of(ParameterTypes.exchange());
} else if (context.isSameTypes(variableElement.asType(), RequestContext.class)) {
return Optional.of(ParameterTypes.context());
} else {
context.reportError("At least one annotation should be present or type should be InputStream", variableElement, SafeArg.of("requestBody", InputStream.class), SafeArg.of("supportedAnnotations", PARAM_ANNOTATION_CLASSES));
return Optional.empty();
}
}
if (otherAnnotationReflectors.size() > 1) {
context.reportError("Only single annotation can be used", variableElement, SafeArg.of("annotations", otherAnnotationReflectors));
return Optional.empty();
}
// TODO(ckozak): More validation of values.
AnnotationReflector annotationReflector = Iterables.getOnlyElement(otherAnnotationReflectors);
if (annotationReflector.isAnnotation(Handle.Body.class)) {
return Optional.of(bodyParameter(variableElement, annotationReflector));
} else if (annotationReflector.isAnnotation(Handle.Header.class)) {
return Optional.of(headerParameter(variableElement, annotationReflector, safeLoggable));
} else if (annotationReflector.isAnnotation(Handle.PathParam.class)) {
return Optional.of(pathParameter(variableElement, annotationReflector, safeLoggable));
} else if (annotationReflector.isAnnotation(Handle.PathMultiParam.class)) {
return Optional.of(pathMultiParameter(variableElement, annotationReflector, safeLoggable));
} else if (annotationReflector.isAnnotation(Handle.QueryParam.class)) {
return Optional.of(queryParameter(variableElement, annotationReflector, safeLoggable));
} else if (annotationReflector.isAnnotation(Handle.Cookie.class)) {
return cookieParameter(variableElement, annotationReflector, safeLoggable);
}
throw new SafeIllegalStateException("Not possible");
}
use of com.palantir.logsafe.Safe in project safe-logging by palantir.
the class LoggerGenerator method generateLoggerImplementation.
private static JavaFile generateLoggerImplementation() {
TypeSpec.Builder loggerBuilder = TypeSpec.classBuilder(LOGGER_NAME).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addJavadoc("Safe-logging logger API used to produce log events with safe-classified {@link Arg} values.").addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", LoggerGenerator.class.getName()).build()).addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "TooManyArguments").build()).addField(FieldSpec.builder(BRIDGE_NAME, DELEGATE).addModifiers(Modifier.PRIVATE, Modifier.FINAL).build()).addMethod(MethodSpec.constructorBuilder().addJavadoc("Internal package-private constructor for {@link SafeLoggerFactory}.").addParameter(ParameterSpec.builder(BRIDGE_NAME, DELEGATE).build()).addStatement("this.$1N = $2T.requireNonNull($1N, $3S)", DELEGATE, Objects.class, "SafeLoggerBridge is required").build());
for (MethodSpec spec : createLoggerBridge().methodSpecs) {
MethodSpec.Builder methodBuilder = spec.toBuilder();
methodBuilder.modifiers.clear();
boolean isVoidMethod = ClassName.VOID.equals(spec.returnType);
CodeBlock args = spec.parameters.stream().map(param -> CodeBlock.of("$N", param.name)).collect(CodeBlock.joining(", "));
methodBuilder.addModifiers(Modifier.PUBLIC).addStatement("$L$N.$N($L)", isVoidMethod ? "" : "return ", DELEGATE, spec.name, args);
loggerBuilder.addMethod(methodBuilder.build());
}
return JavaFile.builder(LOGGER_NAME.packageName(), loggerBuilder.build()).skipJavaLangImports(true).addFileComment(copyright(2021)).build();
}
Aggregations