.name(name) .make() .load(classLoader, loader.resolveStrategy(InjectionBase.class, classLoader, false))
.name("net.bytebuddy.mirror." + AccessibleObject.class.getSimpleName()) .visit(new MemberRemoval().stripInvokables(any())) .make()
.name("net.bytebuddy.mirror." + AccessibleObject.class.getSimpleName()) .visit(new MemberRemoval().stripInvokables(any())) .make()
public ConventionBasedTest generateProxy(ConventionBasedTest conventionBasedTest) { try { String className = generatedClassName(conventionBasedTest); String methodName = generatedMethodName(conventionBasedTest); TestAnnotationImpl testAnnotationImpl = new TestAnnotationImpl(conventionBasedTest); DynamicType.Unloaded<ConventionBasedTestProxy> dynamicType = new ByteBuddy() .subclass(ConventionBasedTestProxy.class) .name(className) .defineMethod(methodName, void.class, ImmutableList.of(), Visibility.PUBLIC) .intercept(MethodCall.invoke(ConventionBasedTestProxy.class.getMethod("test"))) .annotateMethod(testAnnotationImpl) .make(); LOGGER.debug("Generating proxy class: {}.{}, annotation: {}", className, methodName, testAnnotationImpl); return dynamicType .load(getSystemClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded() .getConstructor(ConventionBasedTest.class) .newInstance(conventionBasedTest); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new IllegalStateException("Could not create proxy for convention test: " + conventionBasedTest, e); } }
@SuppressWarnings("unchecked") private static synchronized <S> Class<S> giveDynamicSubclass(Class<S> superclass) { boolean isSystemClass = isSystemClass(superclass.getName()); String namePrefix = isSystemClass ? FALLBACK_PACKAGE_NAME : getPackageName(superclass); String name = namePrefix + "." + superclass.getSimpleName() + "$$DynamicSubclass$" + superclass.hashCode(); Class<S> existsAlready = (Class<S>)classForName(name); if (existsAlready != null) { return existsAlready; } Class<?> context = isSystemClass ? Instantiator.class : superclass; ClassLoadingStrategy<? super ClassLoader> cs = getClassLoadingStrategy(context); return (Class<S>)new ByteBuddy() .with(TypeValidation.DISABLED) .subclass(superclass) .name(name) .make() .load(context.getClassLoader(), cs) .getLoaded(); }
public static ByteArrayClassLoader createClass(String classname, String...fields) { DynamicType.Builder<Object> byteBuddy = new ByteBuddy(ClassFileVersion.JAVA_V6) .subclass(Object.class) .name(classname) .modifiers(Visibility.PUBLIC); for (String field : fields) { byteBuddy = byteBuddy.defineField(field, String.class, Visibility.PUBLIC); } byte[] bytes = byteBuddy.make().getBytes(); Map<String, byte[]> typeDefinitions = new HashMap<String, byte[]>(); typeDefinitions.put(classname, bytes); return new ByteArrayClassLoader(SerializerTest.class.getClassLoader(), typeDefinitions); } }
private byte[] buildAbstractBase(JavaType javaType, String className) { DynamicType.Builder<?> builder = new ByteBuddy() //needed because className will contain the 'abstract' Java keyword .with(TypeValidation.DISABLED) .subclass(createTypeDefinitionFromJavaType(javaType)) .name(className) .modifiers(Visibility.PUBLIC, TypeManifestation.ABSTRACT); if (javaType.isInterface()) { builder = ByteBuddyBuilderUtil.createEqualsAndHashCode(builder); } return builder.make().getBytes(); }
.with(TypeValidation.DISABLED) .subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(ClassLoader.class.getName() + "$ByteBuddyAccessor$" + RandomString.make()) .defineMethod("findLoadedClass", Class.class, Visibility.PUBLIC) .withParameters(ClassLoader.class, String.class)
.name(name) .implement(additionalInterfaces) .implement(ISpockMockObject.class)
.name(name) .ignoreAlso(isGroovyMethod()) .annotateType(features.stripAnnotations
try { intermediate = byteBuddy.subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(String.format("%s$%d", "org.mockito.codegen.MockitoTypeCarrier", Math.abs(random.nextInt()))) .defineField("mockitoType", Class.class, Visibility.PUBLIC, Ownership.STATIC) .make() .name(String.format("%s$%s$%d", source.getName(), "MockitoModuleProbe", Math.abs(random.nextInt()))) .invokable(isTypeInitializer()).intercept(implementation) .make()
.with(TypeValidation.DISABLED) .subclass(ExecutingTransformer.class) .name(ExecutingTransformer.class.getName() + "$ByteBuddy$ModuleSupport") .method(named("transform").and(takesArgument(0, JavaType.MODULE.load()))) .intercept(MethodCall.invoke(ExecutingTransformer.class.getDeclaredMethod("transform",
/** * {@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 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) { return new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(morphingType, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .defineConstructor().withParameters(specialMethodInvocation.getMethodDescription().isStatic() ? Collections.<TypeDescription>emptyList() : Collections.singletonList(instrumentedType)) .intercept(specialMethodInvocation.getMethodDescription().isStatic() ? StaticFieldConstructor.INSTANCE : new InstanceFieldConstructor(instrumentedType)) .method(ElementMatchers.<MethodDescription>isAbstract().and(isDeclaredBy(morphingType))) .intercept(new MethodCall(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT), assigner)) .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(); }
/** * {@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(); } }
/** * {@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 DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { return new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .ignore(ignoreFinalizer ? isFinalizer() : ElementMatchers.<MethodDescription>none()) .subclass(proxiedType) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .method(any()).intercept(new MethodCall(methodAccessorFactory)) .defineMethod(REFLECTION_METHOD, TargetType.class, Ownership.STATIC).intercept(SilentConstruction.INSTANCE) .make(); }
/** * {@inheritDoc} */ public Builder<U> name(String name) { return materialize().name(name); }