Skip to content

Commit

Permalink
Extended API to support collections for unloaded types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed May 7, 2015
1 parent 16eda33 commit 9bff338
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 108 deletions.
65 changes: 65 additions & 0 deletions byte-buddy-dep/src/main/java/net/bytebuddy/ByteBuddy.java
Expand Up @@ -430,6 +430,28 @@ public DynamicType.Builder<?> makeInterface(Class<?>... type) {
return makeInterface(new TypeList.ForLoadedType(nonNull(type)));
}

/**
* Creates a dynamic type builder for an interface that extends a number of given interfaces.
*
* @param types The interface types to extend.
* @return A dynamic type builder for this configuration that defines an interface that extends the specified
* interfaces.
*/
public DynamicType.Builder<?> makeInterface(Iterable<? extends Class<?>> types) {
return makeInterface(new TypeList.ForLoadedType(toList(types)));
}

/**
* Creates a dynamic type builder for an interface that extends a number of given interfaces.
*
* @param typeDescription The interface types to extend.
* @return A dynamic type builder for this configuration that defines an interface that extends the specified
* interfaces.
*/
public DynamicType.Builder<?> makeInterface(TypeDescription... typeDescription) {
return makeInterface(new TypeList.Explicit(Arrays.asList(typeDescription)));
}

/**
* Creates a dynamic type builder for an interface that extends a number of given interfaces.
*
Expand Down Expand Up @@ -874,6 +896,18 @@ public ByteBuddy withTypeAnnotation(Annotation... annotation) {
return withTypeAnnotation(new AnnotationList.ForLoadedAnnotation(nonNull(annotation)));
}

/**
* Defines a new type annotation for this configuration that replaces the currently defined type
* attribute appender.
*
* @param annotations The type annotations to define for this configuration.
* @return A new configuration that represents this configuration with the given annotations as its new
* type attribute appender.
*/
public ByteBuddy withTypeAnnotation(Iterable<? extends Annotation> annotations) {
return withTypeAnnotation(new AnnotationList.ForLoadedAnnotation(toList(annotations)));
}

/**
* Defines a new type annotation for this configuration that replaces the currently defined type
* attribute appender.
Expand Down Expand Up @@ -921,6 +955,17 @@ public OptionalMethodInterception withImplementing(Class<?>... type) {
return withImplementing(new TypeList.ForLoadedType(nonNull(type)));
}

/**
* Defines all dynamic types that are created by this configuration to implement the given interfaces.
*
* @param types The interface types to implement.
* @return The same configuration where any dynamic type that is created by the resulting configuration will
* implement the given interfaces.
*/
public OptionalMethodInterception withImplementing(Iterable<? extends Class<?>> types) {
return withImplementing(new TypeList.ForLoadedType(toList(types)));
}

/**
* Defines all dynamic types that are created by this configuration to implement the given interfaces.
*
Expand Down Expand Up @@ -1946,6 +1991,11 @@ public ByteBuddy withTypeAnnotation(Annotation... annotation) {
return materialize().withTypeAnnotation(annotation);
}

@Override
public ByteBuddy withTypeAnnotation(Iterable<? extends Annotation> annotations) {
return materialize().withTypeAnnotation(annotations);
}

@Override
public ByteBuddy withTypeAnnotation(Collection<? extends AnnotationDescription> annotations) {
return materialize().withTypeAnnotation(annotations);
Expand All @@ -1956,6 +2006,11 @@ public OptionalMethodInterception withImplementing(Class<?>... type) {
return materialize().withImplementing(type);
}

@Override
public OptionalMethodInterception withImplementing(Iterable<? extends Class<?>> types) {
return materialize().withImplementing(types);
}

@Override
public OptionalMethodInterception withImplementing(TypeDescription... type) {
return materialize().withImplementing(type);
Expand Down Expand Up @@ -2011,6 +2066,16 @@ public DynamicType.Builder<?> makeInterface(Class<?>... type) {
return materialize().makeInterface(type);
}

@Override
public DynamicType.Builder<?> makeInterface(Iterable<? extends Class<?>> types) {
return materialize().makeInterface(types);
}

@Override
public DynamicType.Builder<?> makeInterface(TypeDescription... typeDescription) {
return materialize().makeInterface(typeDescription);
}

@Override
public DynamicType.Builder<?> makeInterface(Collection<? extends TypeDescription> typeDescriptions) {
return materialize().makeInterface(typeDescriptions);
Expand Down
Expand Up @@ -5,10 +5,7 @@

import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.*;

/**
* Defines a list of annotation instances.
Expand Down Expand Up @@ -57,15 +54,24 @@ class ForLoadedAnnotation extends AbstractBase<AnnotationDescription, Annotation
/**
* The represented annotations.
*/
private final Annotation[] annotation;
private final List<? extends Annotation> annotations;

