Skip to content

Commit

Permalink
Implements ASM ClassVisitor for ASM7 in AnnotationAcceptingListener.A…
Browse files Browse the repository at this point in the history
…nnotatedClassVisitor (#4148)

Allow for ASM inspecting JDK 14 classes and files a warning, an exception is thrown for JDK 15

Signed-off-by: Jan Supol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Jun 14, 2019
1 parent d0d0594 commit 420ae28
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 3 deletions.
Expand Up @@ -30,6 +30,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

/**
* A parser to make a {@link ClassVisitor} visit a ClassFile structure, as defined in the Java
Expand All @@ -43,6 +44,8 @@
*/
public class ClassReader {

private static final Logger LOGGER = Logger.getLogger(ClassReader.class.getName());

/**
* A flag to skip the Code attributes. If this flag is set the Code attributes are neither parsed
* nor visited.
Expand Down Expand Up @@ -190,7 +193,10 @@ public ClassReader(
this.b = classFileBuffer;
// Check the class' major_version. This field is after the magic and minor_version fields, which
// use 4 and 2 bytes respectively.
if (checkClassVersion && readShort(classFileOffset + 6) > Opcodes.V13) {
if (checkClassVersion && readShort(classFileOffset + 6) == Opcodes.V14) {
LOGGER.warning("Unsupported class file major version " + readShort(classFileOffset + 6));
}
if (checkClassVersion && readShort(classFileOffset + 6) > Opcodes.V14) {
throw new IllegalArgumentException(
"Unsupported class file major version " + readShort(classFileOffset + 6));
}
Expand Down
Expand Up @@ -269,6 +269,7 @@ public interface Opcodes {
int V11 = 0 << 16 | 55;
int V12 = 0 << 16 | 56;
int V13 = 0 << 16 | 57;
int V14 = 0 << 16 | 58;

/**
* Version flag indicating that the class is using 'preview' features.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -38,7 +38,9 @@
import jersey.repackaged.org.objectweb.asm.ClassVisitor;
import jersey.repackaged.org.objectweb.asm.FieldVisitor;
import jersey.repackaged.org.objectweb.asm.MethodVisitor;
import jersey.repackaged.org.objectweb.asm.ModuleVisitor;
import jersey.repackaged.org.objectweb.asm.Opcodes;
import jersey.repackaged.org.objectweb.asm.TypePath;

/**
* A scanner listener that processes Java class files (resource names
Expand Down Expand Up @@ -164,7 +166,7 @@ private final class AnnotatedClassVisitor extends ClassVisitor {
private boolean isAnnotated;

private AnnotatedClassVisitor() {
super(Opcodes.ASM5);
super(Opcodes.ASM7);
}

public void visit(final int version, final int access, final String name,
Expand Down Expand Up @@ -228,6 +230,25 @@ public MethodVisitor visitMethod(final int i, final String string,
return null;
}

public ModuleVisitor visitModule(final String name, final int access, final String version) {
// Do nothing
return null;
}

public void visitNestHost(final String nestHost) {
// do nothing
}

public void visitNestMember(final String nestMember) {
// do nothing
}

public AnnotationVisitor visitTypeAnnotation(
final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
//do nothing
return null;
}

private Class getClassForName(final String className) {
try {
final OsgiRegistry osgiRegistry = ReflectionHelper.getOsgiRegistryInstance();
Expand Down
56 changes: 56 additions & 0 deletions tests/integration/asm/pom.xml
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>org.glassfish.jersey.tests.integration</groupId>
<version>2.29-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>asm</artifactId>
<name>jersey-asm-integration</name>
<description>
Controls a new version of ASM being repackaged does not break Jersey
</description>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.integration.asm;

import jersey.repackaged.org.objectweb.asm.ClassVisitor;
import org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class AnnotatedClassVisitorTest {

@Test
public void testInheritedMethodsFromClassVisitor() {
Class<?> annotatedClassVisitorClass = null;
final Class<?> classVisitorClass = ClassVisitor.class;

final Class<?>[] listenerClasses = AnnotationAcceptingListener.class.getDeclaredClasses();

for (Class<?> c : listenerClasses) {
if (c.getName().contains("AnnotatedClassVisitor")) {
annotatedClassVisitorClass = c;
break;
}
}

final List<Method> classVisitorMethods = Arrays.asList(classVisitorClass.getDeclaredMethods());
final List<Method> annotatedClassVisitorMethods = Arrays.asList(annotatedClassVisitorClass.getDeclaredMethods());
boolean containsAllMethods = true;
for (Method classVisitorMethod : classVisitorMethods) {
boolean foundClassVisitorMethod = false;
for (Method annotatedClassVisitorMethod : annotatedClassVisitorMethods) {
if (annotatedClassVisitorMethod.getName().equals(classVisitorMethod.getName())
&& annotatedClassVisitorMethod.getReturnType() == classVisitorMethod.getReturnType()
&& annotatedClassVisitorMethod.getParameterCount() == classVisitorMethod.getParameterCount()) {
final Class<?>[] annotatedClassVisitorTypes = annotatedClassVisitorMethod.getParameterTypes();
final Class<?>[] classVisitorTypes = classVisitorMethod.getParameterTypes();
boolean typesMatch = true;
for (int i = 0; i != annotatedClassVisitorTypes.length; i++) {
if (annotatedClassVisitorTypes[i] != classVisitorTypes[i]) {
typesMatch = false;
break;
}
}
if (typesMatch) {
foundClassVisitorMethod = true;
//System.out.println("found method " + classVisitorMethod.getName());
break;
}
}
}
if (!foundClassVisitorMethod) {
containsAllMethods = false;
System.out.append("Method ")
.append(classVisitorMethod.getName())
.println(" not implemented by AnnotationAcceptingListener.AnnotatedClassVisitor");
}
}
Assert.assertThat(containsAllMethods, Matchers.is(true));
}
}
1 change: 1 addition & 0 deletions tests/integration/pom.xml
Expand Up @@ -33,6 +33,7 @@
<name>jersey-tests-integration</name>

<modules>
<module>asm</module>
<module>async-jersey-filter</module>
<module>cdi-beanvalidation-webapp</module>
<module>cdi-ejb-test-webapp</module>
Expand Down

0 comments on commit 420ae28

Please sign in to comment.