Skip to content

Commit

Permalink
Added further test cases and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Apr 8, 2015
1 parent 6b0c020 commit 5b64737
Show file tree
Hide file tree
Showing 2 changed files with 574 additions and 632 deletions.
Expand Up @@ -559,17 +559,20 @@ public String toString() {
*/ */
class Default implements Instrumentation.Context.ExtractableView, AuxiliaryType.MethodAccessorFactory { class Default implements Instrumentation.Context.ExtractableView, AuxiliaryType.MethodAccessorFactory {


/**
* Indicates that a field should be defined without a default value.
*/
private static final Object NO_DEFAULT_VALUE = null; private static final Object NO_DEFAULT_VALUE = null;


/** /**
* The default name suffix to be appended to an accessor method. * The name suffix to be appended to an accessor method.
*/ */
public static final String DEFAULT_ACCESSOR_METHOD_SUFFIX = "accessor"; public static final String ACCESSOR_METHOD_SUFFIX = "accessor";


/** /**
* The default name suffix to be prepended to a cache field. * The name prefix to be prepended to a field storing a cached value.
*/ */
public static final String DEFAULT_FIELD_CACHE_PREFIX = "cachedValue"; public static final String FIELD_CACHE_PREFIX = "cachedValue";


/** /**
* The instrumented type that this instance represents. * The instrumented type that this instance represents.
Expand All @@ -586,16 +589,6 @@ class Default implements Instrumentation.Context.ExtractableView, AuxiliaryType.
*/ */
private final ClassFileVersion classFileVersion; private final ClassFileVersion classFileVersion;


/**
* The name suffix to be appended to an accessor method.
*/
private final String accessorMethodSuffix;

/**
* The name prefix to be prepended to a cache field.
*/
private final String fieldCachePrefix;

/** /**
* The naming strategy for naming auxiliary types that are registered. * The naming strategy for naming auxiliary types that are registered.
*/ */
Expand Down Expand Up @@ -643,41 +636,22 @@ class Default implements Instrumentation.Context.ExtractableView, AuxiliaryType.
*/ */
private boolean canRegisterFieldCache; private boolean canRegisterFieldCache;


public Default(TypeDescription instrumentedType,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
InstrumentedType.TypeInitializer typeInitializer,
ClassFileVersion classFileVersion) {
this(instrumentedType,
auxiliaryTypeNamingStrategy,
typeInitializer,
classFileVersion,
DEFAULT_ACCESSOR_METHOD_SUFFIX,
DEFAULT_FIELD_CACHE_PREFIX);
}

/** /**
* Creates a new delegate. * Creates a new instrumentation context.
* *
* @param instrumentedType The description of the type that is currently subject of creation. * @param instrumentedType The description of the type that is currently subject of creation.
* @param auxiliaryTypeNamingStrategy The naming strategy for naming an auxiliary type.
* @param typeInitializer The type initializer of the created instrumented type. * @param typeInitializer The type initializer of the created instrumented type.
* @param classFileVersion The class file version of the created class. * @param classFileVersion The class file version of the created class.
* @param accessorMethodSuffix A suffix that is added to any accessor method where the method name is
* prefixed by the accessed method's name.
* @param fieldCachePrefix A prefix that is added to any field cache.
* @param auxiliaryTypeNamingStrategy The naming strategy for naming an auxiliary type.
*/ */
public Default(TypeDescription instrumentedType, public Default(TypeDescription instrumentedType,
AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy, AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
InstrumentedType.TypeInitializer typeInitializer, InstrumentedType.TypeInitializer typeInitializer,
ClassFileVersion classFileVersion, ClassFileVersion classFileVersion) {
String accessorMethodSuffix,
String fieldCachePrefix) {
this.instrumentedType = instrumentedType; this.instrumentedType = instrumentedType;
this.auxiliaryTypeNamingStrategy = auxiliaryTypeNamingStrategy; this.auxiliaryTypeNamingStrategy = auxiliaryTypeNamingStrategy;
this.typeInitializer = typeInitializer; this.typeInitializer = typeInitializer;
this.classFileVersion = classFileVersion; this.classFileVersion = classFileVersion;
this.accessorMethodSuffix = accessorMethodSuffix;
this.fieldCachePrefix = fieldCachePrefix;
registeredAccessorMethods = new HashMap<Instrumentation.SpecialMethodInvocation, MethodDescription>(); registeredAccessorMethods = new HashMap<Instrumentation.SpecialMethodInvocation, MethodDescription>();
registeredGetters = new HashMap<FieldDescription, MethodDescription>(); registeredGetters = new HashMap<FieldDescription, MethodDescription>();
registeredSetters = new HashMap<FieldDescription, MethodDescription>(); registeredSetters = new HashMap<FieldDescription, MethodDescription>();
Expand All @@ -693,7 +667,7 @@ public MethodDescription registerAccessorFor(Instrumentation.SpecialMethodInvoca
MethodDescription accessorMethod = registeredAccessorMethods.get(specialMethodInvocation); MethodDescription accessorMethod = registeredAccessorMethods.get(specialMethodInvocation);
if (accessorMethod == null) { if (accessorMethod == null) {
String name = String.format("%s$%s$%s", specialMethodInvocation.getMethodDescription().getInternalName(), String name = String.format("%s$%s$%s", specialMethodInvocation.getMethodDescription().getInternalName(),
accessorMethodSuffix, ACCESSOR_METHOD_SUFFIX,
randomString.nextString()); randomString.nextString());
accessorMethod = new MethodDescription.Latent(name, accessorMethod = new MethodDescription.Latent(name,
instrumentedType, instrumentedType,
Expand Down Expand Up @@ -732,7 +706,7 @@ public MethodDescription registerGetterFor(FieldDescription fieldDescription) {
MethodDescription accessorMethod = registeredGetters.get(fieldDescription); MethodDescription accessorMethod = registeredGetters.get(fieldDescription);
if (accessorMethod == null) { if (accessorMethod == null) {
String name = String.format("%s$%s$%s", fieldDescription.getName(), String name = String.format("%s$%s$%s", fieldDescription.getName(),
accessorMethodSuffix, ACCESSOR_METHOD_SUFFIX,
randomString.nextString()); randomString.nextString());
accessorMethod = new MethodDescription.Latent(name, accessorMethod = new MethodDescription.Latent(name,
instrumentedType, instrumentedType,
Expand Down Expand Up @@ -761,7 +735,7 @@ public MethodDescription registerSetterFor(FieldDescription fieldDescription) {
MethodDescription accessorMethod = registeredSetters.get(fieldDescription); MethodDescription accessorMethod = registeredSetters.get(fieldDescription);
if (accessorMethod == null) { if (accessorMethod == null) {
String name = String.format("%s$%s$%s", fieldDescription.getName(), String name = String.format("%s$%s$%s", fieldDescription.getName(),
accessorMethodSuffix, ACCESSOR_METHOD_SUFFIX,
randomString.nextString()); randomString.nextString());
accessorMethod = new MethodDescription.Latent(name, accessorMethod = new MethodDescription.Latent(name,
instrumentedType, instrumentedType,
Expand Down Expand Up @@ -810,7 +784,7 @@ public FieldDescription cache(StackManipulation fieldValue, TypeDescription fiel
return fieldCache; return fieldCache;
} }
validateFieldCacheAccessibility(); validateFieldCacheAccessibility();
fieldCache = new FieldDescription.Latent(String.format("%s$%s", fieldCachePrefix, randomString.nextString()), fieldCache = new FieldDescription.Latent(String.format("%s$%s", FIELD_CACHE_PREFIX, randomString.nextString()),
instrumentedType, instrumentedType,
fieldType, fieldType,
FIELD_CACHE_MODIFIER); FIELD_CACHE_MODIFIER);
Expand Down Expand Up @@ -867,8 +841,6 @@ public String toString() {
"instrumentedType=" + instrumentedType + "instrumentedType=" + instrumentedType +
", typeInitializer=" + typeInitializer + ", typeInitializer=" + typeInitializer +
", classFileVersion=" + classFileVersion + ", classFileVersion=" + classFileVersion +
", accessorMethodSuffix='" + accessorMethodSuffix + '\'' +
", fieldCachePrefix='" + fieldCachePrefix + '\'' +
", auxiliaryTypeNamingStrategy=" + auxiliaryTypeNamingStrategy + ", auxiliaryTypeNamingStrategy=" + auxiliaryTypeNamingStrategy +
", registeredAccessorMethods=" + registeredAccessorMethods + ", registeredAccessorMethods=" + registeredAccessorMethods +
", registeredGetters=" + registeredGetters + ", registeredGetters=" + registeredGetters +
Expand Down Expand Up @@ -909,12 +881,13 @@ protected FieldCacheEntry(StackManipulation fieldValue, TypeDescription fieldTyp
} }


/** /**
* Returns the field value that is represented by this field cache entry. * Returns a stack manipulation where the represented value is stored in the given field.
* *
* @return The field value that is represented by this field cache entry. * @param fieldDescription A static field in which the value is to be stored.
* @return A stack manipulation that represents this storage.
*/ */
public StackManipulation getFieldValue() { public StackManipulation storeIn(FieldDescription fieldDescription) {
return fieldValue; return new Compound(this, FieldAccess.forField(fieldDescription).putter());
} }


/** /**
Expand Down Expand Up @@ -955,12 +928,11 @@ public String toString() {
", fieldType=" + fieldType + ", fieldType=" + fieldType +
'}'; '}';
} }

public StackManipulation storeIn(FieldDescription fieldDescription) {
return new Compound(this, FieldAccess.forField(fieldDescription).putter());
}
} }


/**
* An abstract method pool entry that delegates the implementation of a method to itself.
*/
protected abstract static class AbstractDelegationEntry extends TypeWriter.MethodPool.Entry.AbstractDefiningEntry implements ByteCodeAppender { protected abstract static class AbstractDelegationEntry extends TypeWriter.MethodPool.Entry.AbstractDefiningEntry implements ByteCodeAppender {


@Override @Override
Expand Down

0 comments on commit 5b64737

Please sign in to comment.