Skip to content

Commit

Permalink
Added additional documentation and missing test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Aug 4, 2015
1 parent 96e4a0f commit 92402c5
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 16 deletions.
6 changes: 3 additions & 3 deletions byte-buddy-dep/src/main/java/net/bytebuddy/ByteBuddy.java
Expand Up @@ -1338,9 +1338,9 @@ public boolean isDefined() {
}

@Override
public boolean equals(Object o) {
return this == o || !(o == null || getClass() != o.getClass())
&& value.equals(((Defined) o).value);
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& value.equals(((Defined) other).value);
}

@Override
Expand Down
Expand Up @@ -44,8 +44,9 @@ public interface MethodRegistry {
/**
* Prepares this method registry.
*
* @param instrumentedType The instrumented type that should be created.
* @param methodFilter A filter that only matches methods that should be instrumented.
* @param instrumentedType The instrumented type that should be created.
* @param methodGraphCompiler The method graph compiler to be used for analyzing the fully assembled instrumented type.
* @param methodFilter A filter that only matches methods that should be instrumented.
* @return A prepared version of this method registry.
*/
Prepared prepare(InstrumentedType instrumentedType, MethodGraph.Compiler methodGraphCompiler, LatentMethodMatcher methodFilter);
Expand Down Expand Up @@ -80,6 +81,7 @@ interface Compiled {
* Assembles this compiled entry with a method attribute appender.
*
* @param attributeAppender The method attribute appender to apply together with this handler.
* @param methodDescription The method description to apply with this handler.
* @return A method pool entry representing this handler and the given attribute appender.
*/
TypeWriter.MethodPool.Record assemble(MethodAttributeAppender attributeAppender, MethodDescription methodDescription);
Expand Down Expand Up @@ -330,24 +332,46 @@ public String toString() {
}
}

/**
* A handler for implementing a visibility bridge.
*/
enum ForVisibilityBridge implements Handler {

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

@Override
public InstrumentedType prepare(InstrumentedType instrumentedType) {
return instrumentedType;
throw new IllegalStateException("A visibility bridge handler must not apply any preparations");
}

@Override
public Compiled compile(Implementation.Target implementationTarget) {
return new Compiled(implementationTarget.getTypeDescription());
}

@Override
public String toString() {
return "MethodRegistry.Handler.ForVisibilityBridge." + name();
}

/**
* A compiled handler for a visibility bridge handler.
*/
protected static class Compiled implements Handler.Compiled {

/**
* The instrumented type.
*/
private final TypeDescription instrumentedType;

/**
* Creates a new compiled handler for a visibility bridge.
*
* @param instrumentedType The instrumented type.
*/
protected Compiled(TypeDescription instrumentedType) {
this.instrumentedType = instrumentedType;
}
Expand All @@ -356,6 +380,24 @@ protected Compiled(TypeDescription instrumentedType) {
public TypeWriter.MethodPool.Record assemble(MethodAttributeAppender attributeAppender, MethodDescription methodDescription) {
return TypeWriter.MethodPool.Record.ForDefinedMethod.OfVisibilityBridge.of(instrumentedType, methodDescription, attributeAppender);
}

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

@Override
public int hashCode() {
return instrumentedType.hashCode();
}

@Override
public String toString() {
return "MethodRegistry.Handler.ForVisibilityBridge.Compiled{" +
"instrumentedType=" + instrumentedType +
'}';
}
}
}
}
Expand Down Expand Up @@ -583,10 +625,21 @@ protected Entry(LatentMethodMatcher methodMatcher, Handler handler, MethodAttrib
this.attributeAppenderFactory = attributeAppenderFactory;
}

/**
* Transforms this entry into a prepared state.
*
* @param bridges The bridges to be appended to this entry.
* @return A prepared version of this entry.
*/
protected Prepared.Entry asPreparedEntry(Set<MethodDescription.TypeToken> bridges) {
return new Prepared.Entry(handler, attributeAppenderFactory, bridges);
}

/**
* Returns this entry's handler.
*
* @return The handler of this entry.
*/
protected Handler getHandler() {
return handler;
}
Expand Down Expand Up @@ -644,8 +697,14 @@ protected static class Prepared implements MethodRegistry.Prepared {
*/
private final InstrumentedType.TypeInitializer typeInitializer;

/**
* The instrumented type.
*/
private final TypeDescription instrumentedType;

/**
* A method graph describing the instrumented type.
*/
private final MethodGraph.Linked methodGraph;

/**
Expand All @@ -654,6 +713,8 @@ protected static class Prepared implements MethodRegistry.Prepared {
* @param implementations A map of all method descriptions mapped to their handling entries.
* @param loadedTypeInitializer The loaded type initializer of the instrumented type.
* @param typeInitializer The type initializer of the instrumented type.
* @param instrumentedType The instrumented type.
* @param methodGraph A method graph describing the instrumented type.
*/
protected Prepared(LinkedHashMap<MethodDescription, Entry> implementations,
LoadedTypeInitializer loadedTypeInitializer,
Expand Down Expand Up @@ -742,35 +803,103 @@ public String toString() {
'}';
}

/**
* An entry of a prepared method registry.
*/
protected static class Entry {

/**
* Creates an entry for a visibility bridge.
*
* @param bridgeTarget The bridge method's target.
* @param bridges The type tokens describing all bridges.
* @return An entry representing a visibility bridge.
*/
protected static Entry forVisibilityBridge(MethodDescription bridgeTarget, Set<MethodDescription.TypeToken> bridges) {
return new Entry(Handler.ForVisibilityBridge.INSTANCE, new MethodAttributeAppender.ForMethod(bridgeTarget), bridges);
}

/**
* The handler for implementing methods.
*/
private final Handler handler;

/**
* A attribute appender factory for appending attributes for any implemented method.
*/
private final MethodAttributeAppender.Factory attributeAppenderFactory;

/**
* A set of bridges representing the bridge methods of this method.
*/
private final Set<MethodDescription.TypeToken> bridges;

/**
* Creates a new prepared entry.
*
* @param handler The handler for implementing methods.
* @param attributeAppenderFactory A attribute appender factory for appending attributes for any implemented method.
* @param bridges A set of bridges representing the bridge methods of this method.
*/
protected Entry(Handler handler, MethodAttributeAppender.Factory attributeAppenderFactory, Set<MethodDescription.TypeToken> bridges) {
this.handler = handler;
this.attributeAppenderFactory = attributeAppenderFactory;
this.bridges = bridges;
}

/**
* Returns this entry's handler.
*
* @return The entry's handler.
*/
protected Handler getHandler() {
return handler;
}

/**
* Returns this entry's attribute appender factory.
*
* @return This entry's attribute appender factory.
*/
protected MethodAttributeAppender.Factory getAppenderFactory() {
return attributeAppenderFactory;
}

public Set<MethodDescription.TypeToken> getBridges() {
/**
* A set of bridges for the implemented method.
*
* @return A set of bridges for the implemented method.
*/
protected Set<MethodDescription.TypeToken> getBridges() {
return bridges;
}

@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;
Entry entry = (Entry) other;
return handler.equals(entry.handler)
&& attributeAppenderFactory.equals(entry.attributeAppenderFactory)
&& bridges.equals(entry.bridges);
}

@Override
public int hashCode() {
int result = handler.hashCode();
result = 31 * result + attributeAppenderFactory.hashCode();
result = 31 * result + bridges.hashCode();
return result;
}

@Override
public String toString() {
return "MethodRegistry.Default.Prepared.Entry{" +
"handler=" + handler +
", attributeAppenderFactory=" + attributeAppenderFactory +
", bridges=" + bridges +
'}';
}
}
}

Expand Down Expand Up @@ -875,20 +1004,46 @@ public String toString() {
'}';
}

/**
* An entry of a compiled method registry.
*/
protected static class Entry {

/**
* The compiled handler to be used for any implemented method.
*/
private final Handler.Compiled compiledHandler;

/**
* The attribute appender to be used for any implemented method.
*/
private final MethodAttributeAppender attributeAppender;

/**
* The bridges added to any implemented method.
*/
private final Set<MethodDescription.TypeToken> bridges;

/**
* Creates a new entry of a compiled method registry.
*
* @param compiledHandler The compiled handler to be used for any implemented method.
* @param attributeAppender The attribute appender to be used for any implemented method.
* @param bridges The bridges added to any implemented method.
*/
protected Entry(Handler.Compiled compiledHandler, MethodAttributeAppender attributeAppender, Set<MethodDescription.TypeToken> bridges) {
this.compiledHandler = compiledHandler;
this.attributeAppender = attributeAppender;
this.bridges = bridges;
}

/**
* Binds this entry to the provided method description.
*
* @param methodDescription The method to be bound.
* @param instrumentedType The instrumented type.
* @return A record for implementing the provided method for the instrumented type.
*/
protected Record bind(MethodDescription methodDescription, TypeDescription instrumentedType) {
return Record.AccessBridgeWrapper.of(compiledHandler.assemble(attributeAppender, methodDescription),
instrumentedType,
Expand Down
Expand Up @@ -1356,9 +1356,9 @@ public ByteCodeAppender appender(Target implementationTarget) {
}

@Override
public boolean equals(Object o) {
return this == o || !(o == null || getClass() != o.getClass())
&& Arrays.equals(implementation, ((Compound) o).implementation);
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& Arrays.equals(implementation, ((Compound) other).implementation);
}

@Override
Expand Down
Expand Up @@ -324,6 +324,7 @@ public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(MethodRegistry.Default.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Default.Entry.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Default.Prepared.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Default.Prepared.Entry.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Default.Compiled.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Default.Compiled.Entry.class).apply();
}
Expand Down
@@ -1,6 +1,7 @@
package net.bytebuddy.dynamic.scaffold;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ModifierResolver;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.attribute.MethodAttributeAppender;
Expand All @@ -15,6 +16,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MethodRegistryHandlerTest {
Expand Down Expand Up @@ -81,11 +83,18 @@ public void testHandlerForAnnotationValue() throws Exception {
assertThat(record.getModifierResolver(), is(modifierResolver));
}

@Test(expected = IllegalStateException.class)
public void testVisibilityBridgeHandlerPreparationThrowsException() throws Exception {
MethodRegistry.Handler.ForVisibilityBridge.INSTANCE.prepare(mock(InstrumentedType.class));
}

@Test
public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(MethodRegistry.Handler.ForAbstractMethod.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Handler.ForImplementation.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Handler.ForImplementation.Compiled.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Handler.ForAnnotationValue.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Handler.ForVisibilityBridge.class).apply();
ObjectPropertyAssertion.of(MethodRegistry.Handler.ForVisibilityBridge.Compiled.class).apply();
}
}
Expand Up @@ -78,8 +78,8 @@ public static void foo() {
}

@Override
public boolean equals(Object obj) {
return obj != null && obj.getClass() == getClass();
public boolean equals(Object other) {
return other != null && other.getClass() == getClass();
}

@Override
Expand All @@ -95,8 +95,8 @@ public static void bar() {
}

@Override
public boolean equals(Object obj) {
return obj != null && obj.getClass() == getClass();
public boolean equals(Object other) {
return other != null && other.getClass() == getClass();
}

@Override
Expand Down
Expand Up @@ -1100,8 +1100,8 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
return super.equals(obj);
public boolean equals(Object other) {
return super.equals(other);
}

@Override
Expand Down

0 comments on commit 92402c5

Please sign in to comment.