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() .load(source.getClassLoader(), loader.resolveStrategy(source, source.getClassLoader(), false))
/** * {@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(); }
classBuilder = classBuilder.defineField("reflectionCache", ReflectionCache.class, Visibility.PRIVATE, FieldManifestation.PLAIN) .implement(CachesReflection.class).intercept(FieldAccessor.ofBeanProperty());
classBuilder = classBuilder.defineField("reflectionCache", ReflectionCache.class, Visibility.PRIVATE, FieldManifestation.PLAIN) .implement(CachesReflection.class).intercept(FieldAccessor.ofBeanProperty());
classBuilder = classBuilder.defineField("reflectionCache", ReflectionCache.class, Visibility.PRIVATE, FieldManifestation.PLAIN).implement(CachesReflection.class).intercept(FieldAccessor. ofBeanProperty());
classBuilder = classBuilder.defineField("reflectionCache", ReflectionCache.class, Visibility.PRIVATE, FieldManifestation.PLAIN).implement(CachesReflection.class).intercept(FieldAccessor. ofBeanProperty());
.subclass(generatedType, ConstructorStrategy.Default.NO_CONSTRUCTORS).modifiers(Modifier.FINAL) .defineField("delegate", processFunctionType, Visibility.PRIVATE, FieldManifestation.FINAL)
private Object createInstanceWithFields(Parameter[] parameters) { DynamicType.Builder<Object> objectBuilder = new ByteBuddy().subclass(Object.class) .modifiers(PUBLIC); for (Parameter parameter : parameters) { objectBuilder = objectBuilder.defineField(parameter.getName(), parameter.getType(), PUBLIC) .annotateField(ArrayUtils.add(parameter.getAnnotations(), INJECT_ANNOTATION)); } try { Class<?> createdClass = objectBuilder.make() .load(getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION) .getLoaded(); return createdClass .getConstructor() .newInstance(); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } }
private Object createInstanceWithFields(Parameter[] parameters) { DynamicType.Builder<Object> objectBuilder = new ByteBuddy().subclass(Object.class) .modifiers(PUBLIC); for (Parameter parameter : parameters) { objectBuilder = objectBuilder.defineField(parameter.getName(), parameter.getType(), PUBLIC) .annotateField(ArrayUtils.add(parameter.getAnnotations(), INJECT_ANNOTATION)); } try { Class<?> createdClass = objectBuilder.make() .load(getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION) .getLoaded(); return createdClass .getConstructor() .newInstance(); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } }
private static DynamicType.Builder<?> addFieldWithGetterAndSetter( DynamicType.Builder<?> builder, Class<?> type, String fieldName, String getterName, String setterName) { return builder .defineField( fieldName, type, Visibility.PRIVATE, FieldPersistence.TRANSIENT ) .annotateField( AnnotationDescription.Builder.ofType( Transient.class ).build() ) .defineMethod( getterName, type, Visibility.PUBLIC ) .intercept( FieldAccessor.ofField( fieldName ) ) .defineMethod( setterName, void.class, Visibility.PUBLIC ) .withParameters( type ) .intercept( FieldAccessor.ofField( fieldName ) ); }
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 DynamicType.Builder<?> createField(DynamicType.Builder<?> builder, POJOProperty property, TypeDefinition typeDefinition) { return builder.defineField( property.getName(), typeDefinition, Visibility.PROTECTED ); }
int index = 0; for (TypeDescription capturedType : factoryMethod.getParameterTypes()) { builder = builder.defineField(FIELD_PREFIX + ++index, capturedType, Visibility.PRIVATE, FieldManifestation.FINAL);
/** * {@inheritDoc} */ public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) { for (MethodDescription.InDefinedShape methodDescription : typeDescription.getDeclaredMethods() .filter(not(isBridge()).<MethodDescription>and(isAnnotatedWith(Enhance.class)))) { if (methodDescription.isAbstract()) { throw new IllegalStateException("Cannot cache the value of an abstract method: " + methodDescription); } else if (!methodDescription.getParameters().isEmpty()) { throw new IllegalStateException("Cannot cache the value of a method with parameters: " + methodDescription); } else if (methodDescription.getReturnType().represents(void.class)) { throw new IllegalStateException("Cannot cache void result for " + methodDescription); } String name = methodDescription.getDeclaredAnnotations().ofType(Enhance.class).loadSilent().value(); if (name.length() == 0) { name = methodDescription.getName() + NAME_INFIX + randomString.nextString(); } builder = builder .defineField(name, methodDescription.getReturnType().asErasure(), methodDescription.isStatic() ? Ownership.STATIC : Ownership.MEMBER, Visibility.PRIVATE, SyntheticState.SYNTHETIC, FieldPersistence.TRANSIENT) .visit(Advice.withCustomMapping() .bind(CacheField.class, new CacheFieldOffsetMapping(name)) .to(adviceByType.get(methodDescription.getReturnType().isPrimitive() ? methodDescription.getReturnType().asErasure() : TypeDescription.OBJECT), this.classFileLocator) .on(is(methodDescription))); } return builder; }
/** * {@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(); }
private static DynamicType.Builder<?> addFieldWithGetterAndSetter( DynamicType.Builder<?> builder, Class<?> type, String fieldName, String getterName, String setterName) { return builder .defineField( fieldName, type, Visibility.PRIVATE, FieldPersistence.TRANSIENT ) .annotateField( AnnotationDescription.Builder.ofType( Transient.class ).build() ) .defineMethod( getterName, type, Visibility.PUBLIC ) .intercept( FieldAccessor.ofField( fieldName ) ) .defineMethod( setterName, void.class, Visibility.PUBLIC ) .withParameters( type ) .intercept( FieldAccessor.ofField( fieldName ) ); }
/** * {@inheritDoc} */ public FieldDefinition.Optional.Valuable<U> defineField(String name, TypeDefinition type, int modifiers) { return materialize().defineField(name, type, modifiers); }