Skip to content

Commit

Permalink
Fixed another overvalidation for pre Java-8 interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Oct 15, 2015
1 parent 13ad642 commit 858a504
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1591,8 +1591,7 @@ public MethodVisitor visitMethod(int modifiers, String name, String descriptor,
name.equals(MethodDescription.CONSTRUCTOR_INTERNAL_NAME)
|| name.equals(MethodDescription.TYPE_INITIALIZER_INTERNAL_NAME)
|| (modifiers & Opcodes.ACC_PRIVATE) != 0,
signature != null,
name.equals(MethodDescription.TYPE_INITIALIZER_INTERNAL_NAME));
signature != null);
return new ValidatingMethodVisitor(super.visitMethod(modifiers, name, descriptor, signature, exceptions), name);
}

Expand Down Expand Up @@ -1628,16 +1627,14 @@ protected interface Constraint {
* @param isDefaultValueIncompatible {@code true} if a method's signature cannot describe an annotation property method.
* @param isNonStaticNonVirtual {@code true} if the method is non-virtual and non-static, i.e. a constructor, type initializer or private.
* @param isGeneric {@code true} if this method defines a generic signature.
* @param isTypeInitializer {@code true} if this method represents the type initializer.
*/
void assertMethod(String name,
boolean isAbstract,
boolean isPublic,
boolean isStatic,
boolean isDefaultValueIncompatible,
boolean isNonStaticNonVirtual,
boolean isGeneric,
boolean isTypeInitializer);
boolean isGeneric);

/**
* Asserts the legitimacy of an annotation for the instrumented type.
Expand Down Expand Up @@ -1706,8 +1703,7 @@ public void assertMethod(String name,
boolean isStatic,
boolean isDefaultValueIncompatible,
boolean isNonStaticNonVirtual,
boolean isGeneric,
boolean isTypeInitializer) {
boolean isGeneric) {
if (isAbstract && manifestType) {
throw new IllegalStateException("Cannot define abstract method '" + name + "' for non-abstract class");
}
Expand Down Expand Up @@ -1761,8 +1757,7 @@ public void assertMethod(String name,
boolean isStatic,
boolean isDefaultValueIncompatible,
boolean isNonStaticNonVirtual,
boolean isGeneric,
boolean isTypeInitializer) {
boolean isGeneric) {
throw new IllegalStateException("Cannot define a method for a package description type");
}

Expand Down Expand Up @@ -1839,14 +1834,15 @@ public void assertMethod(String name,
boolean isStatic,
boolean isDefaultValueIncompatible,
boolean isNonStaticNonVirtual,
boolean isGeneric,
boolean isTypeInitializer) {
if ((!isPublic || isNonStaticNonVirtual) && (classic || !isTypeInitializer)) {
throw new IllegalStateException("Cannot define non-public or non-virtual method '" + name + "' for interface type");
} else if (classic && isStatic) {
throw new IllegalStateException("Cannot define static method '" + name + "' for a pre-Java 8 interface type");
} else if (classic && !isAbstract) {
throw new IllegalStateException("Cannot define default method '" + name + "' for pre-Java 8 interface type");
boolean isGeneric) {
if (!name.equals(MethodDescription.TYPE_INITIALIZER_INTERNAL_NAME)) {
if (!isPublic || isNonStaticNonVirtual) {
throw new IllegalStateException("Cannot define non-public or non-virtual method '" + name + "' for interface type");
} else if (classic && isStatic) {
throw new IllegalStateException("Cannot define static method '" + name + "' for a pre-Java 8 interface type");
} else if (classic && !isAbstract) {
throw new IllegalStateException("Cannot define default method '" + name + "' for pre-Java 8 interface type");
}
}
}

Expand Down Expand Up @@ -1919,14 +1915,15 @@ public void assertMethod(String name,
boolean isStatic,
boolean isDefaultValueIncompatible,
boolean isNonStaticNonVirtual,
boolean isGeneric,
boolean isTypeInitializer) {
if (!isPublic || isNonStaticNonVirtual) {
throw new IllegalStateException("Cannot define non-public, non-abstract or non-virtual method '" + name + "' for annotation type");
} else if (classic && isStatic) {
throw new IllegalStateException("Cannot define static method '" + name + "' for a pre-Java 8 annotation type");
} else if (!isStatic && isDefaultValueIncompatible) {
throw new IllegalStateException("Cannot define method '" + name + "' with the given signature as an annotation type method");
boolean isGeneric) {
if (!name.equals(MethodDescription.TYPE_INITIALIZER_INTERNAL_NAME)) {
if (!isPublic || isNonStaticNonVirtual) {
throw new IllegalStateException("Cannot define non-public, non-abstract or non-virtual method '" + name + "' for annotation type");
} else if (classic && isStatic) {
throw new IllegalStateException("Cannot define static method '" + name + "' for a pre-Java 8 annotation type");
} else if (!isStatic && isDefaultValueIncompatible) {
throw new IllegalStateException("Cannot define method '" + name + "' with the given signature as an annotation type method");
}
}
}

Expand Down Expand Up @@ -2000,8 +1997,7 @@ public void assertMethod(String name,
boolean isStatic,
boolean isDefaultValueIncompatible,
boolean isNonStaticNonVirtual,
boolean isGeneric,
boolean isTypeInitializer) {
boolean isGeneric) {
if (isGeneric && !classFileVersion.isAtLeastJava5()) {
throw new IllegalStateException("Cannot define generic method '" + name + "' for class file version " + classFileVersion);
} else if ((isStatic || isNonStaticNonVirtual) && isAbstract) {
Expand Down Expand Up @@ -2080,17 +2076,15 @@ public void assertMethod(String name,
boolean isStatic,
boolean isDefaultValueIncompatible,
boolean isNonStaticNonVirtual,
boolean isGeneric,
boolean isTypeInitializer) {
boolean isGeneric) {
for (Constraint constraint : constraints) {
constraint.assertMethod(name,
isAbstract,
isPublic,
isStatic,
isDefaultValueIncompatible,
isNonStaticNonVirtual,
isGeneric,
isTypeInitializer);
isGeneric);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.bytebuddy.description.modifier.Ownership;
import net.bytebuddy.description.modifier.TypeManifestation;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy;
import net.bytebuddy.implementation.StubMethod;
import net.bytebuddy.implementation.SuperMethodCall;
Expand All @@ -20,6 +21,10 @@
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;

import static net.bytebuddy.matcher.ElementMatchers.isTypeInitializer;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class TypeWriterDefaultTest {

private static final String FOO = "foo", BAR = "bar";
Expand Down Expand Up @@ -273,6 +278,24 @@ public void testAnnotationOnMethodPreJava5TypeAssertion() throws Exception {
.make();
}

@Test
public void testTypeInitializerOnInterface() throws Exception {
assertThat(new ByteBuddy()
.makeInterface()
.invokable(isTypeInitializer())
.intercept(StubMethod.INSTANCE)
.make(), notNullValue(DynamicType.class));
}

@Test
public void testTypeInitializerOnAnnotation() throws Exception {
assertThat(new ByteBuddy()
.makeAnnotation()
.invokable(isTypeInitializer())
.intercept(StubMethod.INSTANCE)
.make(), notNullValue(DynamicType.class));
}

@Test
public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(TypeWriter.Default.ForCreation.class).apply();
Expand Down

0 comments on commit 858a504

Please sign in to comment.