Skip to content

Commit

Permalink
Simplified representation of raw types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Oct 13, 2015
1 parent 58a843f commit 7c59e77
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 224 deletions.
Expand Up @@ -21,7 +21,7 @@ public interface ClassVisitorWrapper {
* @param hint The current hint. This value should be merged (e.g. {@code hint | foo}) into the value that is returned by this wrapper. * @param hint The current hint. This value should be merged (e.g. {@code hint | foo}) into the value that is returned by this wrapper.
* @return The hint to be provided to the ASM {@code ClassWriter}. * @return The hint to be provided to the ASM {@code ClassWriter}.
*/ */
int wrapWriter(int hint); int mergeWriter(int hint);


/** /**
* Defines a hint that is provided to any {@code ClassReader} when reading a class if applicable. Typically, this gives opportunity to * Defines a hint that is provided to any {@code ClassReader} when reading a class if applicable. Typically, this gives opportunity to
Expand All @@ -31,7 +31,7 @@ public interface ClassVisitorWrapper {
* @param hint The current hint. This value should be merged (e.g. {@code hint | foo}) into the value that is returned by this wrapper. * @param hint The current hint. This value should be merged (e.g. {@code hint | foo}) into the value that is returned by this wrapper.
* @return The hint to be provided to the ASM {@code ClassReader}. * @return The hint to be provided to the ASM {@code ClassReader}.
*/ */
int wrapReader(int hint); int mergeReader(int hint);


/** /**
* Applies a {@code ClassVisitorWrapper} to the creation of a {@link net.bytebuddy.dynamic.DynamicType}. * Applies a {@code ClassVisitorWrapper} to the creation of a {@link net.bytebuddy.dynamic.DynamicType}.
Expand Down Expand Up @@ -100,17 +100,17 @@ public Chain append(ClassVisitorWrapper classVisitorWrapper) {
} }


@Override @Override
public int wrapWriter(int hint) { public int mergeWriter(int hint) {
for (ClassVisitorWrapper classVisitorWrapper : classVisitorWrappers) { for (ClassVisitorWrapper classVisitorWrapper : classVisitorWrappers) {
hint = classVisitorWrapper.wrapWriter(hint); hint = classVisitorWrapper.mergeWriter(hint);
} }
return hint; return hint;
} }


@Override @Override
public int wrapReader(int hint) { public int mergeReader(int hint) {
for (ClassVisitorWrapper classVisitorWrapper : classVisitorWrappers) { for (ClassVisitorWrapper classVisitorWrapper : classVisitorWrappers) {
hint = classVisitorWrapper.wrapReader(hint); hint = classVisitorWrapper.mergeReader(hint);
} }
return hint; return hint;
} }
Expand Down
Expand Up @@ -22,10 +22,7 @@
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.GenericSignatureFormatError; import java.lang.reflect.GenericSignatureFormatError;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashSet; import java.util.*;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.utility.ByteBuddyCommons.join; import static net.bytebuddy.utility.ByteBuddyCommons.join;
Expand Down Expand Up @@ -287,7 +284,7 @@ abstract class AbstractBase extends ModifierReviewable.AbstractBase implements T


@Override @Override
public GenericTypeDescription getSuperType() { public GenericTypeDescription getSuperType() {
return LazyProjection.OfPotentiallyRawType.of(getDeclaredSuperType(), GenericTypeDescription.Visitor.NoOp.INSTANCE); return LazyProjection.OfTransformedType.of(getDeclaredSuperType(), RawTypeWrapper.INSTANCE);
} }


/** /**
Expand All @@ -299,7 +296,7 @@ public GenericTypeDescription getSuperType() {


@Override @Override
public GenericTypeList getInterfaces() { public GenericTypeList getInterfaces() {
return new GenericTypeList.OfPotentiallyRawType(getDeclaredInterfaces(), GenericTypeDescription.Visitor.NoOp.INSTANCE); return new GenericTypeList.OfTransformedTypes(getDeclaredInterfaces(), RawTypeWrapper.INSTANCE);
} }


/** /**
Expand Down Expand Up @@ -681,6 +678,95 @@ public String toString() {
return (isPrimitive() ? "" : (isInterface() ? "interface" : "class") + " ") + getName(); return (isPrimitive() ? "" : (isInterface() ? "interface" : "class") + " ") + getName();
} }


/**
* A visitor that represents all {@link TypeDescription} instances as raw generic types.
*/
protected enum RawTypeWrapper implements GenericTypeDescription.Visitor<GenericTypeDescription> {

/**
* The singleton instance.
*/
INSTANCE;

@Override
public GenericTypeDescription onGenericArray(GenericTypeDescription genericArray) {
return ForGenericArray.Latent.of(genericArray.getComponentType().accept(this), 1);
}

@Override
public GenericTypeDescription onWildcard(GenericTypeDescription wildcard) {
// Wildcards which are used within parameterized types are taken care of by the calling method.
GenericTypeList lowerBounds = wildcard.getLowerBounds();
return lowerBounds.isEmpty()
? GenericTypeDescription.ForWildcardType.Latent.boundedAbove(wildcard.getUpperBounds().getOnly().accept(this))
: GenericTypeDescription.ForWildcardType.Latent.boundedBelow(lowerBounds.getOnly().accept(this));
}

@Override
public GenericTypeDescription onParameterizedType(GenericTypeDescription parameterizedType) {
List<GenericTypeDescription> parameters = new ArrayList<GenericTypeDescription>(parameterizedType.getParameters().size());
for (GenericTypeDescription parameter : parameterizedType.getParameters()) {
parameters.add(parameter.accept(this));
}
GenericTypeDescription ownerType = parameterizedType.getOwnerType();
return new GenericTypeDescription.ForParameterizedType.Latent(parameterizedType.asErasure(),
parameters,
ownerType == null
? TypeDescription.UNDEFINED
: ownerType.accept(this));
}

@Override
public GenericTypeDescription onTypeVariable(GenericTypeDescription typeVariable) {
return new RawTypeVariable(typeVariable);
}

@Override
public GenericTypeDescription onNonGenericType(GenericTypeDescription typeDescription) {
return new ForNonGenericType(typeDescription.asErasure());
}

@Override
public String toString() {
return "TypeDescription.AbstractBase.RawTypeWrapper." + name();
}

/**
* An representation of a type variable with raw type bounds.
*/
protected static class RawTypeVariable extends ForTypeVariable {

/**
* The type variable in its declared form.
*/
private final GenericTypeDescription typeVariable;

/**
* Creates a new raw type representation of a type variable.
*
* @param typeVariable The type variable in its declared form.
*/
protected RawTypeVariable(GenericTypeDescription typeVariable) {
this.typeVariable = typeVariable;
}

@Override
public GenericTypeList getUpperBounds() {
return new GenericTypeList.OfTransformedTypes(typeVariable.getUpperBounds(), RawTypeWrapper.INSTANCE);
}

@Override
public TypeVariableSource getVariableSource() {
return typeVariable.getVariableSource();
}

@Override
public String getSymbol() {
return typeVariable.getSymbol();
}
}
}

/** /**
* An adapter implementation of a {@link TypeDescription} that * An adapter implementation of a {@link TypeDescription} that
* describes any type that is not an array or a primitive type. * describes any type that is not an array or a primitive type.
Expand Down

0 comments on commit 7c59e77

Please sign in to comment.