net.bytebuddy.dynamic.DynamicType.Builder Java Examples
The following examples show how to use
net.bytebuddy.dynamic.DynamicType.Builder.
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 | 6 votes |
private static <InT, OutT> DynamicType.Unloaded<?> createDynamicTypeFromSpec(StreamingLedgerSpec<InT, OutT> spec) throws NoSuchMethodException { PackageLocalNamingStrategy generatedTypeName = new PackageLocalNamingStrategy(spec.processFunction.getClass()); TypeDefinition generatedType = Generic.Builder.parameterizedType( ProcessFunctionInvoker.class, spec.inputType.getTypeClass(), spec.resultType.getTypeClass() ).build(); TypeDefinition processFunctionType = new TypeDescription.ForLoadedType(spec.processFunction.getClass()); ForLoadedConstructor superTypeConstructor = new ForLoadedConstructor(ProcessFunctionInvoker.class.getDeclaredConstructor()); MethodDescription processMethodType = processMethodTypeFromSpec(spec); Builder<?> builder = configureByteBuddyBuilder( generatedTypeName, generatedType, processFunctionType, superTypeConstructor, processMethodType, spec.stateBindings.size()); return builder.make(); }
Example #2
Source File: ModelClassEnhancer.java From activejpa with Apache License 2.0 | 6 votes |
@Override public Builder<?> transform(Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) { logger.info("Transforming the model class - {}", typeDescription.getActualName()); if (transformedClasses.contains(typeDescription.getActualName())) { logger.info("Model class - {} is already transformed. Skipping", typeDescription.getActualName()); return builder; } builder = defineMethod(builder, typeDescription, classLoader, "all", TargetType.resolve(new ForLoadedType(List.class), typeDescription)); builder = defineMethod(builder, typeDescription, classLoader, "count", new ForLoadedType(long.class)); builder = defineMethod(builder, typeDescription, classLoader, "count", new ForLoadedType(long.class), new TypeDescription.ForLoadedType(Filter.class)); builder = defineMethod(builder, typeDescription, classLoader, "findById", ForGenerifiedErasure.of(new ForLoadedType(Model.class)), new ForLoadedType(Serializable.class)); builder = defineMethod(builder, typeDescription, classLoader, "deleteAll", TypeDescription.VOID, new TypeDescription.ForLoadedType(Filter.class)); builder = defineMethod(builder, typeDescription, classLoader, "deleteAll", TypeDescription.VOID); builder = defineMethod(builder, typeDescription, classLoader, "exists", new ForLoadedType(boolean.class), new ForLoadedType(Serializable.class)); builder = defineMethod(builder, typeDescription, classLoader, "where", TargetType.resolve(new ForLoadedType(List.class), typeDescription), new ForLoadedType(Object[].class)); builder = defineMethod(builder, typeDescription, classLoader, "where", TargetType.resolve(new ForLoadedType(List.class), typeDescription), new ForLoadedType(Filter.class)); builder = defineMethod(builder, typeDescription, classLoader, "one", ForGenerifiedErasure.of(new ForLoadedType(Model.class)), new ForLoadedType(Object[].class)); builder = defineMethod(builder, typeDescription, classLoader, "first", ForGenerifiedErasure.of(new ForLoadedType(Model.class)), new ForLoadedType(Object[].class)); builder = defineMethod(builder, typeDescription, classLoader, "filter", new ForLoadedType(Filter.class)); transformedClasses.add(typeDescription.getActualName()); return builder; }
Example #3
Source File: ByteBuddyProcessFunctionInvoker.java From da-streamingledger with Apache License 2.0 | 5 votes |
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 #4
Source File: PageImplementation.java From webtester2-core with Apache License 2.0 | 5 votes |
private static Class<? extends Page> create(Class<? extends Page> pageType) { String className = pageType.getCanonicalName() + "$$Impl"; ClassLoader classLoader = pageType.getClassLoader(); InvocationHandler identifyUsingHandler = new IdentifyUsingInvocationHandler(); Builder<BasePage> pageTypeBuilder = new ByteBuddy()// .with(ClassFileVersion.JAVA_V8) .subclass(BasePage.class, ConstructorStrategy.Default.IMITATE_SUPER_CLASS) .implement(pageType) .name(className); if (ClasspathUtils.KOTLIN_MODULE_LOADED) { pageTypeBuilder = addKotlinImplementations(pageTypeBuilder, pageType); } pageTypeBuilder = pageTypeBuilder// .method(isDefaultMethod())// .intercept(Advice.to(ActionAdvice.class)// .wrap(DefaultMethodCall.prioritize(pageType))); pageTypeBuilder = pageTypeBuilder// .method(isAbstract().and(isAnnotatedWith(IdentifyUsing.class)).and(takesArguments(0))) .intercept(InvocationHandlerAdapter.of(identifyUsingHandler)); return pageTypeBuilder.make().load(classLoader).getLoaded(); }
Example #5
Source File: PageImplementation.java From webtester2-core with Apache License 2.0 | 5 votes |
private static Builder<BasePage> addKotlinImplementations(Builder<BasePage> pageTypeBuilder, Class<? extends Page> pageType) { try { Class<?> kotlinPage = Class.forName(ClasspathUtils.KOTLIN_PAGE_CLASS); if (kotlinPage.isAssignableFrom(pageType)) { InvocationHandler kotlinDefaultMethodHandler = new KotlinDefaultMethodHandler(); return pageTypeBuilder// .method(isAbstract())// .intercept(InvocationHandlerAdapter.of(kotlinDefaultMethodHandler)); } return pageTypeBuilder; } catch (ClassNotFoundException e) { throw new IllegalStateException("could not load Kotlin Page implementation", e); } }
Example #6
Source File: PageFragmentImplementation.java From webtester2-core with Apache License 2.0 | 5 votes |
private static Class<? extends PageFragment> create(Class<? extends PageFragment> pageFragmentType) { String className = pageFragmentType.getCanonicalName() + "$$Impl"; ClassLoader classLoader = pageFragmentType.getClassLoader(); InvocationHandler identifyUsingHandler = new IdentifyUsingInvocationHandler(); InvocationHandler attributeHandler = new AttributeInvocationHandler(); Builder<BasePageFragment> pageFragmentTypeBuilder = new ByteBuddy()// .with(ClassFileVersion.JAVA_V8) .subclass(BasePageFragment.class, ConstructorStrategy.Default.IMITATE_SUPER_CLASS) .implement(pageFragmentType) .name(className); if (ClasspathUtils.KOTLIN_MODULE_LOADED) { pageFragmentTypeBuilder = addKotlinImplementations(pageFragmentTypeBuilder, pageFragmentType); } pageFragmentTypeBuilder = pageFragmentTypeBuilder// .method(isDefaultMethod())// .intercept(Advice.to(ActionAdvice.class)// .wrap(Advice.to(MarkingAdvice.class)// .wrap(Advice.to(EventProducerAdvice.class)// .wrap(DefaultMethodCall.prioritize(pageFragmentType))))); pageFragmentTypeBuilder = pageFragmentTypeBuilder// .method(isAbstract().and(isAnnotatedWith(IdentifyUsing.class)).and(takesArguments(0))) .intercept(InvocationHandlerAdapter.of(identifyUsingHandler)); pageFragmentTypeBuilder = pageFragmentTypeBuilder// .method(isAbstract().and(isAnnotatedWith(Attribute.class)).and(takesArguments(0))) .intercept(Advice.to(MarkingAdvice.class)// .wrap(InvocationHandlerAdapter.of(attributeHandler))); return pageFragmentTypeBuilder.make().load(classLoader).getLoaded(); }
Example #7
Source File: PageFragmentImplementation.java From webtester2-core with Apache License 2.0 | 5 votes |
private static Builder<BasePageFragment> addKotlinImplementations(Builder<BasePageFragment> pageFragmentTypeBuilder, Class<? extends PageFragment> pageFragmentType) { try { Class<?> kotlinPageFragment = Class.forName(ClasspathUtils.KOTLIN_PAGE_FRAGMENT_CLASS); if (kotlinPageFragment.isAssignableFrom(pageFragmentType)) { InvocationHandler kotlinDefaultMethodHandler = new KotlinDefaultMethodHandler(); return pageFragmentTypeBuilder// .method(isAbstract())// .intercept(InvocationHandlerAdapter.of(kotlinDefaultMethodHandler)); } return pageFragmentTypeBuilder; } catch (ClassNotFoundException e) { throw new IllegalStateException("could not load Kotlin PageFragment implementation", e); } }
Example #8
Source File: TemplateModelProxyHandler.java From flow with Apache License 2.0 | 5 votes |
private static BiFunction<StateNode, BeanModelType<?>, Object> createInterfaceConstructor( Class<?> modelType) { Builder<InterfaceProxy> builder = new ByteBuddy() .subclass(InterfaceProxy.class).implement(modelType); return createProxyConstructor(modelType.getClassLoader(), builder, modelType.getCanonicalName()); }
Example #9
Source File: TemplateModelProxyHandler.java From flow with Apache License 2.0 | 5 votes |
private static BiFunction<StateNode, BeanModelType<?>, Object> createClassConstructor( Class<?> modelType) { Builder<?> builder = new ByteBuddy().subclass(modelType) .implement(ModelProxy.class); return createProxyConstructor(modelType.getClassLoader(), builder, modelType.getCanonicalName()); }
Example #10
Source File: TemplateModelProxyHandler.java From flow with Apache License 2.0 | 5 votes |
private static BiFunction<StateNode, BeanModelType<?>, Object> createProxyConstructor( ClassLoader classLoader, Builder<?> proxyBuilder, String classFqn) { String proxyClassName = generateProxyClassName(classFqn, classLoader); Class<?> proxyType = proxyBuilder // Handle bean methods (and abstract methods for error handling) .method(method -> isAccessor(method) || method.isAbstract()) .intercept(MethodDelegation.to(proxyHandler)) // Handle internal $stateNode methods .defineField("$stateNode", StateNode.class) .method(method -> "$stateNode".equals(method.getName())) .intercept(FieldAccessor.ofField("$stateNode")) // Handle internal $modelType methods .defineField("$modelType", BeanModelType.class) .method(method -> "$modelType".equals(method.getName())) .intercept(FieldAccessor.ofField("$modelType")) // Create the class .name(proxyClassName).make() .load(classLoader, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); return (node, modelType) -> { Object instance = ReflectTools.createProxyInstance(proxyType, modelType.getProxyType()); ModelProxy modelProxy = (ModelProxy) instance; modelProxy.$stateNode(node); modelProxy.$modelType(modelType); modelType.createInitialValues(node); return instance; }; }
Example #11
Source File: ModelClassEnhancer.java From activejpa with Apache License 2.0 | 5 votes |
/** * @param builder * @param typeDescription * @param classLoader * @param name * @param targetType * @param parameters * @return */ private Builder<?> defineMethod(Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, String name, TypeDefinition targetType, TypeDefinition... parameters) { logger.info("Defining the method - {}.{} with return type - {} and parameters - {}", typeDescription.getActualName(), name, targetType.getActualName(), parameters); try { builder = builder.defineMethod(name, targetType, Ownership.STATIC, Visibility.PUBLIC).withParameters(parameters).intercept(MethodDelegation.to(ModelInterceptor.class)); builder.make(); } catch (Exception exception) { if (! (exception.getCause() instanceof NoSuchMethodException)) { logger.error("Failed while defining the method - {}.{}", typeDescription.getActualName(), name, exception); throw new AssertionError(exception); } } return builder; }
Example #12
Source File: ScheduledRunnableAgentRule.java From java-specialagent with Apache License 2.0 | 4 votes |
@Override public Builder<?> transform(final Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) { return builder.visit(advice(typeDescription).to(ScheduledRunnableAgentRule.class).on(named("schedule").and(takesArguments(Runnable.class, long.class, TimeUnit.class)))); }
Example #13
Source File: FixedDelayAgentRule.java From java-specialagent with Apache License 2.0 | 4 votes |
@Override public Builder<?> transform(final Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) { return builder.visit(advice(typeDescription).to(FixedDelayAgentRule.class).on(named("scheduleWithFixedDelay").and(takesArguments(Runnable.class, long.class, long.class, TimeUnit.class)))); }
Example #14
Source File: ScheduledCallableAgentRule.java From java-specialagent with Apache License 2.0 | 4 votes |
@Override public Builder<?> transform(final Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) { return builder.visit(advice(typeDescription).to(ScheduledCallableAgentRule.class).on(named("schedule").and(takesArguments(Callable.class, long.class, TimeUnit.class)))); }
Example #15
Source File: FixedRateAgentRule.java From java-specialagent with Apache License 2.0 | 4 votes |
@Override public Builder<?> transform(final Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) { return builder.visit(advice(typeDescription).to(FixedRateAgentRule.class).on(named("scheduleAtFixedRate").and(takesArguments(Runnable.class, long.class, long.class, TimeUnit.class)))); }
Example #16
Source File: ExecutorAgentRule.java From java-specialagent with Apache License 2.0 | 4 votes |
@Override public Builder<?> transform(final Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) { return builder.visit(advice(typeDescription).to(ExecutorAgentRule.class).on(named("execute").and(takesArguments(Runnable.class)))); }