Skip to content

Commit

Permalink
Refactored transformation object to contain acutal transformation logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Oct 22, 2015
1 parent ebc94db commit 2eb58f8
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 72 deletions.
Expand Up @@ -1661,67 +1661,101 @@ public String toString() {
} }
} }


/** protected interface Transformation extends RawMatcher {
* A registered transformation as a combination of a
* {@link net.bytebuddy.agent.builder.AgentBuilder.RawMatcher} and a
* {@link net.bytebuddy.agent.builder.AgentBuilder.Transformer}.
*/
protected static class Transformation implements RawMatcher, Transformer {


/** byte[] apply(TypeDescription typeDescription,
* The raw matcher that is represented by this transformation. BinaryLocator.Initialized initialized,
*/ ClassLoader classLoader,
private final RawMatcher rawMatcher; ProtectionDomain protectionDomain,
InitializationStrategy initializationStrategy,
DefinitionHandler definitionHandler,
ByteBuddy byteBuddy,
MethodRebaseResolver.MethodNameTransformer methodNameTransformer,
BootstrapInjectionStrategy bootstrapInjectionStrategy,
AccessControlContext accessControlContext);


/** class Simple implements Transformation {
* The transformer that is represented by this transformation.
*/
private final Transformer transformer;


/** /**
* Creates a new transformation. * The raw matcher that is represented by this transformation.
* */
* @param rawMatcher The raw matcher that is represented by this transformation. private final RawMatcher rawMatcher;
* @param transformer The transformer that is represented by this transformation.
*/
protected Transformation(RawMatcher rawMatcher, Transformer transformer) {
this.rawMatcher = rawMatcher;
this.transformer = transformer;
}


@Override /**
public boolean matches(TypeDescription typeDescription, * The transformer that is represented by this transformation.
ClassLoader classLoader, */
Class<?> classBeingRedefined, private final Transformer transformer;
ProtectionDomain protectionDomain) {
return rawMatcher.matches(typeDescription, classLoader, classBeingRedefined, protectionDomain);
}


@Override /**
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription) { * Creates a new transformation.
return transformer.transform(builder, typeDescription); *
} * @param rawMatcher The raw matcher that is represented by this transformation.
* @param transformer The transformer that is represented by this transformation.
*/
protected Simple(RawMatcher rawMatcher, Transformer transformer) {
this.rawMatcher = rawMatcher;
this.transformer = transformer;
}


@Override @Override
public boolean equals(Object other) { public boolean matches(TypeDescription typeDescription,
return this == other || !(other == null || getClass() != other.getClass()) ClassLoader classLoader,
&& rawMatcher.equals(((Transformation) other).rawMatcher) Class<?> classBeingRedefined,
&& transformer.equals(((Transformation) other).transformer); ProtectionDomain protectionDomain) {
} return rawMatcher.matches(typeDescription, classLoader, classBeingRedefined, protectionDomain);
}


@Override
public int hashCode() {
int result = rawMatcher.hashCode();
result = 31 * result + transformer.hashCode();
return result;
}


@Override @Override
public String toString() { public byte[] apply(TypeDescription typeDescription,
return "AgentBuilder.Default.Transformation{" + BinaryLocator.Initialized initialized,
"rawMatcher=" + rawMatcher + ClassLoader classLoader,
", transformer=" + transformer + ProtectionDomain protectionDomain,
'}'; InitializationStrategy initializationStrategy,
DefinitionHandler definitionHandler,
ByteBuddy byteBuddy,
MethodRebaseResolver.MethodNameTransformer methodNameTransformer,
BootstrapInjectionStrategy bootstrapInjectionStrategy,
AccessControlContext accessControlContext) {
DynamicType.Unloaded<?> dynamicType = initializationStrategy.apply(
transformer.transform(definitionHandler.builder(typeDescription,
byteBuddy,
initialized.getClassFileLocator(),
methodNameTransformer), typeDescription)).make();
Map<TypeDescription, LoadedTypeInitializer> loadedTypeInitializers = dynamicType.getLoadedTypeInitializers();
if (!loadedTypeInitializers.isEmpty()) {
ClassInjector classInjector = classLoader == null
? bootstrapInjectionStrategy.make(protectionDomain)
: new ClassInjector.UsingReflection(classLoader, protectionDomain, accessControlContext, PackageDefinitionStrategy.NoOp.INSTANCE, true);
for (Map.Entry<TypeDescription, Class<?>> auxiliary : classInjector.inject(dynamicType.getRawAuxiliaryTypes()).entrySet()) {
initializationStrategy.initialize(auxiliary.getValue(), loadedTypeInitializers.get(auxiliary.getKey()));
}
}
initializationStrategy.register(typeDescription.getName(), classLoader, loadedTypeInitializers.get(dynamicType.getTypeDescription()));
return dynamicType.getBytes();
}

@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& rawMatcher.equals(((Simple) other).rawMatcher)
&& transformer.equals(((Simple) other).transformer);
}

