Skip to content

Commit

Permalink
Fix IllegalArgumentException Not an array type for bridge methods
Browse files Browse the repository at this point in the history
javac copies annotations to bridge methods (which is good), however the annotations
might become invalid. For instance, the bridge signature for @nullable Object[] is
Object (non-array), so usage=ARRAY signature is invalid
We ignore those annotations for the bridge methods.

See https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6695379

fixes smallrye#102
  • Loading branch information
vlsi committed Jan 16, 2021
1 parent f0dbb56 commit 079d9d9
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 1 deletion.
67 changes: 67 additions & 0 deletions java8-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jdk.min.version>1.8</jdk.min.version>
</properties>

<parent>
<groupId>org.jboss</groupId>
<artifactId>jboss-parent</artifactId>
<version>12</version>
</parent>

<artifactId>jandex-java8-tests</artifactId>
<version>2.2.3.Final-SNAPSHOT</version>
<name>Java Annotation Indexer tests: Java8</name>
<packaging>jar</packaging>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<debug>true</debug>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
<version>2.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2021 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 org.jboss.jandex.test.bridge;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Indexer;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
import org.jboss.jandex.TypeTarget;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class BridgeMethodTest {
public static class ArrayWithNullableElementsConsumer
implements Consumer<@Nullable Object[]> {
@Override
public void accept(@Nullable Object[] objects) {
}
}

@Test
public void arrayWithNullableElements() throws IOException {
verifyMethodSignature(
ArrayWithNullableElementsConsumer.class,
"accept",
TypeTarget.Usage.METHOD_PARAMETER, "java.lang.Object",
"@Nullable java.lang.Object[]");
}

public static class NullableArrayConsumer
implements Consumer<Object @Nullable []> {
@Override
public void accept(Object @Nullable [] objects) {
}
}

@Test
public void nullableArray() throws IOException {
verifyMethodSignature(
NullableArrayConsumer.class,
"accept",
TypeTarget.Usage.METHOD_PARAMETER, "@Nullable java.lang.Object",
"java.lang.Object @Nullable []");
}

public static class NullableArrayWithNullableElementsConsumer
implements Consumer<@Nullable Object @Nullable []> {
@Override
public void accept(@Nullable Object @Nullable [] objects) {
}
}

@Test
public void nullableArrayWithNullableElementsConsumer() throws IOException {
verifyMethodSignature(
NullableArrayWithNullableElementsConsumer.class,
"accept",
TypeTarget.Usage.METHOD_PARAMETER, "@Nullable java.lang.Object",
"@Nullable java.lang.Object @Nullable []");
}

public static class ArrayWithNullableElementsSupplier
implements Supplier<@Nullable Object[]> {
@Override
public @Nullable Object[] get() {
return new Object[0];
}
}

@Test
public void arrayWithNullableElementsSupplier() throws IOException {
verifyMethodSignature(
ArrayWithNullableElementsSupplier.class,
"get",
TypeTarget.Usage.EMPTY,
"java.lang.Object",
"@Nullable java.lang.Object[]");
}

public static class NullableArraySupplier
implements Supplier<Object @Nullable []> {
@Override
public Object @Nullable [] get() {
return null;
}
}

@Test
public void nullableArraySupplier() throws IOException {
verifyMethodSignature(
NullableArraySupplier.class,
"get",
TypeTarget.Usage.EMPTY,
"@Nullable java.lang.Object",
"java.lang.Object @Nullable []");
}

private boolean isBridge(MethodInfo methodInfo) {
int bridgeModifiers = 0x1000 /* SYNTHETIC */ | 0x40 /* BRIDGE */;
return (methodInfo.flags() & bridgeModifiers) == bridgeModifiers;
}

private InputStream getClassBytes(Class<?> klass) {
String fileName = klass.getName();
fileName = fileName.substring(fileName.lastIndexOf('.') + 1);
return klass.getResourceAsStream(fileName + ".class");
}

private void verifyMethodSignature(
Class<?> klass,
String methodName,
TypeTarget.Usage usage,
String expectedBridgeType,
String expectedNonBridgeType) throws IOException {
Indexer indexer = new Indexer();
ClassInfo info = indexer.index(getClassBytes(klass));
int methods = 0;
for (MethodInfo method : info.methods()) {
if (!methodName.equals(method.name())) {
continue;
}
String expectedType = isBridge(method) ? expectedBridgeType : expectedNonBridgeType;
Type type;
switch (usage) {
case METHOD_PARAMETER:
type = method.parameters().get(0);
break;
case EMPTY:
type = method.returnType();
break;
default:
throw new IllegalArgumentException("Expected METHOD_PARAMETER or EMPTY, got " + usage);
}
Assert.assertEquals(type + " signature for " +
(isBridge(method) ? "" : "non-") + "bridge method " + method,
expectedType, type.toString());
methods++;
}
if (methods == 0) {
Assert.fail("At least one '" + methodName + "' method is expected in " + klass);
}
}
}
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>java8-tests</id>
<modules>
<module>java8-tests</module>
</modules>
</profile>
</profiles>
<repositories>
<repository>
<id>jboss-public-repository</id>
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/org/jboss/jandex/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@ private void resolveTypeAnnotation(AnnotationTarget target, TypeAnnotationState
return;
}

