Skip to content

Commit

Permalink
[javac] Bring the processing of j2kt related annotations up to date.
Browse files Browse the repository at this point in the history
This cl also makes naming and annotations apis more consistent.

PiperOrigin-RevId: 703332544
  • Loading branch information
rluble authored and copybara-github committed Dec 6, 2024
1 parent 47e2117 commit bc0971c
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,86 +15,92 @@
*/
package com.google.j2cl.transpiler.frontend.javac;

import static com.google.common.collect.MoreCollectors.toOptional;
import static com.google.j2cl.transpiler.frontend.javac.KtInteropAnnotationUtils.getSuppressWarningsAnnotation;

import com.google.common.base.Predicate;
import com.google.j2cl.transpiler.frontend.common.Nullability;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import javax.lang.model.AnnotatedConstruct;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;

/** Utility functions to process annotations. */
public final class AnnotationUtils {

@Nullable
static AnnotationMirror findAnnotationBindingByName(
List<? extends AnnotationMirror> annotations, String name) {
if (annotations == null) {
return null;
}
for (AnnotationMirror annotationBinding : annotations) {
if (getAnnotationName(annotationBinding).equals(name)) {
return annotationBinding;
}
}
return null;
static AnnotationMirror findAnnotationByName(AnnotatedConstruct annotatedConstruct, String name) {
return getAnnotation(annotatedConstruct, a -> getAnnotationName(a).equals(name));
}

static String getAnnotationName(AnnotationMirror annotationMirror) {
return ((TypeElement) annotationMirror.getAnnotationType().asElement())
.getQualifiedName()
.toString();
static String getAnnotationName(AnnotationMirror annotation) {
return ((TypeElement) annotation.getAnnotationType().asElement()).getQualifiedName().toString();
}

@Nullable
static String getAnnotationParameterString(AnnotationMirror annotationMirror, String paramName) {
if (annotationMirror == null) {
return null;
}
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> member :
annotationMirror.getElementValues().entrySet()) {
if (member.getKey().getSimpleName().contentEquals(paramName)) {
return (String) member.getValue().getValue();
}
}
return null;
static String getAnnotationParameterString(AnnotationMirror annotation, String paramName) {
var parameterValue = getAnnotationParameterValue(annotation, paramName);
return parameterValue != null ? (String) parameterValue : null;
}

@Nullable
static List<?> getAnnotationParameterArray(AnnotationMirror annotationMirror, String paramName) {
if (annotationMirror == null) {
return null;
}
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> member :
annotationMirror.getElementValues().entrySet()) {
if (member.getKey().getSimpleName().contentEquals(paramName)) {
if (member.getValue().getValue() instanceof List) {
return (List<?>) member.getValue().getValue();
}
}
}
return null;
static List<?> getAnnotationParameterArray(AnnotationMirror annotation, String paramName) {
var parameterValue = getAnnotationParameterValue(annotation, paramName);
return parameterValue instanceof List ? (List<?>) parameterValue : null;
}

static boolean getAnnotationParameterBoolean(
AnnotationMirror annotationMirror, String paramName, boolean defaultValue) {
if (annotationMirror == null) {
return defaultValue;
}
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> member :
annotationMirror.getElementValues().entrySet()) {
if (member.getKey().getSimpleName().contentEquals(paramName)) {
return (Boolean) member.getValue().getValue();
}
AnnotationMirror annotation, String paramName, boolean defaultValue) {
var parameterValue = getAnnotationParameterValue(annotation, paramName);
return parameterValue != null ? (Boolean) parameterValue : defaultValue;
}

@Nullable
static Object getAnnotationParameterValue(AnnotationMirror annotation, String paramName) {
if (annotation == null) {
return null;
}
return defaultValue;
return annotation.getElementValues().entrySet().stream()
.filter(e -> e.getKey().getSimpleName().contentEquals(paramName))
.map(Entry::getValue)
.map(AnnotationValue::getValue)
.collect(toOptional())
.orElse(null);
}

/** Returns true if the construct is annotated with {@code annotationSourceName}. */
static boolean hasAnnotation(AnnotatedConstruct construct, String annotationSourceName) {
return findAnnotationBindingByName(construct.getAnnotationMirrors(), annotationSourceName)
!= null;
return findAnnotationByName(construct, annotationSourceName) != null;
}

public static boolean isWarningSuppressed(AnnotatedConstruct annotatedConstruct, String warning) {
var annotation = getSuppressWarningsAnnotation(annotatedConstruct);
if (annotation == null) {
return false;
}

var suppressions = getAnnotationParameterArray(annotation, "value");
return suppressions.contains(warning);
}

@Nullable
public static AnnotationMirror getAnnotation(
AnnotatedConstruct annotatedConstruct, Predicate<? super AnnotationMirror> whichAnnotation) {

return annotatedConstruct.getAnnotationMirrors().stream()
.filter(whichAnnotation)
.findFirst()
.orElse(null);
}

public static boolean hasNullMarkedAnnotation(AnnotatedConstruct annotatedConstruct) {
AnnotationMirror annotation =
getAnnotation(
annotatedConstruct, a -> Nullability.isNullMarkedAnnotation(getAnnotationName(a)));
return annotation != null;
}

private AnnotationUtils() {}
Expand Down
Loading

0 comments on commit bc0971c

Please sign in to comment.