Skip to content

Commit 3c8c7f1

Browse files
committed
Clean up code.
1 parent 3f969d1 commit 3c8c7f1

File tree

6 files changed

+53
-22
lines changed

6 files changed

+53
-22
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/description/type/TypeDescription.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public interface TypeDescription extends TypeDefinition, ByteCodeElement, TypeVa
341341
*
342342
* @return {@code true} if the represented type includes a module description.
343343
*/
344-
boolean isModule();
344+
boolean isModuleType();
345345

346346
/**
347347
* Returns the annotations that this type declares or inherits from super types.
@@ -8017,7 +8017,7 @@ public String getInternalName() {
80178017
* {@inheritDoc}
80188018
*/
80198019
public int getActualModifiers(boolean superFlag) {
8020-
if (isModule()) {
8020+
if (isModuleType()) {
80218021
return Opcodes.ACC_MODULE;
80228022
}
80238023
int actualModifiers = getModifiers()
@@ -8072,8 +8072,8 @@ public String getGenericSignature() {
80728072
/**
80738073
* {@inheritDoc}
80748074
*/
8075-
public boolean isModule() {
8076-
return toModuleDescription() != null;
8075+
public boolean isModuleType() {
8076+
return getName().equals(ModuleDescription.MODULE_CLASS_NAME);
80778077
}
80788078

80798079
/**

byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/DynamicType.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5779,8 +5779,8 @@ public OpensDefinition<U> open(String aPackage, int modifiers) {
57795779
* {@inheritDoc}
57805780
*/
57815781
public ModuleDefinition<U> uses(Collection<String> services) {
5782-
Set<String> merged = new LinkedHashSet<String>(this.packages);
5783-
merged.addAll(packages);
5782+
Set<String> uses = new LinkedHashSet<String>(this.uses);
5783+
uses.addAll(services);
57845784
return new ModuleDefinitionAdapter(name,
57855785
modifiers,
57865786
version,
@@ -5789,7 +5789,7 @@ public ModuleDefinition<U> uses(Collection<String> services) {
57895789
requires,
57905790
exports,
57915791
opens,
5792-
merged,
5792+
uses,
57935793
provides);
57945794
}
57955795

byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/InstrumentedType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,8 @@ public TypeDescription validated() {
17561756
for (AnnotationDescription annotationDescription : getDeclaredAnnotations()) {
17571757
if (!annotationDescription.isSupportedOn(ElementType.TYPE)
17581758
&& !(isAnnotation() && annotationDescription.isSupportedOn(ElementType.ANNOTATION_TYPE))
1759-
&& !(isPackageType() && annotationDescription.isSupportedOn(ElementType.PACKAGE))) {
1759+
&& !(isPackageType() && annotationDescription.isSupportedOn(ElementType.PACKAGE))
1760+
&& !(isModuleType() && annotationDescription.isSupportedOn("MODULE"))) {
17601761
throw new IllegalStateException("Cannot add " + annotationDescription + " on " + this);
17611762
} else if (!typeAnnotationTypes.add(annotationDescription.getAnnotationType())) {
17621763
throw new IllegalStateException("Duplicate annotation " + annotationDescription + " for " + this);

byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/TypeWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5081,7 +5081,7 @@ public void visit(int classFileVersionNumber,
50815081
| resolveDeprecationModifiers(modifiers)
50825082
// Anonymous types might not preserve their class file's final modifier via their inner class modifier.
50835083
| (((modifiers & Opcodes.ACC_FINAL) != 0 && instrumentedType.isAnonymousType()) ? Opcodes.ACC_FINAL : ModifierContributor.EMPTY_MASK)
5084-
| (instrumentedType.isModule() ? Opcodes.ACC_MODULE : ModifierContributor.EMPTY_MASK),
5084+
| (instrumentedType.isModuleType() ? Opcodes.ACC_MODULE : ModifierContributor.EMPTY_MASK),
50855085
instrumentedType.getInternalName(),
50865086
TypeDescription.AbstractBase.RAW_TYPES
50875087
? genericSignature

byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3423,7 +3423,7 @@ public ModuleDescription toModuleDescription() {
34233423
}
34243424

34253425
@Override
3426-
public boolean isModule() {
3426+
public boolean isModuleType() {
34273427
return moduleToken != null;
34283428
}
34293429

byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/scaffold/subclass/SubclassDynamicTypeBuilderTest.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@
3333
import org.objectweb.asm.Opcodes;
3434

3535
import java.lang.annotation.Annotation;
36+
import java.lang.annotation.ElementType;
3637
import java.lang.annotation.Retention;
3738
import java.lang.annotation.RetentionPolicy;
39+
import java.lang.annotation.Target;
3840
import java.lang.reflect.AnnotatedElement;
3941
import java.lang.reflect.Constructor;
4042
import java.lang.reflect.Method;
4143
import java.lang.reflect.Modifier;
4244
import java.lang.reflect.Type;
4345
import java.util.ArrayList;
46+
import java.util.Collections;
4447
import java.util.List;
4548
import java.util.Map;
4649

@@ -51,6 +54,7 @@
5154
import static org.hamcrest.CoreMatchers.is;
5255
import static org.hamcrest.CoreMatchers.not;
5356
import static org.hamcrest.CoreMatchers.notNullValue;
57+
import static org.hamcrest.CoreMatchers.nullValue;
5458
import static org.hamcrest.CoreMatchers.sameInstance;
5559
import static org.hamcrest.MatcherAssert.assertThat;
5660

@@ -225,31 +229,57 @@ public void testPackageDefinition() throws Exception {
225229
@Test
226230
@JavaVersionRule.Enforce(9)
227231
public void testModuleDefinition() throws Exception {
232+
DynamicType.Builder<? extends Annotation> builder = new ByteBuddy()
233+
.makeAnnotation()
234+
.name(BAR + "." + FOO + BAR)
235+
.annotateType(AnnotationDescription.Builder.ofType(Retention.class)
236+
.define("value", RetentionPolicy.RUNTIME)
237+
.build())
238+
.annotateType(AnnotationDescription.Builder.ofType(Target.class)
239+
.defineEnumerationArray("value", ElementType.class, ElementType.valueOf("MODULE"))
240+
.build());
228241
Class<?> type = new ByteBuddy()
229242
.subclass(Object.class)
230243
.name(BAR + "." + QUX)
231244
.make()
232-
.include(new ByteBuddy()
233-
.makeAnnotation()
234-
.name(Foo.class.getName())
235-
.annotateType(AnnotationDescription.Builder.ofType(Retention.class)
236-
.define("value", RetentionPolicy.RUNTIME)
237-
.build())
238-
.make())
245+
.include(builder.make())
239246
.include(new ByteBuddy()
240247
.makeModule(FOO)
241248
.version("1")
249+
.mainClass(BAR + "." + QUX)
242250
.packages(BAR)
243251
.export(BAR)
244-
.annotateType(AnnotationDescription.Builder.ofType(Foo.class).build())
252+
.open(BAR)
253+
.uses(Runnable.class)
254+
.provides(Runnable.class.getName(), BAR + "." + QUX)
255+
.annotateType(AnnotationDescription.Builder.ofType(builder.toTypeDescription()).build())
245256
.make())
246257
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
247-
.getLoaded(); // TODO: filter module-info
258+
.getLoaded();
248259
ModuleDescription moduleDescription = ModuleDescription.ForLoadedModule.of(Class.class.getMethod("getModule").invoke(type));
249-
assertThat(moduleDescription.getActualName(), is(ModuleDescription.MODULE_CLASS_NAME));
260+
assertThat(moduleDescription.getActualName(), is(FOO));
250261
assertThat(moduleDescription.getModifiers(), is(ModifierContributor.EMPTY_MASK));
251-
assertThat(moduleDescription.getDeclaredAnnotations().size(), is(1));
252-
assertThat(moduleDescription.getDeclaredAnnotations().get(0).getAnnotationType().getName(), is(Foo.class.getName()));
262+
assertThat(moduleDescription.getVersion(), is("1"));
263+
assertThat(moduleDescription.getMainClass(), is(BAR + "." + QUX));
264+
assertThat(moduleDescription.getRequires().size(), is(1));
265+
assertThat(moduleDescription.getRequires().get("java.base"), notNullValue(ModuleDescription.Requires.class));
266+
assertThat(moduleDescription.getRequires().get("java.base").getModifiers(), is(Opcodes.ACC_MANDATED));
267+
assertThat(moduleDescription.getRequires().get("java.base").getVersion(), nullValue(String.class));
268+
assertThat(moduleDescription.getExports().size(), is(1));
269+
assertThat(moduleDescription.getExports().get(BAR), notNullValue(ModuleDescription.Exports.class));
270+
assertThat(moduleDescription.getExports().get(BAR).getModifiers(), is(0));
271+
assertThat(moduleDescription.getExports().get(BAR).getTargets().size(), is(0));
272+
assertThat(moduleDescription.getOpens().size(), is(1));
273+
assertThat(moduleDescription.getOpens().get(BAR), notNullValue(ModuleDescription.Opens.class));
274+
assertThat(moduleDescription.getOpens().get(BAR).getModifiers(), is(0));
275+
assertThat(moduleDescription.getOpens().get(BAR).getTargets().size(), is(0));
276+
assertThat(moduleDescription.getUses().size(), is(1));
277+
assertThat(moduleDescription.getUses().contains(Runnable.class.getName()), is(true));
278+
assertThat(moduleDescription.getProvides().size(), is(1));
279+
assertThat(moduleDescription.getProvides().get(Runnable.class.getName()), notNullValue(ModuleDescription.Provides.class));
280+
assertThat(moduleDescription.getProvides().get(Runnable.class.getName()).getProviders(), is(Collections.singleton(BAR + "." + QUX)));
281+
// assertThat(moduleDescription.getDeclaredAnnotations().size(), is(1)); TODO: why are these missing?
282+
// assertThat(moduleDescription.getDeclaredAnnotations().get(0).getAnnotationType().getName(), is(BAR + "." + FOO + BAR));
253283
}
254284

255285
@Test

0 commit comments

Comments
 (0)