Skip to content

Commit

Permalink
Add test for signature polymorphic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
uschindler committed Jun 15, 2016
1 parent 794442a commit 4e9005d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
12 changes: 11 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,9 @@
<condition property="-gen.sunmisc">
<available classname="sun.misc.BASE64Encoder"/>
</condition>
<condition property="-gen.jdk7">
<available classname="java.lang.invoke.MethodHandle"/>
</condition>
<condition property="-gen.jdk8">
<hasmethod classname="java.util.Collections" method="emptySortedSet"/>
</condition>
Expand All @@ -655,14 +658,21 @@
nowarn="true" source="1.6" target="1.6" debug="true" deprecation="false" encoding="${build.encoding}"/>
</target>

<target name="-generate-test-classes-jdk7" if="-gen.jdk7">
<echo level="info" message="Generating test classes for Java 7:"/>
<delete dir="src/test/antunit" includes="Java7*.class"/>
<javac includeantruntime="false" srcdir="src/test/antunit" destdir="src/test/antunit" includes="Java7*.java"
nowarn="true" source="1.7" target="1.7" debug="true" deprecation="false" encoding="${build.encoding}"/>
</target>

<target name="-generate-test-classes-jdk8" if="-gen.jdk8">
<echo level="info" message="Generating test classes for Java 8:"/>
<delete dir="src/test/antunit" includes="Java8*.class"/>
<javac includeantruntime="false" srcdir="src/test/antunit" destdir="src/test/antunit" includes="Java8*.java"
nowarn="true" source="1.8" target="1.8" debug="true" deprecation="false" encoding="${build.encoding}"/>
</target>

<target name="generate-test-classes" depends="-generate-test-classes-init,-generate-test-classes-sunmisc,-generate-test-classes-jdk6,-generate-test-classes-jdk8" description="Regenerates .class files used by tests if the current JDK version supports it"/>
<target name="generate-test-classes" depends="-generate-test-classes-init,-generate-test-classes-sunmisc,-generate-test-classes-jdk6,-generate-test-classes-jdk7,-generate-test-classes-jdk8" description="Regenerates .class files used by tests if the current JDK version supports it"/>

<target name="show-help-mojo" depends="install-maven-artifacts" description="Shows help about mojo usage">
<artifact:mvn mavenVersion="${maven.version}" failonerror="true" fork="${maven.fork}" taskname="help">
Expand Down
Binary file added src/test/antunit/Java7MethodHandles.class
Binary file not shown.
30 changes: 30 additions & 0 deletions src/test/antunit/Java7MethodHandles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* (C) Copyright Uwe Schindler (Generics Policeman) and others.
*
* 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.
*/

/* The binary class file is packaged together with the source distribution.
*/

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

class Java7MethodHandles {
static void main(String... args) throws Throwable {
MethodHandle mh = MethodHandles.publicLookup().findVirtual(StringBuilder.class, "append",
MethodType.methodType(StringBuilder.class, int.class));
StringBuilder result = (StringBuilder) mh.invoke(new StringBuilder(), 1);
}
}
34 changes: 34 additions & 0 deletions src/test/antunit/TestJava7.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* (C) Copyright Uwe Schindler (Generics Policeman) and others.
*
* 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.
-->
<project xmlns:au="antlib:org.apache.ant.antunit">

<condition property="has.MethodHandle">
<available classname="java.lang.invoke.MethodHandle"/>
</condition>

<target name="testSignaturePolymorphic" if="has.MethodHandle">
<au:expectfailure expectedMessage="Check for forbidden API calls failed, see log">
<forbiddenapis failOnMissingClasses="false">
<file file="Java7MethodHandles.class"/>
java.lang.invoke.MethodHandle#invoke(java.lang.Object[]) @ Forbidden signature polymorphic method
</forbiddenapis>
</au:expectfailure>
<au:assertLogContains level="error" text="Forbidden method invocation (signature polymorphic): java.lang.invoke.MethodHandle#invoke(java.lang.Object[]) [Forbidden signature polymorphic method]"/>
<au:assertLogContains level="error" text=" 1 error(s)"/>
</target>

</project>

0 comments on commit 4e9005d

Please sign in to comment.