|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage; |
| 18 | + |
| 19 | +import com.google.protobuf.AbstractMessage; |
| 20 | +import com.google.protobuf.GeneratedMessageV3; |
| 21 | +import com.google.protobuf.ProtocolMessageEnum; |
| 22 | +import io.github.classgraph.ClassGraph; |
| 23 | +import io.github.classgraph.ClassInfo; |
| 24 | +import io.github.classgraph.ScanResult; |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.nio.file.Paths; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +public final class GenerateGrpcProtobufReflectConfig { |
| 34 | + |
| 35 | + public static void main(String[] args) throws IOException { |
| 36 | + try (ScanResult scanResult = |
| 37 | + new ClassGraph().enableAllInfo().acceptPackages("io.grpc").scan()) { |
| 38 | + String json = |
| 39 | + Stream.of( |
| 40 | + Stream.of( |
| 41 | + "{\n" |
| 42 | + + " \"name\":\"org.apache.commons.logging.LogFactory\",\n" |
| 43 | + + " \"allDeclaredFields\":true,\n" |
| 44 | + + " \"allDeclaredMethods\":true,\n" |
| 45 | + + " \"allDeclaredConstructors\": true\n" |
| 46 | + + " }", |
| 47 | + "{\n" |
| 48 | + + " \"name\":\"org.apache.commons.logging.impl.Jdk14Logger\",\n" |
| 49 | + + " \"methods\":[{\"name\":\"<init>\",\"parameterTypes\":[\"java.lang.String\"] }]\n" |
| 50 | + + " }", |
| 51 | + "{\n" |
| 52 | + + " \"name\":\"org.apache.commons.logging.impl.LogFactoryImpl\",\n" |
| 53 | + + " \"allDeclaredFields\":true,\n" |
| 54 | + + " \"allDeclaredMethods\":true,\n" |
| 55 | + + " \"methods\":[{\"name\":\"<init>\",\"parameterTypes\":[] }]\n" |
| 56 | + + " }"), |
| 57 | + Stream.of( |
| 58 | + scanResult.getSubclasses(GeneratedMessageV3.class).stream(), |
| 59 | + scanResult.getSubclasses(AbstractMessage.Builder.class).stream(), |
| 60 | + scanResult.getAllEnums() |
| 61 | + .filter(ci -> ci.implementsInterface(ProtocolMessageEnum.class)) |
| 62 | + .stream()) |
| 63 | + .flatMap(s -> s) |
| 64 | + .map(ClassInfo::getName) |
| 65 | + .sorted() |
| 66 | + .map( |
| 67 | + name -> |
| 68 | + String.format( |
| 69 | + "{ \"name\": \"%s\", \"queryAllDeclaredConstructors\": true, \"queryAllPublicConstructors\": true, \"queryAllDeclaredMethods\": true, \"allPublicMethods\": true, \"allDeclaredClasses\": true, \"allPublicClasses\": true }", |
| 70 | + name))) |
| 71 | + .flatMap(s -> s) |
| 72 | + .collect(Collectors.joining(",\n ", "[\n ", "\n]\n")); |
| 73 | + String workingDirectory = System.getProperty("user.dir"); // should be google-cloud-storage |
| 74 | + String testResourcesPath = "src/test/resources"; |
| 75 | + String reflectConfigResourcePath = |
| 76 | + "META-INF/native-image/com/google/cloud/storage/reflect-config.json"; |
| 77 | + Path path = Paths.get(workingDirectory, testResourcesPath, reflectConfigResourcePath); |
| 78 | + System.err.println("Writing reflect-config.json at path: " + path); |
| 79 | + Files.write(path, json.getBytes(StandardCharsets.UTF_8)); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments