Java Code Examples for net.bytebuddy.description.type.TypeDescription#Latent

The following examples show how to use net.bytebuddy.description.type.TypeDescription#Latent . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HelperClassManager.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
static Class injectClass(@Nullable ClassLoader targetClassLoader, @Nullable ProtectionDomain pd, String className, boolean isBootstrapClass) throws IOException, ClassNotFoundException {
    if (targetClassLoader == null) {
        if (isBootstrapClass) {
            return Class.forName(className, false, null);
        } else {
            throw new UnsupportedOperationException("Cannot load non-bootstrap class from bootstrap class loader");
        }
    }

    ClassInjector classInjector;
    if (targetClassLoader == ClassLoader.getSystemClassLoader()) {
        classInjector = ClassInjector.UsingReflection.ofSystemClassLoader();
    } else {
        classInjector = new ClassInjector.UsingReflection(targetClassLoader, pd, PackageDefinitionStrategy.NoOp.INSTANCE,
            true);
    }
    final byte[] classBytes = getAgentClassBytes(className);
    final TypeDescription typeDesc =
        new TypeDescription.Latent(className, 0, null, Collections.<TypeDescription.Generic>emptyList());
    Map<TypeDescription, byte[]> typeMap = new HashMap<>();
    typeMap.put(typeDesc, classBytes);
    return classInjector.inject(typeMap).values().iterator().next();
}
 
Example 2
Source File: AbstractDynamicTypeBuilderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclaredAsMemberType() throws Exception {
    TypeDescription sample = new TypeDescription.Latent("foo.Bar$Qux", Opcodes.ACC_PUBLIC, TypeDescription.Generic.OBJECT) {
        @Override
        public String getSimpleName() {
            return "Qux";
        }

        @Override
        public boolean isAnonymousType() {
            return false;
        }

        @Override
        public boolean isMemberType() {
            return true;
        }
    };
    Class<?> outer = new ByteBuddy()
            .subclass(Object.class)
            .name("foo.Bar")
            .declaredTypes(sample)
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST.opened())
            .getLoaded();
    Class<?> type = createPlainWithoutValidation()
            .name(sample.getName())
            .innerTypeOf(outer).asMemberType()
            .make()
            .load((InjectionClassLoader) outer.getClassLoader(), InjectionClassLoader.Strategy.INSTANCE)
            .getLoaded();
    assertThat(type.getDeclaringClass(), is((Object) outer));
    assertThat(type.getEnclosingClass(), is((Object) outer));
    assertThat(type.getEnclosingMethod(), nullValue(Method.class));
    assertThat(type.getEnclosingConstructor(), nullValue(Constructor.class));
    assertThat(type.isAnonymousClass(), is(false));
    assertThat(type.isLocalClass(), is(false));
    assertThat(type.isMemberClass(), is(true));
}
 
Example 3
Source File: AbstractDynamicTypeBuilderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclaredAsAnonymousType() throws Exception {
    // Older JVMs derive the anonymous class property from a naming convention.
    TypeDescription sample = new TypeDescription.Latent("foo.Bar$1", Opcodes.ACC_PUBLIC, TypeDescription.Generic.OBJECT) {
        @Override
        public String getSimpleName() {
            return "";
        }

        @Override
        public boolean isAnonymousType() {
            return true;
        }

        @Override
        public boolean isMemberType() {
            return false;
        }
    };
    Class<?> outer = new ByteBuddy()
            .subclass(Object.class)
            .name("foo.Bar")
            .declaredTypes(sample)
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST.opened())
            .getLoaded();
    Class<?> type = createPlainWithoutValidation()
            .name(sample.getName())
            .innerTypeOf(outer).asAnonymousType()
            .make()
            .load((InjectionClassLoader) outer.getClassLoader(), InjectionClassLoader.Strategy.INSTANCE)
            .getLoaded();
    assertThat(type.getDeclaringClass(), nullValue(Class.class));
    assertThat(type.getEnclosingClass(), is((Object) outer));
    assertThat(type.getEnclosingMethod(), nullValue(Method.class));
    assertThat(type.getEnclosingConstructor(), nullValue(Constructor.class));
    assertThat(type.isAnonymousClass(), is(true));
    assertThat(type.isLocalClass(), is(false));
    assertThat(type.isMemberClass(), is(false));
}
 