/**
* Creates a new list of loaded annotations.
*
* @param annotation The represented annotations.
*/
public ForLoadedAnnotation(Annotation... annotation) {
this.annotation = annotation;
this(Arrays.asList(annotation));
}

/**
* Creates a new list of loaded annotations.
*
* @param annotations The represented annotations.
*/
public ForLoadedAnnotation(List<? extends Annotation> annotations) {
this.annotations = annotations;
}

/**
Expand All @@ -84,18 +90,18 @@ public static List<AnnotationList> asList(Annotation[][] annotations) {

@Override
public AnnotationDescription get(int index) {
return AnnotationDescription.ForLoadedAnnotation.of(annotation[index]);
return AnnotationDescription.ForLoadedAnnotation.of(annotations.get(index));
}

@Override
public int size() {
return annotation.length;
return annotations.size();
}

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
for (Annotation anAnnotation : annotation) {
if (anAnnotation.annotationType().equals(annotationType)) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().equals(annotationType)) {
return true;
}
}
Expand All @@ -104,8 +110,8 @@ public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {

@Override
public boolean isAnnotationPresent(TypeDescription annotationType) {
for (Annotation anAnnotation : annotation) {
if (annotationType.represents(anAnnotation.getClass())) {
for (Annotation annotation : annotations) {
if (annotationType.represents(annotation.getClass())) {
return true;
}
}
Expand All @@ -114,9 +120,9 @@ public boolean isAnnotationPresent(TypeDescription annotationType) {

@Override
public <T extends Annotation> AnnotationDescription.Loadable<T> ofType(Class<T> annotationType) {
for (Annotation anAnnotation : annotation) {
if (anAnnotation.annotationType().equals(annotationType)) {
return AnnotationDescription.ForLoadedAnnotation.of(annotationType.cast(anAnnotation));
for (Annotation annotation : annotations) {
if (annotation.annotationType().equals(annotationType)) {
return AnnotationDescription.ForLoadedAnnotation.of(annotationType.cast(annotation));
}
}
return null;
Expand All @@ -125,13 +131,13 @@ public <T extends Annotation> AnnotationDescription.Loadable<T> ofType(Class<T>
@Override
public AnnotationList inherited(Set<? extends TypeDescription> ignoredTypes) {
List<Annotation> inherited = new LinkedList<Annotation>();
for (Annotation annotation : this.annotation) {
for (Annotation annotation : annotations) {
if (!ignoredTypes.contains(new TypeDescription.ForLoadedType(annotation.annotationType()))
&& annotation.annotationType().isAnnotationPresent(Inherited.class)) {
inherited.add(annotation);
}
}
return new ForLoadedAnnotation(inherited.toArray(new Annotation[inherited.size()]));
return new ForLoadedAnnotation(inherited);
}

@Override
Expand Down
Expand Up @@ -3,6 +3,7 @@
import net.bytebuddy.matcher.FilterableList;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -18,25 +19,34 @@ class ForLoadedField extends AbstractBase<FieldDescription, FieldList> implement
/**
* The loaded fields this field list represents.
*/
private final Field[] field;
private final List<? extends Field> fields;

/**
* Creates a new immutable field list that represents an array of loaded field.
*
* @param field An array of fields to be represented by this field list.
*/
public ForLoadedField(Field... field) {
this.field = field;
this(Arrays.asList(field));
}

