net.bytebuddy.description.modifier.FieldManifestation Java Examples

The following examples show how to use net.bytebuddy.description.modifier.FieldManifestation. 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: ByteBuddyProcessFunctionInvoker.java    From da-streamingledger with Apache License 2.0 5 votes vote down vote up
private static Builder<?> configureByteBuddyBuilder(
        PackageLocalNamingStrategy generatedTypeName,
        TypeDefinition generatedType,
        TypeDefinition processFunctionType,
        ForLoadedConstructor superTypeConstructor,
        MethodDescription processMethodType,
        int numberOfStateBindings) {

    return new ByteBuddy()
            // final class <Name> extends <ProcessFunctionInvoker> {
            .with(generatedTypeName)
            .subclass(generatedType, ConstructorStrategy.Default.NO_CONSTRUCTORS).modifiers(Modifier.FINAL)
            // private final <processFunction class> delegate;
            .defineField("delegate", processFunctionType, Visibility.PRIVATE, FieldManifestation.FINAL)
            // public <Name>(<processFunction class> delegate) {
            //     super();
            //     this.delegate = delegate;
            // }
            .defineConstructor(Modifier.PUBLIC)
            .withParameters(processFunctionType)
            .intercept(MethodCall.invoke(superTypeConstructor)
                    .andThen(FieldAccessor.ofField("delegate").setsArgumentAt(0))
            )
            // invoke(input, context, StateAccess[] arguments) {
            //      this.delegate.invoke(input, context, arguments[0], arguments[1], .. arguments[n - 1]);
            // }
            .method(ElementMatchers.named("invoke"))
            .intercept(MethodCall.invoke(processMethodType)
                    .onField("delegate")
                    .withArgument(0, 1) // event & context
                    .withArgumentArrayElements(2, numberOfStateBindings) // StateAccess
                    .withAssigner(Assigner.DEFAULT, Typing.STATIC)
            );
}
 
Example #2
Source File: FieldAccessorOtherTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgumentSetterConstructor() throws Exception {
    Class<?> loaded = new ByteBuddy()
            .subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS)
            .defineField(FOO, String.class, Visibility.PUBLIC, FieldManifestation.FINAL)
            .defineConstructor(Visibility.PUBLIC)
            .withParameters(String.class)
            .intercept(MethodCall.invoke(Object.class.getDeclaredConstructor()).andThen(FieldAccessor.ofField(FOO).setsArgumentAt(0)))
            .make()
            .load(null, ClassLoadingStrategy.Default.WRAPPER)
            .getLoaded();
    assertThat(loaded.getDeclaredField(FOO).get(loaded.getDeclaredConstructor(String.class).newInstance(FOO)), is((Object) FOO));
}
 
Example #3
Source File: AbstractAnnotationFrameFactory.java    From Ferma with Apache License 2.0 4 votes vote down vote up
private <E> Class<? extends E> constructClass(final Element element, final Class<E> clazz) {
    Class constructedClass = constructedClassCache.get(clazz);
    if (constructedClass != null)
        return constructedClass;

    DynamicType.Builder<? extends E> classBuilder;
    if (clazz.isInterface())
        if (element instanceof Vertex)
            classBuilder = (DynamicType.Builder<? extends E>) new ByteBuddy().subclass(AbstractVertexFrame.class).implement(clazz);
        else if (element instanceof Edge)
            classBuilder = (DynamicType.Builder<? extends E>) new ByteBuddy().subclass(AbstractEdgeFrame.class).implement(clazz);
        else
            throw new IllegalStateException("class is neither an Edge or a vertex!");
    else {
        if( !(element instanceof Vertex || element instanceof Edge) )
            throw new IllegalStateException("element is neither an edge nor a vertex");
        else if (element instanceof Vertex && !VertexFrame.class.isAssignableFrom(clazz))
            throw new IllegalStateException(clazz.getName() + " Class is not a type of VertexFrame");
        else if (element instanceof Edge && !EdgeFrame.class.isAssignableFrom(clazz))
            throw new IllegalStateException(clazz.getName() + " Class is not a type of EdgeFrame");
        classBuilder = new ByteBuddy().subclass(clazz);
    }

    classBuilder = classBuilder.defineField("reflectionCache", ReflectionCache.class, Visibility.PRIVATE, FieldManifestation.PLAIN).implement(CachesReflection.class).intercept(FieldAccessor.
          ofBeanProperty());

    //try and construct any abstract methods that are left
    for (final Method method : clazz.getMethods())
        if (isAbstract(method))
            annotation_loop:
            for (final Annotation annotation : method.getAnnotations()) {
                final MethodHandler handler = methodHandlers.get(annotation.annotationType());
                if (handler != null) {
                    classBuilder = handler.processMethod(classBuilder, method, annotation);
                    break;
                }
            }

    constructedClass = classBuilder.make().load(AnnotationFrameFactory.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    this.constructedClassCache.put(clazz, constructedClass);
    return constructedClass;
}
 
Example #4
Source File: AbstractAnnotationFrameFactory.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
private <E> Class<? extends E> constructClass(final Element element, final Class<E> clazz)
{
    Class constructedClass = constructedClassCache.get(clazz);
    if (constructedClass != null)
        return constructedClass;

    DynamicType.Builder<? extends E> classBuilder;
    if (clazz.isInterface())
    {
        if (element instanceof Vertex)
            classBuilder = (DynamicType.Builder<? extends E>) new ByteBuddy().subclass(AbstractVertexFrame.class);
        else if (element instanceof Edge)
            classBuilder = (DynamicType.Builder<? extends E>) new ByteBuddy().subclass(AbstractEdgeFrame.class);
        else
            throw new IllegalStateException("class is neither an Edge or a vertex!");

        if (clazz.getCanonicalName().contains("ByteBuddy"))
        {
            // if the input class is itself a bytebuddy class, only take its interfaces
            classBuilder = classBuilder.implement(clazz.getInterfaces());
        }
        else
        {
            classBuilder = classBuilder.implement(clazz);
        }
    }
    else
    {
        if (!(element instanceof Vertex || element instanceof Edge))
            throw new IllegalStateException("element is neither an edge nor a vertex");
        else if (element instanceof Vertex && !VertexFrame.class.isAssignableFrom(clazz))
            throw new IllegalStateException(clazz.getName() + " Class is not a type of VertexFrame");
        else if (element instanceof Edge && !EdgeFrame.class.isAssignableFrom(clazz))
            throw new IllegalStateException(clazz.getName() + " Class is not a type of EdgeFrame");
        classBuilder = new ByteBuddy().subclass(clazz);
    }

    classBuilder = classBuilder.defineField("reflectionCache", ReflectionCache.class, Visibility.PRIVATE, FieldManifestation.PLAIN)
                .implement(CachesReflection.class).intercept(FieldAccessor.ofBeanProperty());

    /*
     * Just a hack so that our generified frame types can work.
     *
     * This information will not really be used by the generated class.
     */
    classBuilder = classBuilder.typeVariable("T");

    // try and construct any abstract methods that are left
    for (final Method method : clazz.getMethods())
        if (isAbstract(method))
            annotation_loop: for (final Annotation annotation : method.getAnnotations())
            {
                final MethodHandler handler = methodHandlers.get(annotation.annotationType());
                if (handler != null)
                {
                    classBuilder = handler.processMethod(classBuilder, method, annotation);
                    break;
                }
            }

    DynamicType.Unloaded unloadedClass = classBuilder.make();
    constructedClass = unloadedClass.load(this.classLoader, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    this.constructedClassCache.put(clazz, constructedClass);
    return constructedClass;
}