Example 4
Source File: AbstractDynamicTypeBuilderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclaredAsLocalType() throws Exception {
    TypeDescription sample = new TypeDescription.Latent("foo.Bar$Qux", Opcodes.ACC_PUBLIC, TypeDescription.Generic.OBJECT) {
        @Override
        public String getSimpleName() {
            return "Qux";
        }

        @Override
        public boolean isAnonymousType() {
            return false;
        }

        @Override
        public boolean isMemberType() {
            return false;
        }
    };
    Class<?> outer = new ByteBuddy()
            .subclass(Object.class)
            .name("foo.Bar")
            .declaredTypes(sample)
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST.opened())
            .getLoaded();
    Class<?> type = createPlainWithoutValidation()
            .name(sample.getName())
            .innerTypeOf(outer)
            .make()
            .load((InjectionClassLoader) outer.getClassLoader(), InjectionClassLoader.Strategy.INSTANCE)
            .getLoaded();
    assertThat(type.getDeclaringClass(), nullValue(Class.class));
    assertThat(type.getEnclosingClass(), is((Object) outer));
    assertThat(type.getEnclosingMethod(), nullValue(Method.class));
    assertThat(type.getEnclosingConstructor(), nullValue(Constructor.class));
    assertThat(type.isAnonymousClass(), is(false));
    assertThat(type.isLocalClass(), is(true));
    assertThat(type.isMemberClass(), is(false));
}
 
Example 5
Source File: AbstractDynamicTypeBuilderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclaredAsAnonymousTypeInMethod() throws Exception {
    // Older JVMs derive the anonymous class property from a naming convention.
    TypeDescription sample = new TypeDescription.Latent("foo.Bar$1", Opcodes.ACC_PUBLIC, TypeDescription.Generic.OBJECT) {
        @Override
        public String getSimpleName() {
            return "";
        }

        @Override
        public boolean isAnonymousType() {
            return true;
        }

        @Override
        public boolean isMemberType() {
            return false;
        }
    };
    Class<?> outer = new ByteBuddy()
            .subclass(Object.class)
            .name("foo.Bar")
            .declaredTypes(sample)
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST.opened())
            .getLoaded();
    Class<?> type = createPlainWithoutValidation()
            .name(sample.getName())
            .innerTypeOf(outer.getConstructor()).asAnonymousType()
            .make()
            .load((InjectionClassLoader) outer.getClassLoader(), InjectionClassLoader.Strategy.INSTANCE)
            .getLoaded();
    assertThat(type.getDeclaringClass(), nullValue(Class.class));
    assertThat(type.getEnclosingClass(), is((Object) outer));
    assertThat(type.getEnclosingMethod(), nullValue(Method.class));
    assertThat(type.getEnclosingConstructor(), is((Object) outer.getConstructor()));
    assertThat(type.isAnonymousClass(), is(true));
    assertThat(type.isLocalClass(), is(false));
    assertThat(type.isMemberClass(), is(false));
}
 
Example 6
Source File: AbstractDynamicTypeBuilderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclaredAsLocalTypeInInitializer() throws Exception {
    // Older JVMs derive the anonymous class property from a naming convention.
    TypeDescription sample = new TypeDescription.Latent("foo.Bar$Qux", Opcodes.ACC_PUBLIC, TypeDescription.Generic.OBJECT) {
        @Override
        public String getSimpleName() {
            return "Qux";
        }

        @Override
        public boolean isAnonymousType() {
            return false;
        }

        @Override
        public boolean isMemberType() {
            return false;
        }
    };
    Class<?> outer = new ByteBuddy()
            .subclass(Object.class)
            .name("foo.Bar")
            .declaredTypes(sample)
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST.opened())
            .getLoaded();
    Class<?> type = createPlainWithoutValidation()
            .name(sample.getName())
            .innerTypeOf(new MethodDescription.Latent.TypeInitializer(TypeDescription.ForLoadedType.of(outer)))
            .make()
            .load((InjectionClassLoader) outer.getClassLoader(), InjectionClassLoader.Strategy.INSTANCE)
            .getLoaded();
    assertThat(type.getDeclaringClass(), nullValue(Class.class));
    assertThat(type.getEnclosingClass(), is((Object) outer));
    assertThat(type.getEnclosingMethod(), nullValue(Method.class));
    assertThat(type.getEnclosingConstructor(), nullValue(Constructor.class));
    assertThat(type.isAnonymousClass(), is(false));
    assertThat(type.isLocalClass(), is(true));
    assertThat(type.isMemberClass(), is(false));
}
 