/**
* Creates a new immutable field list that represents an array of loaded field.
*
* @param fields An array of fields to be represented by this field list.
*/
public ForLoadedField(List<? extends Field> fields) {
this.fields = fields;
}

@Override
public FieldDescription get(int index) {
return new FieldDescription.ForLoadedField(field[index]);
return new FieldDescription.ForLoadedField(fields.get(index));
}

@Override
public int size() {
return field.length;
return fields.size();
}

@Override
Expand Down
Expand Up @@ -4,6 +4,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -21,21 +22,31 @@ class ForLoadedType extends AbstractBase<MethodDescription, MethodList> implemen
/**
* The loaded methods that are represented by this method list.
*/
private final Method[] methods;
private final List<? extends Method> methods;

/**
* The loaded constructors that are represented by this method list.
*/
private final Constructor<?>[] constructors;
private final List<? extends Constructor<?>> constructors;

/**
* Creates a new list for a loaded type. Method descriptions are created on demand.
*
* @param type The type to be represented by this method list.
*/
public ForLoadedType(Class<?> type) {
constructors = type.getDeclaredConstructors();
methods = type.getDeclaredMethods();
this(type.getDeclaredConstructors(), type.getDeclaredMethods());
}

/**
* Creates a method list that represents the given constructors and methods in their given order. The
* constructors are assigned the indices before the methods.
*
* @param constructor The constructors to be represented by the method list.
* @param method The methods to be represented by the method list.
*/
public ForLoadedType(Constructor<?>[] constructor, Method[] method) {
this(Arrays.asList(constructor), Arrays.asList(method));
}

/**
Expand All @@ -45,23 +56,22 @@ public ForLoadedType(Class<?> type) {
* @param constructors The constructors to be represented by the method list.
* @param methods The methods to be represented by the method list.
*/
public ForLoadedType(Constructor<?>[] constructors, Method[] methods) {
public ForLoadedType(List<? extends Constructor<?>> constructors, List<? extends Method> methods) {
this.constructors = constructors;
this.methods = methods;
}

@Override
public MethodDescription get(int index) {
if (index < constructors.length) {
return new MethodDescription.ForLoadedConstructor(constructors[index]);
} else {
return new MethodDescription.ForLoadedMethod(methods[index - constructors.length]);
}
return index < constructors.size()
? new MethodDescription.ForLoadedConstructor(constructors.get(index))
: new MethodDescription.ForLoadedMethod(methods.get(index - constructors.size()));

}

@Override
public int size() {
return constructors.length + methods.length;
return constructors.size() + methods.size();
}

@Override
Expand Down
Expand Up @@ -34,49 +34,49 @@ class ForLoadedType extends AbstractBase<TypeDescription, TypeList> implements T
/**
* The loaded types this type list represents.
*/
private final Class<?>[] type;
private final List<? extends Class<?>> types;

/**
* Creates a new type list for an array of loaded types.
*
* @param type The types to be represented by this list.
*/
public ForLoadedType(Class<?>... type) {
this.type = type;
this(Arrays.asList(type));
}

/**
* Creates a new type list for an array of loaded types.
*
* @param types The types to be represented by this list.
*/
public ForLoadedType(List<Class<?>> types) {
type = types.toArray(new Class<?>[types.size()]);
public ForLoadedType(List<? extends Class<?>> types) {
this.types = types;
}

@Override
public TypeDescription get(int index) {
return new TypeDescription.ForLoadedType(type[index]);
return new TypeDescription.ForLoadedType(types.get(index));
}

@Override
public int size() {
return type.length;
return types.size();
}

@Override
public String[] toInternalNames() {
String[] internalNames = new String[type.length];
String[] internalNames = new String[types.size()];
int i = 0;
for (Class<?> aType : type) {
internalNames[i++] = Type.getInternalName(aType);
for (Class<?> type : types) {
internalNames[i++] = Type.getInternalName(type);
}
return internalNames.length == 0 ? null : internalNames;
}

@Override
public int getStackSize() {
return StackSize.sizeOf(Arrays.asList(type));
return StackSize.sizeOf(types);
}

@Override
Expand Down

0 comments on commit 9bff338

Please sign in to comment.