Skip to content

Commit

Permalink
Add regression test for record annotations
Browse files Browse the repository at this point in the history
This adds a simple app using record annotations, for which a
native image gets generated. This is a regression test for
the fix described in issue oracle#3984
  • Loading branch information
jerboaa committed Mar 23, 2022
1 parent d6ed604 commit 8c09ff5
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ jobs:
JDK: "labsjdk-ce-17"
GATE: "style,fullbuild"
PRIMARY: "espresso"
- env:
JDK: "labsjdk-ce-17"
GATE: "recordannotation"
PRIMARY: "substratevm"
steps:
- uses: actions/checkout@v2
with:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ bench-results.json
jmh_result.json
/vm/src/installer/dist/
/tools/generators/node_modules/
/tools/generators/out/
/tools/generators/out/
target/
34 changes: 34 additions & 0 deletions substratevm/mx.substratevm/mx_substratevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def __getattr__(self, name):
'muslcbuild',
'hellomodule',
'condconfig',
'recordannotion',
])

def vm_native_image_path(config=None):
Expand Down Expand Up @@ -473,6 +474,10 @@ def help_stdout_check(output):
if t:
hellomodule([])

with Task('record annotation tests', tasks, tags=[GraalTags.recordannotation]) as t:
if t:
recordannotation([])


def native_unittests_task(extra_build_args=None):
if mx.is_windows():
Expand Down Expand Up @@ -1212,6 +1217,35 @@ def helloworld(args, config=None):
"""
run_helloworld_command(args, config, "helloworld")

@mx.command(suite_name=suite.name, command_name='recordannotation')
def recordannotation(args, config=None):
"""
builds a native image of an annotated Record-using app.
"""
jdk = get_jdk()
if jdk.javaCompliance >= '17':
# Build a record app module with maven
module_path = []
proj_dir = join(suite.dir, 'src', 'native-image-record-annot-tests')
mx.run_maven(['-e', 'package'], cwd=proj_dir)
module_path.append(join(proj_dir, 'target', 'recordannotations.jar'))
config = GraalVMConfig.build(native_images=['native-image', 'lib:native-image-agent', 'lib:native-image-diagnostics-agent'])
with native_image_context(hosted_assertions=False, config=config) as native_image:
module_path_sep = ';' if mx.is_windows() else ':'
run_args = [
'-ea',
'-p', module_path_sep.join(module_path), 'recordannotations.Main'
]
# Build into native image
mx.log('Building image recordannotion: ' + str(module_path))
build_dir = join(svmbuild_dir(), 'recordannotations')
built_image = native_image([
'--verbose', '-H:Path=' + build_dir,
] + run_args)
mx.log('Running image ' + built_image)
mx.run([built_image])
else:
mx.log('Record annotations test skipped as the JDK does not support it.')

@mx.command(suite_name=suite.name, command_name='hellomodule')
def hellomodule(args):
Expand Down
53 changes: 53 additions & 0 deletions substratevm/src/native-image-record-annot-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>records</groupId>
<artifactId>recordannotations</artifactId>
<version>1</version>

<name>recordannotations</name>

<properties>
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<profiles>
<profile>
<id>java-17</id>
<activation>
<jdk>17</jdk>
</activation>
<build>
<finalName>recordannotations</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
<mainClass>recordannotations.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package recordannotations;

// Main record under test. See Main.
@RCA2
record F(@RCA @RCA2 String name, @RCA2 int i) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package recordannotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.RecordComponent;
import java.util.Arrays;
import java.util.List;

public class Main {

public static void main(String[] args) {
failIfAssertionsAreDisabled();

F x = new F("x", 1);

RecordComponent[] recordComponents = x.getClass().getRecordComponents();
assert recordComponents.length == 2;

// Verify String component annotations
RecordComponent stringComp = recordComponents[0];
List<Annotation> stringCompAnnot = Arrays.asList(stringComp.getAnnotations());
assert stringCompAnnot.size() == 2 : "expected 2 record annotations for string component";
assert stringCompAnnot.stream().anyMatch(a -> a.annotationType().getName().equals("recordannotations.RCA"));
assert stringCompAnnot.stream().anyMatch(a -> a.annotationType().getName().equals("recordannotations.RCA2"));
assert stringComp.getAnnotatedType().getType().getTypeName().equals(String.class.getTypeName());
Annotation rcaAnnotation = stringComp.getAnnotation(RCA.class);
assert rcaAnnotation != null : "expected RCA annotation to be present";
assert String.class.getTypeName().equals(stringComp.getGenericType().getTypeName());

// Verify int component annotations
RecordComponent intComp = recordComponents[1];
List<Annotation> intCompAnnot = Arrays.asList(intComp.getAnnotations());
assert intCompAnnot.size() == 1 : "expected 1 record annotations for int component";
assert intCompAnnot.stream().anyMatch(a -> a.annotationType().getName().equals("recordannotations.RCA2"));
assert intComp.getAnnotatedType().getType().getTypeName().equals(int.class.getTypeName());
assert intComp.getAnnotation(RCA.class) == null;
assert intComp.getAnnotation(RCA2.class) != null;
assert int.class.getTypeName().equals(intComp.getGenericType().getTypeName());

assert x.i() == 1;
assert "x".equals(x.name());

System.out.println("Record annotation tests passed!");
}

private static void failIfAssertionsAreDisabled() {
boolean enabled = false;
assert enabled = true;
if (!enabled) {
throw new AssertionError("This example requires that assertions are enabled (-ea)");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package recordannotations;

import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.RECORD_COMPONENT, ElementType.FIELD })
@interface RCA {
// empty
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package recordannotations;

import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.RECORD_COMPONENT, ElementType.FIELD, ElementType.TYPE })
@interface RCA2 {
// empty
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"name" : "recordannotations.F",
"allDeclaredConstructors" : true,
"allPublicConstructors" : true,
"allDeclaredMethods" : true,
"allPublicMethods" : true,
"allDeclaredClasses" : true,
"allPublicClasses" : true
}
]

0 comments on commit 8c09ff5

Please sign in to comment.