Example 7
Source File: AbstractDynamicTypeBuilderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclaredAsAnonymousTypeInInitializer() throws Exception {
    // Older JVMs derive the anonymous class property from a naming convention.
    TypeDescription sample = new TypeDescription.Latent("foo.Bar$1", Opcodes.ACC_PUBLIC, TypeDescription.Generic.OBJECT) {
        @Override
        public String getSimpleName() {
            return "";
        }

        @Override
        public boolean isAnonymousType() {
            return true;
        }

        @Override
        public boolean isMemberType() {
            return false;
        }
    };
    Class<?> outer = new ByteBuddy()
            .subclass(Object.class)
            .name("foo.Bar")
            .declaredTypes(sample)
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST.opened())
            .getLoaded();
    Class<?> type = createPlainWithoutValidation()
            .name(sample.getName())
            .innerTypeOf(new MethodDescription.Latent.TypeInitializer(TypeDescription.ForLoadedType.of(outer))).asAnonymousType()
            .make()
            .load((InjectionClassLoader) outer.getClassLoader(), InjectionClassLoader.Strategy.INSTANCE)
            .getLoaded();
    assertThat(type.getDeclaringClass(), nullValue(Class.class));
    assertThat(type.getEnclosingClass(), is((Object) outer));
    assertThat(type.getEnclosingMethod(), nullValue(Method.class));
    assertThat(type.getEnclosingConstructor(), nullValue(Constructor.class));
    assertThat(type.isAnonymousClass(), is(true));
    assertThat(type.isLocalClass(), is(false));
    assertThat(type.isMemberClass(), is(false));
}
 
Example 8
Source File: AbstractDynamicTypeBuilderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclaredAsLocalTypeInConstructor() throws Exception {
    TypeDescription sample = new TypeDescription.Latent("foo.Bar$Qux", Opcodes.ACC_PUBLIC, TypeDescription.Generic.OBJECT) {
        @Override
        public String getSimpleName() {
            return "Qux";
        }

        @Override
        public boolean isAnonymousType() {
            return false;
        }

        @Override
        public boolean isMemberType() {
            return false;
        }
    };
    Class<?> outer = new ByteBuddy()
            .subclass(Object.class)
            .name("foo.Bar")
            .declaredTypes(sample)
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST.opened())
            .getLoaded();
    Class<?> type = createPlainWithoutValidation()
            .name(sample.getName())
            .innerTypeOf(outer.getConstructor())
            .make()
            .load((InjectionClassLoader) outer.getClassLoader(), InjectionClassLoader.Strategy.INSTANCE)
            .getLoaded();
    assertThat(type.getDeclaringClass(), nullValue(Class.class));
    assertThat(type.getEnclosingClass(), is((Object) outer));
    assertThat(type.getEnclosingMethod(), nullValue(Method.class));
    assertThat(type.getEnclosingConstructor(), is((Object) outer.getConstructor()));
    assertThat(type.isAnonymousClass(), is(false));
    assertThat(type.isLocalClass(), is(true));
    assertThat(type.isMemberClass(), is(false));
}
 
Example 9
Source File: MapReduceTracer.java    From garmadon with Apache License 2.0 4 votes vote down vote up
Types(String name, int modifiers, TypeDescription.Generic superClass) {
    this.typeDescription = new TypeDescription.Latent(name, modifiers, superClass);
}
 
Example 10
Source File: AbstractDynamicTypeBuilderTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeclaredAsLocalTypeInMethod() throws Exception {
    TypeDescription sample = new TypeDescription.Latent("foo.Bar$Qux", Opcodes.ACC_PUBLIC, TypeDescription.Generic.OBJECT) {
        @Override
        public String getSimpleName() {
            return "Qux";
        }

        @Override
        public boolean isAnonymousType() {
            return false;
        }

        @Override
        public boolean isMemberType() {
            return false;
        }
    };
    Class<?> outer = new ByteBuddy()
            .subclass(Object.class)
            .name("foo.Bar")
            .defineMethod("foo", void.class, Visibility.PUBLIC)
            .intercept(StubMethod.INSTANCE)
            .declaredTypes(sample)
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST.opened())
            .getLoaded();
    Class<?> type = createPlainWithoutValidation()
            .name(sample.getName())
            .innerTypeOf(outer.getMethod(FOO))
            .make()
            .load((InjectionClassLoader) outer.getClassLoader(), InjectionClassLoader.Strategy.INSTANCE)
            .getLoaded();
    assertThat(type.getDeclaringClass(), nullValue(Class.class));
    assertThat(type.getEnclosingClass(), is((Object) outer));
    assertThat(type.getEnclosingMethod(), is(outer.getMethod(FOO)));
    assertThat(type.getEnclosingConstructor(), nullValue(Constructor.class));
    assertThat(type.isAnonymousClass(), is(false));
    assertThat(type.isLocalClass(), is(true));
    assertThat(type.isMemberClass(), is(false));
}