Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated discovery of JAX-RS method entities. #3358

Merged
merged 1 commit into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
// all classes, fields and methods annotated with @Reflected
addAnnotatedWithReflected(context);

// all classes used as return types and parameters in JAX-RS resources
processJaxRsTypes(context);

// JAX-RS types required for headers, query params etc.
addJaxRsConversions(context);

Expand Down Expand Up @@ -222,6 +225,17 @@ private void addJaxRsConversions(BeforeAnalysisContext context, String annotatio
}
}

private void processJaxRsTypes(BeforeAnalysisContext context) {
tracer.parsing(() -> "Looking up JAX-RS resource methods.");

new JaxRsMethodAnalyzer(context, util)
tomas-langer marked this conversation as resolved.
Show resolved Hide resolved
.find()
.forEach(it -> {
tracer.parsing(() -> " class " + it);
context.register(it).addAll();
});
}

private void addAnnotatedWithReflected(BeforeAnalysisContext context) {
// want to make sure we use the correct classloader
String annotation = Reflected.class.getName();
Expand Down Expand Up @@ -461,7 +475,7 @@ private void superclasses(BeforeAnalysisContext context, Class<?> aClass) {
}
}

private final class BeforeAnalysisContext {
final class BeforeAnalysisContext {
private final BeforeAnalysisAccess access;
private final Set<Class<?>> processed = new HashSet<>();
private final Set<Class<?>> excluded = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.graal.nativeimage.extension;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import io.github.classgraph.ClassInfo;
import io.github.classgraph.ClassInfoList;
import io.github.classgraph.ClassRefTypeSignature;
import io.github.classgraph.MethodInfo;
import io.github.classgraph.MethodInfoList;
import io.github.classgraph.MethodParameterInfo;
import io.github.classgraph.TypeArgument;
import io.github.classgraph.TypeSignature;

/*
* Analyzes JAX-RS resource method signatures and returns all types to be added for reflection.
*/
class JaxRsMethodAnalyzer {
/*
what we ignore:
- JsonObject and JsonArray - part of JSON-P handling, which works without reflection
- Response - part of JAX-RS and works already
- String - no need to add it
- Map, List, Set - basic collection classes - we only care about the type parameter, not the collection itself
*/
private static final Set<String> IGNORED_TYPES = Set.of("javax.json.JsonObject",
"javax.json.JsonArray",
"javax.ws.rs.core.Response",
"java.lang.String",
Map.class.getName(),
List.class.getName(),
Set.class.getName());

private static final String HTTP_METHOD_ANNOTATION = "javax.ws.rs.HttpMethod";

private final Set<String> classesToAdd = new HashSet<>();
private final HelidonReflectionFeature.BeforeAnalysisContext context;
private final NativeUtil nativeUtil;

JaxRsMethodAnalyzer(HelidonReflectionFeature.BeforeAnalysisContext context,
NativeUtil nativeUtil) {
this.context = context;
this.nativeUtil = nativeUtil;
}

Set<Class<?>> find() {
ClassInfoList classes = context.scan()
.getClassesWithMethodAnnotation(HTTP_METHOD_ANNOTATION);

for (ClassInfo aClass : classes) {
MethodInfoList methods = aClass.getMethodInfo();
for (MethodInfo method : methods) {
if (method.hasAnnotation(HTTP_METHOD_ANNOTATION)) {
add(method.getTypeSignatureOrTypeDescriptor()
.getResultType());

MethodParameterInfo[] parameterInfo = method.getParameterInfo();
for (MethodParameterInfo param : parameterInfo) {
if (param.getAnnotationInfo().isEmpty()) {
add(param.getTypeSignatureOrTypeDescriptor());
}
}
}
}
}
Set<String> result = Set.copyOf(classesToAdd);
classesToAdd.clear();

return result.stream()
.map(nativeUtil.classMapper("jaxrs-result-or-param"))
.filter(Objects::nonNull)
.filter(nativeUtil.inclusionFilter("jaxrs-result-or-param"))
.collect(Collectors.toSet());
}

void add(TypeSignature type) {
if (type instanceof ClassRefTypeSignature) {
ClassRefTypeSignature crts = (ClassRefTypeSignature) type;
if (add(crts.getFullyQualifiedClassName())) {
List<TypeArgument> typeArgs = crts.getTypeArguments();
if (typeArgs != null && !typeArgs.isEmpty()) {
typeArgs.forEach(it -> add(it.getTypeSignature()));
}
}
} else {
add(type.toString());
}
}

boolean add(String type) {
if (!IGNORED_TYPES.contains(type)) {
return classesToAdd.add(type);
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ Set<Class<?>> findSubclasses(String superclassName) {
}
}

Predicate<Class<?>> inclusionFilter(String description) {
return new InclusionFilter(tracer, exclusion, description);
}

Function<String, Class<?>> classMapper(String description) {
return new StringClassResolverMapper(tracer, classResolver, description);
}

private static class InclusionFilter implements Predicate<Class<?>> {
private final NativeTrace tracer;
private final Function<Class<?>, Boolean> exclusion;
Expand All @@ -301,6 +309,27 @@ public boolean test(Class<?> aClass) {
}
}

private static class StringClassResolverMapper implements Function<String, Class<?>> {
private final NativeTrace tracer;
private final Function<String, Class<?>> classResolver;
private final String message;

StringClassResolverMapper(NativeTrace tracer, Function<String, Class<?>> classResolver, String message) {
this.tracer = tracer;
this.classResolver = classResolver;
this.message = message;
}

@Override
public Class<?> apply(String className) {
Class<?> clazz = classResolver.apply(className);
if (clazz == null) {
tracer.parsing(() -> " class " + className + " " + message + " is not on classpath.");
}
return clazz;
}
}

private static class ClassResolverMapper implements Function<ClassInfo, Class<?>> {
private final NativeTrace tracer;
private final Function<String, Class<?>> classResolver;
Expand Down