Skip to content

Commit

Permalink
Removed specific API for generic types from field description.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Jun 9, 2015
1 parent 7994556 commit 49fe122
Show file tree
Hide file tree
Showing 21 changed files with 46 additions and 59 deletions.
4 changes: 2 additions & 2 deletions byte-buddy-dep/src/main/java/net/bytebuddy/ByteBuddy.java
Expand Up @@ -2399,15 +2399,15 @@ protected ValuesMethodAppender(TypeDescription instrumentedType) {
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) { public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
FieldDescription valuesField = instrumentedType.getDeclaredFields().filter(named(ENUM_VALUES)).getOnly(); FieldDescription valuesField = instrumentedType.getDeclaredFields().filter(named(ENUM_VALUES)).getOnly();
MethodDescription cloneArrayMethod = new MethodDescription.Latent(CLONE_METHOD_NAME, MethodDescription cloneArrayMethod = new MethodDescription.Latent(CLONE_METHOD_NAME,
valuesField.getFieldType(), valuesField.getType().asRawType(),
TypeDescription.OBJECT, TypeDescription.OBJECT,
Collections.<TypeDescription>emptyList(), Collections.<TypeDescription>emptyList(),
Opcodes.ACC_PUBLIC, Opcodes.ACC_PUBLIC,
Collections.<TypeDescription>emptyList()); Collections.<TypeDescription>emptyList());
return new Size(new StackManipulation.Compound( return new Size(new StackManipulation.Compound(
FieldAccess.forField(valuesField).getter(), FieldAccess.forField(valuesField).getter(),
MethodInvocation.invoke(cloneArrayMethod), MethodInvocation.invoke(cloneArrayMethod),
TypeCasting.to(valuesField.getFieldType()), TypeCasting.to(valuesField.getType().asRawType()),
MethodReturn.REFERENCE MethodReturn.REFERENCE
).apply(methodVisitor, implementationContext).getMaximalSize(), instrumentedMethod.getStackSize()); ).apply(methodVisitor, implementationContext).getMaximalSize(), instrumentedMethod.getStackSize());
} }
Expand Down
Expand Up @@ -16,25 +16,13 @@
*/ */
public interface FieldDescription extends ByteCodeElement, NamedElement.WithGenericName { public interface FieldDescription extends ByteCodeElement, NamedElement.WithGenericName {


/** GenericTypeDescription getType();
* Returns a description of the type of this field.
*
* @return A type description of this field.
*/
TypeDescription getFieldType();

GenericTypeDescription getFieldTypeGen();


/** /**
* An abstract base implementation of a field description. * An abstract base implementation of a field description.
*/ */
abstract class AbstractFieldDescription extends AbstractModifierReviewable implements FieldDescription { abstract class AbstractFieldDescription extends AbstractModifierReviewable implements FieldDescription {


@Override
public TypeDescription getFieldType() {
return getFieldTypeGen().asRawType();
}

@Override @Override
public String getInternalName() { public String getInternalName() {
return getName(); return getName();
Expand All @@ -47,12 +35,12 @@ public String getSourceCodeName() {


@Override @Override
public String getDescriptor() { public String getDescriptor() {
return getFieldType().getDescriptor(); return getType().asRawType().getDescriptor();
} }


@Override @Override
public String getGenericSignature() { public String getGenericSignature() {
GenericTypeDescription fieldType = getFieldTypeGen(); GenericTypeDescription fieldType = getType();
return fieldType.getSort().isRawType() return fieldType.getSort().isRawType()
? null ? null
: fieldType.accept(new GenericTypeDescription.Visitor.ForSignatureVisitor(new SignatureWriter())).toString(); : fieldType.accept(new GenericTypeDescription.Visitor.ForSignatureVisitor(new SignatureWriter())).toString();
Expand Down Expand Up @@ -85,7 +73,7 @@ public String toGenericString() {
if (getModifiers() != EMPTY_MASK) { if (getModifiers() != EMPTY_MASK) {
stringBuilder.append(Modifier.toString(getModifiers())).append(" "); stringBuilder.append(Modifier.toString(getModifiers())).append(" ");
} }
stringBuilder.append(getFieldTypeGen().getSourceCodeName()).append(" "); stringBuilder.append(getType().getSourceCodeName()).append(" ");
stringBuilder.append(getDeclaringType().getSourceCodeName()).append("."); stringBuilder.append(getDeclaringType().getSourceCodeName()).append(".");
return stringBuilder.append(getName()).toString(); return stringBuilder.append(getName()).toString();
} }
Expand All @@ -96,7 +84,7 @@ public String toString() {
if (getModifiers() != EMPTY_MASK) { if (getModifiers() != EMPTY_MASK) {
stringBuilder.append(Modifier.toString(getModifiers())).append(" "); stringBuilder.append(Modifier.toString(getModifiers())).append(" ");
} }
stringBuilder.append(getFieldType().getSourceCodeName()).append(" "); stringBuilder.append(getType().asRawType().getSourceCodeName()).append(" ");
stringBuilder.append(getDeclaringType().getSourceCodeName()).append("."); stringBuilder.append(getDeclaringType().getSourceCodeName()).append(".");
return stringBuilder.append(getName()).toString(); return stringBuilder.append(getName()).toString();
} }
Expand All @@ -122,7 +110,7 @@ public ForLoadedField(Field field) {
} }


@Override @Override
public GenericTypeDescription getFieldTypeGen() { public GenericTypeDescription getType() {
return new GenericTypeDescription.LazyProjection.OfLoadedFieldType(field); return new GenericTypeDescription.LazyProjection.OfLoadedFieldType(field);
} }


Expand Down Expand Up @@ -197,7 +185,7 @@ public Latent(String fieldName,
} }


@Override @Override
public GenericTypeDescription getFieldTypeGen() { public GenericTypeDescription getType() {
return fieldType; return fieldType;
} }


Expand Down
Expand Up @@ -1624,7 +1624,7 @@ public FieldValueTarget<S> defineField(Field field) {


@Override @Override
public FieldValueTarget<S> defineField(FieldDescription fieldDescription) { public FieldValueTarget<S> defineField(FieldDescription fieldDescription) {
return defineField(fieldDescription.getName(), fieldDescription.getFieldTypeGen(), fieldDescription.getModifiers()); return defineField(fieldDescription.getName(), fieldDescription.getType(), fieldDescription.getModifiers());
} }


@Override @Override
Expand Down
Expand Up @@ -399,13 +399,13 @@ protected class FieldToken extends FieldDescription.AbstractFieldDescription {


private FieldToken(GenericTypeDescription.Visitor<GenericTypeDescription> substitutor, FieldDescription fieldDescription) { private FieldToken(GenericTypeDescription.Visitor<GenericTypeDescription> substitutor, FieldDescription fieldDescription) {
name = fieldDescription.getName(); name = fieldDescription.getName();
fieldType = fieldDescription.getFieldTypeGen().accept(substitutor); fieldType = fieldDescription.getType().accept(substitutor);
modifiers = fieldDescription.getModifiers(); modifiers = fieldDescription.getModifiers();
declaredAnnotations = fieldDescription.getDeclaredAnnotations(); declaredAnnotations = fieldDescription.getDeclaredAnnotations();
} }


@Override @Override
public GenericTypeDescription getFieldTypeGen() { public GenericTypeDescription getType() {
return fieldType; return fieldType;
} }


Expand Down
Expand Up @@ -97,7 +97,7 @@ protected ByteCodeAppender.Size applyGetter(MethodVisitor methodVisitor,
Implementation.Context implementationContext, Implementation.Context implementationContext,
FieldDescription fieldDescription, FieldDescription fieldDescription,
MethodDescription methodDescription) { MethodDescription methodDescription) {
StackManipulation stackManipulation = assigner.assign(fieldDescription.getFieldType(), StackManipulation stackManipulation = assigner.assign(fieldDescription.getType().asRawType(),
methodDescription.getReturnType(), methodDescription.getReturnType(),
dynamicallyTyped); dynamicallyTyped);
if (!stackManipulation.isValid()) { if (!stackManipulation.isValid()) {
Expand Down Expand Up @@ -128,7 +128,7 @@ protected ByteCodeAppender.Size applySetter(MethodVisitor methodVisitor,
FieldDescription fieldDescription, FieldDescription fieldDescription,
MethodDescription methodDescription) { MethodDescription methodDescription) {
StackManipulation stackManipulation = assigner.assign(methodDescription.getParameters().get(0).getType(), StackManipulation stackManipulation = assigner.assign(methodDescription.getParameters().get(0).getType(),
fieldDescription.getFieldType(), fieldDescription.getType().asRawType(),
dynamicallyTyped); dynamicallyTyped);
if (!stackManipulation.isValid()) { if (!stackManipulation.isValid()) {
throw new IllegalStateException("Setter type of " + methodDescription + " is not compatible with " + fieldDescription); throw new IllegalStateException("Setter type of " + methodDescription + " is not compatible with " + fieldDescription);
Expand All @@ -140,7 +140,7 @@ protected ByteCodeAppender.Size applySetter(MethodVisitor methodVisitor,
fieldDescription, fieldDescription,
methodDescription, methodDescription,
new StackManipulation.Compound( new StackManipulation.Compound(
MethodVariableAccess.forType(fieldDescription.getFieldType()) MethodVariableAccess.forType(fieldDescription.getType().asRawType())
.loadOffset(methodDescription.getParameters().get(0).getOffset()), .loadOffset(methodDescription.getParameters().get(0).getOffset()),
stackManipulation, stackManipulation,
FieldAccess.forField(fieldDescription).putter() FieldAccess.forField(fieldDescription).putter()
Expand Down
Expand Up @@ -716,7 +716,7 @@ public MethodDescription registerGetterFor(FieldDescription fieldDescription) {
randomString.nextString()); randomString.nextString());
accessorMethod = new MethodDescription.Latent(name, accessorMethod = new MethodDescription.Latent(name,
instrumentedType, instrumentedType,
fieldDescription.getFieldType(), fieldDescription.getType().asRawType(),
Collections.<TypeDescription>emptyList(), Collections.<TypeDescription>emptyList(),
resolveModifier(fieldDescription.isStatic()), resolveModifier(fieldDescription.isStatic()),
Collections.<TypeDescription>emptyList()); Collections.<TypeDescription>emptyList());
Expand Down Expand Up @@ -746,7 +746,7 @@ public MethodDescription registerSetterFor(FieldDescription fieldDescription) {
accessorMethod = new MethodDescription.Latent(name, accessorMethod = new MethodDescription.Latent(name,
instrumentedType, instrumentedType,
TypeDescription.VOID, TypeDescription.VOID,
Collections.singletonList(fieldDescription.getFieldType()), Collections.singletonList(fieldDescription.getType().asRawType()),
resolveModifier(fieldDescription.isStatic()), resolveModifier(fieldDescription.isStatic()),
Collections.<TypeDescription>emptyList()); Collections.<TypeDescription>emptyList());
registerSetter(fieldDescription, accessorMethod); registerSetter(fieldDescription, accessorMethod);
Expand Down Expand Up @@ -1039,7 +1039,7 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext, Me
? StackManipulation.LegalTrivial.INSTANCE ? StackManipulation.LegalTrivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0), : MethodVariableAccess.REFERENCE.loadOffset(0),
FieldAccess.forField(fieldDescription).getter(), FieldAccess.forField(fieldDescription).getter(),
MethodReturn.returning(fieldDescription.getFieldType()) MethodReturn.returning(fieldDescription.getType().asRawType())
).apply(methodVisitor, implementationContext); ).apply(methodVisitor, implementationContext);
return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize()); return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
} }
Expand Down
Expand Up @@ -1589,12 +1589,12 @@ public Resolved resolve(TypeDescription instrumentedType,
Assigner assigner, Assigner assigner,
boolean dynamicallyTyped) { boolean dynamicallyTyped) {
FieldDescription fieldDescription = instrumentedType.getDeclaredFields().filter(named(name)).getOnly(); FieldDescription fieldDescription = instrumentedType.getDeclaredFields().filter(named(name)).getOnly();
StackManipulation stackManipulation = assigner.assign(fieldDescription.getFieldType(), typeDescription, dynamicallyTyped); StackManipulation stackManipulation = assigner.assign(fieldDescription.getType().asRawType(), typeDescription, dynamicallyTyped);
if (!stackManipulation.isValid()) { if (!stackManipulation.isValid()) {
throw new IllegalStateException("Cannot assign " + fieldDescription + " to " + typeDescription); throw new IllegalStateException("Cannot assign " + fieldDescription + " to " + typeDescription);
} }
return new Resolved.Simple(new StackManipulation.Compound(FieldAccess.forField(fieldDescription).getter(), return new Resolved.Simple(new StackManipulation.Compound(FieldAccess.forField(fieldDescription).getter(),
stackManipulation), fieldDescription.getFieldType()); stackManipulation), fieldDescription.getType().asRawType());
} }


@Override @Override
Expand Down Expand Up @@ -1743,7 +1743,7 @@ public Resolved resolve(TypeDescription instrumentedType,
? StackManipulation.LegalTrivial.INSTANCE ? StackManipulation.LegalTrivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0), : MethodVariableAccess.REFERENCE.loadOffset(0),
FieldAccess.forField(fieldDescription).getter() FieldAccess.forField(fieldDescription).getter()
), fieldDescription.getFieldType()); ), fieldDescription.getType().asRawType());
} }


@Override @Override
Expand Down
Expand Up @@ -1248,7 +1248,7 @@ public StackManipulation resolve(TypeDescription instrumentedType,
? StackManipulation.LegalTrivial.INSTANCE ? StackManipulation.LegalTrivial.INSTANCE
: MethodVariableAccess.REFERENCE.loadOffset(0), : MethodVariableAccess.REFERENCE.loadOffset(0),
FieldAccess.forField(fieldDescription).getter(), FieldAccess.forField(fieldDescription).getter(),
assigner.assign(fieldDescription.getFieldType(), targetType, dynamicallyTyped) assigner.assign(fieldDescription.getType().asRawType(), targetType, dynamicallyTyped)
); );
if (!stackManipulation.isValid()) { if (!stackManipulation.isValid()) {
throw new IllegalStateException("Cannot assign " + fieldDescription + " to " + targetType); throw new IllegalStateException("Cannot assign " + fieldDescription + " to " + targetType);
Expand Down
Expand Up @@ -287,7 +287,7 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext, Me
for (FieldDescription fieldDescription : fieldList) { for (FieldDescription fieldDescription : fieldList) {
fieldLoading[index] = new StackManipulation.Compound( fieldLoading[index] = new StackManipulation.Compound(
thisReference, thisReference,
MethodVariableAccess.forType(fieldDescription.getFieldType()) MethodVariableAccess.forType(fieldDescription.getType().asRawType())
.loadOffset(instrumentedMethod.getParameters().get(index).getOffset()), .loadOffset(instrumentedMethod.getParameters().get(index).getOffset()),
FieldAccess.forField(fieldDescription).putter() FieldAccess.forField(fieldDescription).putter()
); );
Expand Down
Expand Up @@ -448,7 +448,7 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext, Me
for (FieldDescription fieldDescription : fieldList) { for (FieldDescription fieldDescription : fieldList) {
fieldLoading[index] = new StackManipulation.Compound( fieldLoading[index] = new StackManipulation.Compound(
thisReference, thisReference,
MethodVariableAccess.forType(fieldDescription.getFieldType()) MethodVariableAccess.forType(fieldDescription.getType().asRawType())
.loadOffset(instrumentedMethod.getParameters().get(index).getOffset()), .loadOffset(instrumentedMethod.getParameters().get(index).getOffset()),
FieldAccess.forField(fieldDescription).putter() FieldAccess.forField(fieldDescription).putter()
); );
Expand Down
Expand Up @@ -194,7 +194,7 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context implementa
fieldDescription.getDeclaringType().getInternalName(), fieldDescription.getDeclaringType().getInternalName(),
fieldDescription.getInternalName(), fieldDescription.getInternalName(),
fieldDescription.getDescriptor()); fieldDescription.getDescriptor());
return resolveSize(fieldDescription.getFieldType().getStackSize()); return resolveSize(fieldDescription.getType().getStackSize());
} }


/** /**
Expand Down
Expand Up @@ -4255,7 +4255,7 @@ private LazyFieldDescription(int modifiers,
} }


@Override @Override
public GenericTypeDescription getFieldTypeGen() { public GenericTypeDescription getType() {
return signatureResolution.resolveFieldType(fieldTypeDescriptor, typePool, this); return signatureResolution.resolveFieldType(fieldTypeDescriptor, typePool, this);
} }


Expand Down
Expand Up @@ -183,7 +183,7 @@ public static MethodType ofSetter(Field field) {
* @return The type of a setter for the given field. * @return The type of a setter for the given field.
*/ */
public static MethodType ofSetter(FieldDescription fieldDescription) { public static MethodType ofSetter(FieldDescription fieldDescription) {
return new MethodType(TypeDescription.VOID, Collections.singletonList(fieldDescription.getFieldType())); return new MethodType(TypeDescription.VOID, Collections.singletonList(fieldDescription.getType().asRawType()));
} }


/** /**
Expand All @@ -203,7 +203,7 @@ public static MethodType ofGetter(Field field) {
* @return The type of a getter for the given field. * @return The type of a getter for the given field.
*/ */
public static MethodType ofGetter(FieldDescription fieldDescription) { public static MethodType ofGetter(FieldDescription fieldDescription) {
return new MethodType(fieldDescription.getFieldType(), Collections.<TypeDescription>emptyList()); return new MethodType(fieldDescription.getType().asRawType(), Collections.<TypeDescription>emptyList());
} }


/** /**
Expand Down Expand Up @@ -576,7 +576,7 @@ public static MethodHandle ofGetter(FieldDescription fieldDescription) {
return new MethodHandle(HandleType.ofGetter(fieldDescription), return new MethodHandle(HandleType.ofGetter(fieldDescription),
fieldDescription.getDeclaringType(), fieldDescription.getDeclaringType(),
fieldDescription.getInternalName(), fieldDescription.getInternalName(),
fieldDescription.getFieldType(), fieldDescription.getType().asRawType(),
Collections.<TypeDescription>emptyList()); Collections.<TypeDescription>emptyList());
} }


Expand All @@ -601,7 +601,7 @@ public static MethodHandle ofSetter(FieldDescription fieldDescription) {
fieldDescription.getDeclaringType(), fieldDescription.getDeclaringType(),
fieldDescription.getInternalName(), fieldDescription.getInternalName(),
TypeDescription.VOID, TypeDescription.VOID,
Collections.singletonList(fieldDescription.getFieldType())); Collections.singletonList(fieldDescription.getType().asRawType()));
} }


@Override @Override
Expand Down
Expand Up @@ -42,8 +42,8 @@ public void testPrecondition() throws Exception {


@Test @Test
public void testFieldType() throws Exception { public void testFieldType() throws Exception {
assertThat(describe(first).getFieldType(), is((TypeDescription) new TypeDescription.ForLoadedType(first.getType()))); assertThat(describe(first).getType(), is((GenericTypeDescription) new TypeDescription.ForLoadedType(first.getType())));
assertThat(describe(second).getFieldType(), is((TypeDescription) new TypeDescription.ForLoadedType(second.getType()))); assertThat(describe(second).getType(), is((GenericTypeDescription) new TypeDescription.ForLoadedType(second.getType())));
} }


@Test @Test
Expand Down Expand Up @@ -171,7 +171,8 @@ public void testAnnotations() throws Exception {


@Test @Test
public void testGenericTypes() throws Exception { public void testGenericTypes() throws Exception {
assertThat(describe(genericField).getFieldTypeGen(), is(GenericTypeDescription.Sort.describe(genericField.getGenericType()))); assertThat(describe(genericField).getType(), is(GenericTypeDescription.Sort.describe(genericField.getGenericType())));
assertThat(describe(genericField).getType().asRawType(), is((TypeDescription) new TypeDescription.ForLoadedType(genericField.getType())));
} }


@Test @Test
Expand Down
Expand Up @@ -7,9 +7,7 @@
import net.bytebuddy.dynamic.DynamicType; import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.FixedValue; import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.test.utility.DebuggingWrapper;
import org.junit.Test; import org.junit.Test;
import org.objectweb.asm.util.ASMifier;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
Expand Down Expand Up @@ -44,7 +42,7 @@ public void testGenericField() throws Exception {
Class<?> type = unloaded.load(null, ClassLoadingStrategy.Default.WRAPPER).getLoaded(); Class<?> type = unloaded.load(null, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
FieldDescription createdField = new FieldDescription.ForLoadedField(type.getDeclaredField(FOO)); FieldDescription createdField = new FieldDescription.ForLoadedField(type.getDeclaredField(FOO));
FieldDescription originalField = new FieldDescription.ForLoadedField(GenericField.class.getDeclaredField(FOO)); FieldDescription originalField = new FieldDescription.ForLoadedField(GenericField.class.getDeclaredField(FOO));
assertThat(createdField.getFieldTypeGen(), is(originalField.getFieldTypeGen())); assertThat(createdField.getType(), is(originalField.getType()));
} }


@Test @Test
Expand Down

0 comments on commit 49fe122

Please sign in to comment.