Skip to content

Commit

Permalink
Removed Byte Buddy commons in favor of a better object-oriented desig…
Browse files Browse the repository at this point in the history
…n as a more complex validation system is required for generic types.
  • Loading branch information
Rafael Winterhalter committed Dec 30, 2015
1 parent 0b6f082 commit 2fd9ba6
Show file tree
Hide file tree
Showing 38 changed files with 497 additions and 1,606 deletions.
132 changes: 71 additions & 61 deletions byte-buddy-dep/src/main/java/net/bytebuddy/ByteBuddy.java

Large diffs are not rendered by default.

Expand Up @@ -2,7 +2,6 @@


import net.bytebuddy.description.modifier.*; import net.bytebuddy.description.modifier.*;
import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.utility.ByteBuddyCommons;
import net.bytebuddy.utility.RandomString; import net.bytebuddy.utility.RandomString;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;


Expand Down Expand Up @@ -139,7 +138,7 @@ public List<TypeDescription.Generic> getDeclaredInterfaces() {


@Override @Override
public Visibility getVisibility() { public Visibility getVisibility() {
switch (modifiers & ByteBuddyCommons.VISIBILITY_MODIFIER_MASK) { switch (modifiers & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE)) {
case Opcodes.ACC_PUBLIC: case Opcodes.ACC_PUBLIC:
return Visibility.PUBLIC; return Visibility.PUBLIC;
case Opcodes.ACC_PROTECTED: case Opcodes.ACC_PROTECTED:
Expand Down
Expand Up @@ -37,7 +37,6 @@
import java.util.*; import java.util.*;


import static net.bytebuddy.matcher.ElementMatchers.*; import static net.bytebuddy.matcher.ElementMatchers.*;
import static net.bytebuddy.utility.ByteBuddyCommons.nonNull;


/** /**
* <p> * <p>
Expand Down Expand Up @@ -1969,7 +1968,7 @@ public Default() {
* @param byteBuddy The Byte Buddy instance to be used. * @param byteBuddy The Byte Buddy instance to be used.
*/ */
public Default(ByteBuddy byteBuddy) { public Default(ByteBuddy byteBuddy) {
this(nonNull(byteBuddy), this(byteBuddy,
BinaryLocator.Default.FAST, BinaryLocator.Default.FAST,
TypeStrategy.Default.REBASE, TypeStrategy.Default.REBASE,
Listener.NoOp.INSTANCE, Listener.NoOp.INSTANCE,
Expand Down Expand Up @@ -2019,7 +2018,7 @@ protected Default(ByteBuddy byteBuddy,


@Override @Override
public Identified type(RawMatcher matcher) { public Identified type(RawMatcher matcher) {
return new Matched(nonNull(matcher), Transformer.NoOp.INSTANCE); return new Matched(matcher, Transformer.NoOp.INSTANCE);
} }


@Override @Override
Expand All @@ -2029,12 +2028,12 @@ public Identified type(ElementMatcher<? super TypeDescription> typeMatcher) {


@Override @Override
public Identified type(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super ClassLoader> classLoaderMatcher) { public Identified type(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super ClassLoader> classLoaderMatcher) {
return type(new RawMatcher.ForElementMatcherPair(nonNull(typeMatcher), nonNull(classLoaderMatcher))); return type(new RawMatcher.ForElementMatcherPair(typeMatcher, classLoaderMatcher));
} }


@Override @Override
public AgentBuilder withByteBuddy(ByteBuddy byteBuddy) { public AgentBuilder withByteBuddy(ByteBuddy byteBuddy) {
return new Default(nonNull(byteBuddy), return new Default(byteBuddy,
binaryLocator, binaryLocator,
typeStrategy, typeStrategy,
listener, listener,
Expand All @@ -2051,7 +2050,7 @@ public AgentBuilder withListener(Listener listener) {
return new Default(byteBuddy, return new Default(byteBuddy,
binaryLocator, binaryLocator,
typeStrategy, typeStrategy,
new Listener.Compound(this.listener, nonNull(listener)), new Listener.Compound(this.listener, listener),
nativeMethodStrategy, nativeMethodStrategy,
accessControlContext, accessControlContext,
initializationStrategy, initializationStrategy,
Expand All @@ -2064,7 +2063,7 @@ public AgentBuilder withListener(Listener listener) {
public AgentBuilder withTypeStrategy(TypeStrategy typeStrategy) { public AgentBuilder withTypeStrategy(TypeStrategy typeStrategy) {
return new Default(byteBuddy, return new Default(byteBuddy,
binaryLocator, binaryLocator,
nonNull(typeStrategy), typeStrategy,
listener, listener,
nativeMethodStrategy, nativeMethodStrategy,
accessControlContext, accessControlContext,
Expand All @@ -2077,7 +2076,7 @@ public AgentBuilder withTypeStrategy(TypeStrategy typeStrategy) {
@Override @Override
public AgentBuilder withBinaryLocator(BinaryLocator binaryLocator) { public AgentBuilder withBinaryLocator(BinaryLocator binaryLocator) {
return new Default(byteBuddy, return new Default(byteBuddy,
nonNull(binaryLocator), binaryLocator,
typeStrategy, typeStrategy,
listener, listener,
nativeMethodStrategy, nativeMethodStrategy,
Expand Down Expand Up @@ -2139,7 +2138,7 @@ public AgentBuilder withRedefinitionStrategy(RedefinitionStrategy redefinitionSt
nativeMethodStrategy, nativeMethodStrategy,
accessControlContext, accessControlContext,
initializationStrategy, initializationStrategy,
nonNull(redefinitionStrategy), redefinitionStrategy,
bootstrapInjectionStrategy, bootstrapInjectionStrategy,
transformation); transformation);
} }
Expand All @@ -2152,7 +2151,7 @@ public AgentBuilder withInitializationStrategy(InitializationStrategy initializa
listener, listener,
nativeMethodStrategy, nativeMethodStrategy,
accessControlContext, accessControlContext,
nonNull(initializationStrategy), initializationStrategy,
redefinitionStrategy, redefinitionStrategy,
bootstrapInjectionStrategy, bootstrapInjectionStrategy,
transformation); transformation);
Expand All @@ -2168,7 +2167,7 @@ public AgentBuilder enableBootstrapInjection(File folder, Instrumentation instru
accessControlContext, accessControlContext,
initializationStrategy, initializationStrategy,
redefinitionStrategy, redefinitionStrategy,
new BootstrapInjectionStrategy.Enabled(nonNull(folder), nonNull(instrumentation)), new BootstrapInjectionStrategy.Enabled(folder, instrumentation),
transformation); transformation);
} }


Expand Down Expand Up @@ -3170,7 +3169,7 @@ protected Matched(RawMatcher rawMatcher, Transformer transformer) {


@Override @Override
public Identified.Extendable transform(Transformer transformer) { public Identified.Extendable transform(Transformer transformer) {
return new Matched(rawMatcher, new Transformer.Compound(this.transformer, nonNull(transformer))); return new Matched(rawMatcher, new Transformer.Compound(this.transformer, transformer));
} }


@Override @Override
Expand Down
Expand Up @@ -14,7 +14,6 @@
import java.util.*; import java.util.*;


import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.utility.ByteBuddyCommons.nonNull;


/** /**
* An annotation description describes {@link java.lang.annotation.Annotation} meta data of a class without this class * An annotation description describes {@link java.lang.annotation.Annotation} meta data of a class without this class
Expand Down Expand Up @@ -1789,7 +1788,7 @@ protected Builder(TypeDescription annotationType, Map<String, AnnotationValue<?,
* @return A builder for creating an annotation of the given type. * @return A builder for creating an annotation of the given type.
*/ */
public static Builder forType(Class<? extends Annotation> annotationType) { public static Builder forType(Class<? extends Annotation> annotationType) {
return forType(new TypeDescription.ForLoadedType(nonNull(annotationType))); return forType(new TypeDescription.ForLoadedType(annotationType));
} }


/** /**
Expand All @@ -1813,15 +1812,15 @@ public static Builder forType(TypeDescription annotationType) {
* @return A builder with the additional, given property. * @return A builder with the additional, given property.
*/ */
public Builder define(String property, AnnotationValue<?, ?> value) { public Builder define(String property, AnnotationValue<?, ?> value) {
MethodList<?> methodDescriptions = annotationType.getDeclaredMethods().filter(named(nonNull(property))); MethodList<?> methodDescriptions = annotationType.getDeclaredMethods().filter(named(property));
if (methodDescriptions.isEmpty()) { if (methodDescriptions.isEmpty()) {
throw new IllegalArgumentException(annotationType + " does not define a property named " + property); throw new IllegalArgumentException(annotationType + " does not define a property named " + property);
} else if (!methodDescriptions.getOnly().getReturnType().asErasure().isAnnotationValue(value.resolve())) { } else if (!methodDescriptions.getOnly().getReturnType().asErasure().isAnnotationValue(value.resolve())) {
throw new IllegalArgumentException(value + " cannot be assigned to " + property); throw new IllegalArgumentException(value + " cannot be assigned to " + property);
} }
Map<String, AnnotationValue<?, ?>> annotationValues = new HashMap<String, AnnotationValue<?, ?>>(); Map<String, AnnotationValue<?, ?>> annotationValues = new HashMap<String, AnnotationValue<?, ?>>();
annotationValues.putAll(this.annotationValues); annotationValues.putAll(this.annotationValues);
if (annotationValues.put(methodDescriptions.getOnly().getName(), nonNull(value)) != null) { if (annotationValues.put(methodDescriptions.getOnly().getName(), value) != null) {
throw new IllegalArgumentException("Property already defined: " + property); throw new IllegalArgumentException("Property already defined: " + property);
} }
return new Builder(annotationType, annotationValues); return new Builder(annotationType, annotationValues);
Expand All @@ -1835,7 +1834,7 @@ public Builder define(String property, AnnotationValue<?, ?> value) {
* @return A builder with the additional enumeration property. * @return A builder with the additional enumeration property.
*/ */
public Builder define(String property, Enum<?> value) { public Builder define(String property, Enum<?> value) {
return define(property, new EnumerationDescription.ForLoadedEnumeration(nonNull(value))); return define(property, new EnumerationDescription.ForLoadedEnumeration(value));
} }


/** /**
Expand All @@ -1847,7 +1846,7 @@ public Builder define(String property, Enum<?> value) {
* @return A builder with the additional enumeration property. * @return A builder with the additional enumeration property.
*/ */
public Builder define(String property, TypeDescription enumerationType, String value) { public Builder define(String property, TypeDescription enumerationType, String value) {
return define(property, new EnumerationDescription.Latent(nonNull(enumerationType), nonNull(value))); return define(property, new EnumerationDescription.Latent(enumerationType, value));
} }


/** /**
Expand All @@ -1859,7 +1858,7 @@ public Builder define(String property, TypeDescription enumerationType, String v
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Builder define(String property, EnumerationDescription value) { public Builder define(String property, EnumerationDescription value) {
return define(property, AnnotationValue.ForEnumeration.<Enum>of(nonNull(value))); return define(property, AnnotationValue.ForEnumeration.<Enum>of(value));
} }


/** /**
Expand All @@ -1870,7 +1869,7 @@ public Builder define(String property, EnumerationDescription value) {
* @return A builder with the additional annotation property. * @return A builder with the additional annotation property.
*/ */
public Builder define(String property, Annotation annotation) { public Builder define(String property, Annotation annotation) {
return define(property, new ForLoadedAnnotation<Annotation>(nonNull(annotation))); return define(property, new ForLoadedAnnotation<Annotation>(annotation));
} }


/** /**
Expand All @@ -1881,7 +1880,7 @@ public Builder define(String property, Annotation annotation) {
* @return A builder with the additional annotation property. * @return A builder with the additional annotation property.
*/ */
public Builder define(String property, AnnotationDescription annotationDescription) { public Builder define(String property, AnnotationDescription annotationDescription) {
return define(property, new AnnotationValue.ForAnnotation<Annotation>(nonNull(annotationDescription))); return define(property, new AnnotationValue.ForAnnotation<Annotation>(annotationDescription));
} }


/** /**
Expand All @@ -1892,7 +1891,7 @@ public Builder define(String property, AnnotationDescription annotationDescripti
* @return A builder with the additional class property. * @return A builder with the additional class property.
*/ */
public Builder define(String property, Class<?> type) { public Builder define(String property, Class<?> type) {
return define(property, new TypeDescription.ForLoadedType(nonNull(type))); return define(property, new TypeDescription.ForLoadedType(type));
} }


/** /**
Expand Down Expand Up @@ -1920,9 +1919,9 @@ public <T extends Enum<?>> Builder defineEnumerationArray(String property, Class
EnumerationDescription[] enumerationDescription = new EnumerationDescription[value.length]; EnumerationDescription[] enumerationDescription = new EnumerationDescription[value.length];
int index = 0; int index = 0;
for (T aValue : value) { for (T aValue : value) {
enumerationDescription[index++] = new EnumerationDescription.ForLoadedEnumeration(nonNull(aValue)); enumerationDescription[index++] = new EnumerationDescription.ForLoadedEnumeration(aValue);
} }
return defineEnumerationArray(property, new TypeDescription.ForLoadedType(nonNull(enumerationType)), enumerationDescription); return defineEnumerationArray(property, new TypeDescription.ForLoadedType(enumerationType), enumerationDescription);
} }


/** /**
Expand All @@ -1939,7 +1938,7 @@ public Builder defineEnumerationArray(String property, TypeDescription enumerati
} }
EnumerationDescription[] enumerationDescription = new EnumerationDescription[value.length]; EnumerationDescription[] enumerationDescription = new EnumerationDescription[value.length];
for (int i = 0; i < value.length; i++) { for (int i = 0; i < value.length; i++) {
enumerationDescription[i] = new EnumerationDescription.Latent(nonNull(enumerationType), nonNull(value[i])); enumerationDescription[i] = new EnumerationDescription.Latent(enumerationType, value[i]);
} }
return defineEnumerationArray(property, enumerationType, enumerationDescription); return defineEnumerationArray(property, enumerationType, enumerationDescription);
} }
Expand All @@ -1954,7 +1953,7 @@ public Builder defineEnumerationArray(String property, TypeDescription enumerati
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Builder defineEnumerationArray(String property, TypeDescription enumerationType, EnumerationDescription... value) { public Builder defineEnumerationArray(String property, TypeDescription enumerationType, EnumerationDescription... value) {
return define(property, AnnotationValue.ForComplexArray.<Enum>of(enumerationType, nonNull(value))); return define(property, AnnotationValue.ForComplexArray.<Enum>of(enumerationType, value));
} }


/** /**
Expand All @@ -1968,8 +1967,8 @@ public Builder defineEnumerationArray(String property, TypeDescription enumerati
*/ */
public <T extends Annotation> Builder defineAnnotationArray(String property, Class<T> annotationType, T... annotation) { public <T extends Annotation> Builder defineAnnotationArray(String property, Class<T> annotationType, T... annotation) {
return defineAnnotationArray(property, return defineAnnotationArray(property,
new TypeDescription.ForLoadedType(nonNull(annotationType)), new TypeDescription.ForLoadedType(annotationType),
new AnnotationList.ForLoadedAnnotation(nonNull(annotation)).toArray(new AnnotationDescription[annotation.length])); new AnnotationList.ForLoadedAnnotation(annotation).toArray(new AnnotationDescription[annotation.length]));
} }


/** /**
Expand All @@ -1981,7 +1980,7 @@ public <T extends Annotation> Builder defineAnnotationArray(String property, Cla
* @return A builder with the additional annotation property. * @return A builder with the additional annotation property.
*/ */
public Builder defineAnnotationArray(String property, TypeDescription annotationType, AnnotationDescription... annotationDescription) { public Builder defineAnnotationArray(String property, TypeDescription annotationType, AnnotationDescription... annotationDescription) {
return define(property, AnnotationValue.ForComplexArray.of(annotationType, nonNull(annotationDescription))); return define(property, AnnotationValue.ForComplexArray.of(annotationType, annotationDescription));
} }


/** /**
Expand All @@ -1992,7 +1991,7 @@ public Builder defineAnnotationArray(String property, TypeDescription annotation
* @return A builder with the additional type array property. * @return A builder with the additional type array property.
*/ */
public Builder defineTypeArray(String property, Class<?>... type) { public Builder defineTypeArray(String property, Class<?>... type) {
return defineTypeArray(property, new TypeList.ForLoadedTypes(nonNull(type)).toArray(new TypeDescription[type.length])); return defineTypeArray(property, new TypeList.ForLoadedTypes(type).toArray(new TypeDescription[type.length]));
} }


/** /**
Expand Down
@@ -1,5 +1,8 @@
package net.bytebuddy.description.modifier; package net.bytebuddy.description.modifier;


import java.util.Arrays;
import java.util.List;

/** /**
* An element that describes a type modifier as described in the * An element that describes a type modifier as described in the
* <a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html">JVMS</a>. * <a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html">JVMS</a>.
Expand Down Expand Up @@ -64,4 +67,25 @@ interface ForType extends ModifierContributor {
interface ForParameter extends ModifierContributor { interface ForParameter extends ModifierContributor {
/* marker interface */ /* marker interface */
} }

class Resolver<T extends ModifierContributor> {

private final List<T> modifierContributors;

protected Resolver(List<T> modifierContributors) {
this.modifierContributors = modifierContributors;
}

public static <S extends ModifierContributor> Resolver<S> of(S... modifierContributor) {
return new Resolver<S>(Arrays.asList(modifierContributor));
}

public int resolve() {
int modifiers = EMPTY_MASK;
for (T modifierContributor : modifierContributors) {
modifiers = (modifiers & ~modifierContributor.getRange()) | modifierContributor.getMask();
}
return modifiers;
}
}
} }
Expand Up @@ -14,6 +14,7 @@
import net.bytebuddy.dynamic.TargetType; import net.bytebuddy.dynamic.TargetType;
import net.bytebuddy.implementation.bytecode.StackSize; import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.utility.CompoundList;
import net.bytebuddy.utility.JavaType; import net.bytebuddy.utility.JavaType;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
Expand All @@ -26,7 +27,6 @@
import java.util.*; import java.util.*;


import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.utility.ByteBuddyCommons.join;


/** /**
* Implementations of this interface represent a Java type, i.e. a class or interface. Instances of this interface always * Implementations of this interface represent a Java type, i.e. a class or interface. Instances of this interface always
Expand Down Expand Up @@ -2588,7 +2588,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "TypeDescription.Generic.LazyProjection.OfLoadedParameter.Dispatcher.ForModernVm{" + return "TypeDescription.Generic.LazyProjection.ForLoadedParameter.Dispatcher.ForModernVm{" +
"getType=" + getType + "getType=" + getType +
", getParameterizedType=" + getParameterizedType + ", getParameterizedType=" + getParameterizedType +
'}'; '}';
Expand Down Expand Up @@ -2618,7 +2618,7 @@ public Class<?> getType(Object parameter) {


@Override @Override
public String toString() { public String toString() {
return "TypeDescription.Generic.LazyProjection.OfLoadedParameter.Dispatcher.ForLegacyVm." + name(); return "TypeDescription.Generic.LazyProjection.ForLoadedParameter.Dispatcher.ForLegacyVm." + name();
} }
} }
} }
Expand Down Expand Up @@ -2933,7 +2933,7 @@ public AnnotationList getInheritedAnnotations() {
for (AnnotationDescription annotationDescription : declaredAnnotations) { for (AnnotationDescription annotationDescription : declaredAnnotations) {
annotationTypes.add(annotationDescription.getAnnotationType()); annotationTypes.add(annotationDescription.getAnnotationType());
} }
return new AnnotationList.Explicit(join(declaredAnnotations, getSuperType().asErasure().getInheritedAnnotations().inherited(annotationTypes))); return new AnnotationList.Explicit(CompoundList.of(declaredAnnotations, getSuperType().asErasure().getInheritedAnnotations().inherited(annotationTypes)));
} }
} }


Expand Down
Expand Up @@ -16,8 +16,6 @@
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;


import static net.bytebuddy.utility.ByteBuddyCommons.nonNull;

/** /**
* Locates a class file or its byte array representation when it is given its type description. * Locates a class file or its byte array representation when it is given its type description.
*/ */
Expand Down Expand Up @@ -480,7 +478,7 @@ public AgentBased(Instrumentation instrumentation, ClassLoadingDelegate classLoa
throw new IllegalArgumentException(instrumentation + " does not support retransformation"); throw new IllegalArgumentException(instrumentation + " does not support retransformation");
} }
this.instrumentation = instrumentation; this.instrumentation = instrumentation;
this.classLoadingDelegate = nonNull(classLoadingDelegate); this.classLoadingDelegate = classLoadingDelegate;
} }


/** /**
Expand Down

0 comments on commit 2fd9ba6

Please sign in to comment.