@Override
public int hashCode() {
int result = rawMatcher.hashCode();
result = 31 * result + transformer.hashCode();
return result;
}

@Override
public String toString() {
return "AgentBuilder.Default.Transformation.Simple{" +
"rawMatcher=" + rawMatcher +
", transformer=" + transformer +
'}';
}
} }
} }


Expand Down Expand Up @@ -1765,23 +1799,18 @@ public byte[] transform(ClassLoader classLoader,
TypeDescription typeDescription = initialized.getTypePool().describe(binaryTypeName).resolve(); TypeDescription typeDescription = initialized.getTypePool().describe(binaryTypeName).resolve();
for (Transformation transformation : transformations) { for (Transformation transformation : transformations) {
if (transformation.matches(typeDescription, classLoader, classBeingRedefined, protectionDomain)) { if (transformation.matches(typeDescription, classLoader, classBeingRedefined, protectionDomain)) {
DynamicType.Unloaded<?> dynamicType = initializationStrategy.apply( byte[] transformed = transformation.apply(typeDescription,
transformation.transform(definitionHandler.builder(typeDescription, initialized,
byteBuddy, classLoader,
initialized.getClassFileLocator(), protectionDomain,
methodNameTransformer), typeDescription)).make(); initializationStrategy,
Map<TypeDescription, LoadedTypeInitializer> loadedTypeInitializers = dynamicType.getLoadedTypeInitializers(); definitionHandler,
if (loadedTypeInitializers.size() > 1) { byteBuddy,
ClassInjector classInjector = classLoader == null methodNameTransformer,
? bootstrapInjectionStrategy.make(protectionDomain) bootstrapInjectionStrategy,
: new ClassInjector.UsingReflection(classLoader, protectionDomain, accessControlContext, PackageDefinitionStrategy.NoOp.INSTANCE, true); accessControlContext);
for (Map.Entry<TypeDescription, Class<?>> auxiliary : classInjector.inject(dynamicType.getRawAuxiliaryTypes()).entrySet()) { listener.onTransformation(typeDescription, null);
initializationStrategy.initialize(auxiliary.getValue(), loadedTypeInitializers.get(auxiliary.getKey())); return transformed;
}
}
initializationStrategy.register(binaryTypeName, classLoader, loadedTypeInitializers.get(dynamicType.getTypeDescription()));
listener.onTransformation(typeDescription, dynamicType);
return dynamicType.getBytes();
} }
} }
listener.onIgnored(binaryTypeName); listener.onIgnored(binaryTypeName);
Expand Down Expand Up @@ -1928,7 +1957,7 @@ protected AgentBuilder materialize() {
disableSelfInitialization, disableSelfInitialization,
retransformation, retransformation,
bootstrapInjectionStrategy, bootstrapInjectionStrategy,
join(new Transformation(rawMatcher, transformer), transformations)); join(new Transformation.Simple(rawMatcher, transformer), transformations));
} }


/** /**
Expand Down
Expand Up @@ -235,7 +235,7 @@ public AccessControlContext create() {
} }
}).apply(); }).apply();
ObjectPropertyAssertion.of(AgentBuilder.Default.Matched.class).apply(); ObjectPropertyAssertion.of(AgentBuilder.Default.Matched.class).apply();
ObjectPropertyAssertion.of(AgentBuilder.Default.Transformation.class).apply(); ObjectPropertyAssertion.of(AgentBuilder.Default.Transformation.Simple.class).apply();
ObjectPropertyAssertion.of(AgentBuilder.Default.BootstrapInjectionStrategy.Enabled.class).apply(); ObjectPropertyAssertion.of(AgentBuilder.Default.BootstrapInjectionStrategy.Enabled.class).apply();
ObjectPropertyAssertion.of(AgentBuilder.Default.BootstrapInjectionStrategy.Disabled.class).apply(); ObjectPropertyAssertion.of(AgentBuilder.Default.BootstrapInjectionStrategy.Disabled.class).apply();
ObjectPropertyAssertion.of(AgentBuilder.Default.ExecutingTransformer.class).applyBasic(); ObjectPropertyAssertion.of(AgentBuilder.Default.ExecutingTransformer.class).applyBasic();
Expand Down

0 comments on commit 2eb58f8

Please sign in to comment.