if (targetsArrayInBridgeMethod(typeAnnotationState, types[index], method)) {
return;
}

types[index] = resolveTypePath(types[index], typeAnnotationState);
method.setParameters(intern(types));
} else if (typeTarget.usage() == TypeTarget.Usage.EMPTY && target instanceof FieldInfo) {
Expand All @@ -731,7 +735,11 @@ private void resolveTypeAnnotation(AnnotationTarget target, TypeAnnotationState
if (((EmptyTypeTarget) typeTarget).isReceiver()) {
method.setReceiverType(resolveTypePath(method.receiverType(), typeAnnotationState));
} else {
method.setReturnType(resolveTypePath(method.returnType(), typeAnnotationState));
Type returnType = method.returnType();
if (targetsArrayInBridgeMethod(typeAnnotationState, returnType, method)) {
return;
}
method.setReturnType(resolveTypePath(returnType, typeAnnotationState));
}
} else if (typeTarget.usage() == TypeTarget.Usage.THROWS && target instanceof MethodInfo) {
MethodInfo method = (MethodInfo) target;
Expand All @@ -747,6 +755,30 @@ private void resolveTypeAnnotation(AnnotationTarget target, TypeAnnotationState
}
}

private boolean targetsArrayInBridgeMethod(
TypeAnnotationState typeAnnotationState, Type type, MethodInfo method) {
// javac copies annotations to bridge methods (which is good), however the annotations
// might become invalid. For instance, the bridge signature for @Nullable Object[] is
// Object (non-array), so usage=ARRAY signature is invalid
// We ignore those annotations for the bridge methods.
// See https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6695379
return type.kind() != Type.Kind.ARRAY && isBridge(method) &&
targetsArray(typeAnnotationState);
}

private boolean isBridge(MethodInfo methodInfo) {
int bridgeModifiers = Modifiers.SYNTHETIC | 0x40;
return (methodInfo.flags() & bridgeModifiers) == bridgeModifiers;
}

private boolean targetsArray(TypeAnnotationState typeAnnotationState) {
if (typeAnnotationState.pathElements.size() == 0) {
return false;
}
PathElement pathElement = typeAnnotationState.pathElements.peek();
return pathElement != null && pathElement.kind == PathElement.Kind.ARRAY;
}

private Type resolveTypePath(Type type, TypeAnnotationState typeAnnotationState) {
PathElementStack elements = typeAnnotationState.pathElements;
PathElement element = elements.pop();
Expand Down Expand Up @@ -823,6 +855,9 @@ private void updateTypeTarget(AnnotationTarget enclosingTarget, TypeAnnotationSt
} else {
MethodInfo method = (MethodInfo) enclosingTarget;
type = target.asEmpty().isReceiver() ? method.receiverType() : method.returnType();
if (targetsArrayInBridgeMethod(typeAnnotationState, type, method)) {
return;
}
}
break;
}
Expand All @@ -835,6 +870,9 @@ private void updateTypeTarget(AnnotationTarget enclosingTarget, TypeAnnotationSt
case METHOD_PARAMETER: {
MethodInfo method = (MethodInfo) enclosingTarget;
type = method.methodInternal().parameterArray()[target.asMethodParameterType().position()];
if (targetsArrayInBridgeMethod(typeAnnotationState, type, method)) {
return;
}
break;
}
case TYPE_PARAMETER: {
Expand Down

0 comments on commit 079d9d9

Please sign in to comment.