return byteBuddy.subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(name) .make() .load(classLoader, loader.resolveStrategy(InjectionBase.class, classLoader, false)) .getLoaded();
.name("net.bytebuddy.mirror." + AccessibleObject.class.getSimpleName()) .visit(new MemberRemoval().stripInvokables(any())) .make() .load(AccessibleObject.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded()
.name("net.bytebuddy.mirror." + AccessibleObject.class.getSimpleName()) .visit(new MemberRemoval().stripInvokables(any())) .make() .load(AccessibleObject.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded()
failed.put(typeDescription, errored); } else if (!applied.isEmpty()) { DynamicType dynamicType = builder.make(); listener.onTransformation(typeDescription, applied); for (Map.Entry<TypeDescription, LoadedTypeInitializer> entry : dynamicType.getLoadedTypeInitializers().entrySet()) {
.intercept(new BridgeMethodImplementation(lambdaMethodName, lambdaMethod)); byte[] classFile = builder.make().getBytes(); for (ClassFileTransformer classFileTransformer : classFileTransformers) { try {
.or(hasParameters(whereAny(hasType(isPackagePrivate()))))); return builder.make() .load(classLoader, loader.resolveStrategy(features.mockedType, classLoader, localMock)) .getLoaded();
.intercept(FixedValue.argument(0)); Class<?> type = builder.make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, new ClassLoadingStrategy.ForUnsafeInjection()) .getLoaded();
private Unloaded<?> make(TypePool typePool, DynamicType.Builder<?> builder) { if ( System.getSecurityManager() != null ) { builder = builder.visit( getDeclaredMethodMemberSubstitution ); builder = builder.visit( getMethodMemberSubstitution ); } Unloaded<?> unloadedClass; if ( typePool != null ) { unloadedClass = builder.make( typePool ); } else { unloadedClass = builder.make(); } if ( DEBUG ) { try { unloadedClass.saveIn( new File( System.getProperty( "java.io.tmpdir" ) + "/bytebuddy/" ) ); } catch (IOException e) { LOG.warn( "Unable to save generated class %1$s", unloadedClass.getTypeDescription().getName(), e ); } } if ( System.getSecurityManager() != null ) { // we authorize the proxy class to access the method lookup dispatcher HibernateMethodLookupDispatcher.registerAuthorizedClass( unloadedClass.getTypeDescription().getName() ); } return unloadedClass; }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { Implementation.Composable constructor = MethodCall.invoke(DEFAULT_CONSTRUCTOR).andThen(FieldAccessor.ofField(TYPE_FIELD).setsArgumentAt(0)); int index = 1; for (String field : fields.keySet()) { constructor = constructor.andThen(FieldAccessor.ofField(field).setsArgumentAt(index++)); } DynamicType.Builder<?> builder = new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(PrivilegedExceptionAction.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .defineConstructor(Visibility.PUBLIC) .withParameters(CompoundList.of(Class.class, new ArrayList<Class<?>>(fields.values()))) .intercept(constructor) .method(named("run")) .intercept(MethodCall.invoke(methodDescription) .onField(TYPE_FIELD) .withField(fields.keySet().toArray(new String[fields.size()]))) .defineField(TYPE_FIELD, Class.class, Visibility.PRIVATE); for (Map.Entry<String, Class<?>> entry : fields.entrySet()) { builder = builder.defineField(entry.getKey(), entry.getValue(), Visibility.PRIVATE); } return builder.make(); } }
/** * {@inheritDoc} */ public byte[] apply(InitializationStrategy initializationStrategy, ClassFileLocator classFileLocator, TypeStrategy typeStrategy, ByteBuddy byteBuddy, NativeMethodStrategy methodNameTransformer, BootstrapInjectionStrategy bootstrapInjectionStrategy, AccessControlContext accessControlContext, Listener listener) { InitializationStrategy.Dispatcher dispatcher = initializationStrategy.dispatcher(); DynamicType.Unloaded<?> dynamicType = dispatcher.apply(transformer.transform(typeStrategy.builder(typeDescription, byteBuddy, classFileLocator, methodNameTransformer.resolve(), classLoader, module, protectionDomain), typeDescription, classLoader, module)).make(TypeResolutionStrategy.Disabled.INSTANCE, typePool); dispatcher.register(dynamicType, classLoader, new BootstrapClassLoaderCapableInjectorFactory(bootstrapInjectionStrategy, classLoader, protectionDomain)); listener.onTransformation(typeDescription, classLoader, module, loaded, dynamicType); return dynamicType.getBytes(); }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { MethodDescription accessorMethod = methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT); LinkedHashMap<String, TypeDescription> parameterFields = extractFields(accessorMethod); DynamicType.Builder<?> builder = new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .with(PrecomputedMethodGraph.INSTANCE) .subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(Runnable.class, Callable.class).intercept(new MethodCall(accessorMethod, assigner)) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .defineConstructor().withParameters(parameterFields.values()) .intercept(ConstructorCall.INSTANCE); for (Map.Entry<String, TypeDescription> field : parameterFields.entrySet()) { builder = builder.defineField(field.getKey(), field.getValue(), Visibility.PRIVATE); } return builder.make(); }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { LinkedHashMap<String, TypeDescription> parameterFields = extractFields(sourceMethod); DynamicType.Builder<?> builder = new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(forwardingType, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .method(ElementMatchers.<MethodDescription>isAbstract().and(isDeclaredBy(forwardingType))) .intercept(new MethodCall(sourceMethod, assigner)) .defineConstructor().withParameters(parameterFields.values()) .intercept(ConstructorCall.INSTANCE); for (Map.Entry<String, TypeDescription> field : parameterFields.entrySet()) { builder = builder.defineField(field.getKey(), field.getValue(), Visibility.PRIVATE); } return builder.make(); }
@Override protected void apply(ByteBuddy byteBuddy, Instrumentation instrumentation, ClassFileTransformer classFileTransformer) { if (LambdaFactory.register(classFileTransformer, new LambdaInstanceFactory(byteBuddy))) { Class<?> lambdaMetaFactory; try { lambdaMetaFactory = Class.forName("java.lang.invoke.LambdaMetafactory"); } catch (ClassNotFoundException ignored) { return; } byteBuddy.with(Implementation.Context.Disabled.Factory.INSTANCE) .redefine(lambdaMetaFactory) .visit(new AsmVisitorWrapper.ForDeclaredMethods() .method(named("metafactory"), MetaFactoryRedirection.INSTANCE) .method(named("altMetafactory"), AlternativeMetaFactoryRedirection.INSTANCE)) .make() .load(lambdaMetaFactory.getClassLoader(), ClassReloadingStrategy.of(instrumentation)); } }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { return new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .with(MethodGraph.Empty.INSTANCE) // avoid parsing the graph .subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .annotateType(eager ? Collections.singletonList(AnnotationDescription.Builder.ofType(SignatureRelevant.class).build()) : Collections.<AnnotationDescription>emptyList()) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .make(); } }
@Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { if (classBeingRedefined == null || !mocked.contains(classBeingRedefined) || EXCLUDES.contains(classBeingRedefined)) { return null; } else { try { return byteBuddy.redefine(classBeingRedefined, ClassFileLocator.Simple.of(classBeingRedefined.getName(), classfileBuffer)) // Note: The VM erases parameter meta data from the provided class file (bug). We just add this information manually. .visit(new ParameterWritingVisitorWrapper(classBeingRedefined)) .visit(mockTransformer) .make() .getBytes(); } catch (Throwable throwable) { lastException = throwable; return null; } } }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { return fieldResolver.apply(new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(fieldResolver.getProxyType(), ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .defineConstructor().withParameters(fieldDescription.isStatic() ? Collections.<TypeDescription>emptyList() : Collections.singletonList(instrumentedType)) .intercept(fieldDescription.isStatic() ? StaticFieldConstructor.INSTANCE : new InstanceFieldConstructor(instrumentedType)), fieldDescription, assigner, methodAccessorFactory).make(); }
/** * {@inheritDoc} */ public Unloaded<U> make(TypeResolutionStrategy typeResolutionStrategy, TypePool typePool) { return materialize().make(typeResolutionStrategy, typePool); }
/** * {@inheritDoc} */ public DynamicType.Unloaded<U> make() { return materialize().make(); }
/** * {@inheritDoc} */ public Unloaded<U> make(TypeResolutionStrategy typeResolutionStrategy) { return materialize().make(typeResolutionStrategy); }
/** * {@inheritDoc} */ public Unloaded<U> make(TypePool typePool) { return materialize().make(typePool); }