Skip to content

Commit

Permalink
Add JSR14 class file and adjust verification to allow for their imple…
Browse files Browse the repository at this point in the history
…mentation.
  • Loading branch information
raphw committed Dec 19, 2020
1 parent 745d77e commit 2738fbc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3326,7 +3326,7 @@ protected ForClassFileVersion(ClassFileVersion classFileVersion) {
public void assertType(int modifiers, boolean definesInterfaces, boolean isGeneric) {
if ((modifiers & Opcodes.ACC_ANNOTATION) != 0 && !classFileVersion.isAtLeast(ClassFileVersion.JAVA_V5)) {
throw new IllegalStateException("Cannot define annotation type for class file version " + classFileVersion);
} else if (isGeneric && !classFileVersion.isAtLeast(ClassFileVersion.JAVA_V5)) {
} else if (isGeneric && !classFileVersion.isAtLeast(ClassFileVersion.JAVA_V4)) { // JSR14 allows for generic 1.4 classes.
throw new IllegalStateException("Cannot define a generic type for class file version " + classFileVersion);
}
}
Expand All @@ -3335,7 +3335,7 @@ public void assertType(int modifiers, boolean definesInterfaces, boolean isGener
* {@inheritDoc}
*/
public void assertField(String name, boolean isPublic, boolean isStatic, boolean isFinal, boolean isGeneric) {
if (isGeneric && !classFileVersion.isAtLeast(ClassFileVersion.JAVA_V5)) {
if (isGeneric && !classFileVersion.isAtLeast(ClassFileVersion.JAVA_V4)) { // JSR14 allows for generic 1.4 classes.
throw new IllegalStateException("Cannot define generic field '" + name + "' for class file version " + classFileVersion);
}
}
Expand All @@ -3352,7 +3352,7 @@ public void assertMethod(String name,
boolean isConstructor,
boolean isDefaultValueIncompatible,
boolean isGeneric) {
if (isGeneric && !classFileVersion.isAtLeast(ClassFileVersion.JAVA_V5)) {
if (isGeneric && !classFileVersion.isAtLeast(ClassFileVersion.JAVA_V4)) { // JSR14 allows for generic 1.4 classes.
throw new IllegalStateException("Cannot define generic method '" + name + "' for class file version " + classFileVersion);
} else if (!isVirtual && isAbstract) {
throw new IllegalStateException("Cannot define static or non-virtual method '" + name + "' to be abstract");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
import net.bytebuddy.implementation.bytecode.member.MethodReturn;
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.utility.RandomString;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2014 - 2020 Rafael Winterhalter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.bytebuddy.test.precompiled;

public class Jsr14Sample<TYPE> {

public Jsr14Sample<TYPE> field;

public <TYPE> Jsr14Sample<TYPE> method() { return null; }
}
13 changes: 11 additions & 2 deletions byte-buddy-dep/src/test/java/net/bytebuddy/ByteBuddyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.StubMethod;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.test.utility.DebuggingWrapper;
import org.junit.Ignore;
import org.junit.Test;

Expand All @@ -20,7 +19,6 @@
import java.util.Collections;

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

Expand Down Expand Up @@ -170,6 +168,17 @@ public void testClassWithManyMethods() throws Exception {
assertThat(subclass.getDeclaredMethods().length, is(1000));
}

@Test
public void testClassCompiledToJsr14() throws Exception {
assertThat(new ByteBuddy()
.redefine(Class.forName("net.bytebuddy.test.precompiled.Jsr14Sample"))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded()
.getConstructor()
.newInstance(), notNullValue());
}

public static class Recorder {

public int counter;
Expand Down
Binary file not shown.

0 comments on commit 2738fbc

Please sign in to comment.