|
| 1 | +package org.codehaus.plexus.languages.java.jpms; |
| 2 | + |
| 3 | +/* |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.lang.classfile.ClassFile; |
| 25 | +import java.lang.classfile.ClassModel; |
| 26 | +import java.lang.classfile.attribute.ModuleAttribute; |
| 27 | +import java.lang.classfile.attribute.ModuleExportInfo; |
| 28 | +import java.lang.classfile.attribute.ModuleProvidesInfo; |
| 29 | +import java.lang.classfile.attribute.ModuleRequiresInfo; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.LinkedHashSet; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Set; |
| 35 | + |
| 36 | +import org.codehaus.plexus.languages.java.jpms.JavaModuleDescriptor.JavaRequires.JavaModifier; |
| 37 | + |
| 38 | +/** |
| 39 | + * Extract information from module using the Class File API |
| 40 | + * |
| 41 | + * @author Robert Scholte |
| 42 | + * @since 1.5.0 |
| 43 | + */ |
| 44 | +class ClassFileApiModuleInfoParser extends AbstractBinaryModuleInfoParser { |
| 45 | + @Override |
| 46 | + JavaModuleDescriptor parse(InputStream in) throws IOException { |
| 47 | + byte[] bytes = in.readAllBytes(); |
| 48 | + ClassModel classModel = ClassFile.of().parse(bytes); |
| 49 | + |
| 50 | + ModuleAttribute moduleAttr = classModel |
| 51 | + .findAttribute(java.lang.classfile.Attributes.module()) |
| 52 | + .orElseThrow(() -> new IOException("Not a module-info class file")); |
| 53 | + |
| 54 | + JavaModuleDescriptor.Builder builder = |
| 55 | + JavaModuleDescriptor.newModule(moduleAttr.moduleName().name().stringValue()); |
| 56 | + |
| 57 | + // Process requires |
| 58 | + for (ModuleRequiresInfo requiresInfo : moduleAttr.requires()) { |
| 59 | + String moduleName = requiresInfo.requires().name().stringValue(); |
| 60 | + int flags = requiresInfo.requiresFlagsMask(); |
| 61 | + |
| 62 | + boolean isStatic = (flags & ClassFile.ACC_STATIC_PHASE) != 0; |
| 63 | + boolean isTransitive = (flags & ClassFile.ACC_TRANSITIVE) != 0; |
| 64 | + |
| 65 | + if (isStatic || isTransitive) { |
| 66 | + Set<JavaModifier> modifiers = new LinkedHashSet<>(); |
| 67 | + if (isStatic) { |
| 68 | + modifiers.add(JavaModifier.STATIC); |
| 69 | + } |
| 70 | + if (isTransitive) { |
| 71 | + modifiers.add(JavaModifier.TRANSITIVE); |
| 72 | + } |
| 73 | + builder.requires(modifiers, moduleName); |
| 74 | + } else { |
| 75 | + builder.requires(moduleName); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Process exports |
| 80 | + for (ModuleExportInfo exportInfo : moduleAttr.exports()) { |
| 81 | + String packageName = |
| 82 | + exportInfo.exportedPackage().name().stringValue().replace('/', '.'); |
| 83 | + if (exportInfo.exportsTo().isEmpty()) { |
| 84 | + builder.exports(packageName); |
| 85 | + } else { |
| 86 | + Set<String> targets = new HashSet<>(); |
| 87 | + exportInfo |
| 88 | + .exportsTo() |
| 89 | + .forEach(target -> targets.add(target.name().stringValue())); |
| 90 | + builder.exports(packageName, targets); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Process uses |
| 95 | + moduleAttr.uses().forEach(usesInfo -> { |
| 96 | + String serviceName = usesInfo.name().stringValue().replace('/', '.'); |
| 97 | + builder.uses(serviceName); |
| 98 | + }); |
| 99 | + |
| 100 | + // Process provides |
| 101 | + for (ModuleProvidesInfo providesInfo : moduleAttr.provides()) { |
| 102 | + String serviceName = providesInfo.provides().name().stringValue().replace('/', '.'); |
| 103 | + List<String> providers = new ArrayList<>(); |
| 104 | + providesInfo |
| 105 | + .providesWith() |
| 106 | + .forEach(provider -> |
| 107 | + providers.add(provider.name().stringValue().replace('/', '.'))); |
| 108 | + builder.provides(serviceName, providers); |
| 109 | + } |
| 110 | + |
| 111 | + return builder.build(); |
| 112 | + } |
| 113 | +} |
0 commit comments