Skip to content

Commit

Permalink
Add support for sealed class attributes to ijar
Browse files Browse the repository at this point in the history
  • Loading branch information
cushon authored and copybara-github committed Oct 5, 2021
1 parent 130c96d commit 70ae390
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
27 changes: 27 additions & 0 deletions third_party/ijar/classfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,30 @@ struct RecordAttribute : Attribute {
std::vector<RecordComponentInfo *> components_;
};

// See JVMS §4.7.31
struct PermittedSubclassesAttribute : Attribute {
static PermittedSubclassesAttribute *Read(const u1 *&p,
Constant *attribute_name) {
PermittedSubclassesAttribute *attr = new PermittedSubclassesAttribute;
attr->attribute_name_ = attribute_name;
u2 number_of_exceptions = get_u2be(p);
for (int ii = 0; ii < number_of_exceptions; ++ii) {
attr->permitted_subclasses_.push_back(constant(get_u2be(p)));
}
return attr;
}

void Write(u1 *&p) {
WriteProlog(p, permitted_subclasses_.size() * 2 + 2);
put_u2be(p, permitted_subclasses_.size());
for (size_t ii = 0; ii < permitted_subclasses_.size(); ++ii) {
put_u2be(p, permitted_subclasses_[ii]->slot());
}
}

std::vector<Constant *> permitted_subclasses_;
};

struct GeneralAttribute : Attribute {
static GeneralAttribute* Read(const u1 *&p, Constant *attribute_name,
u4 attribute_length) {
Expand Down Expand Up @@ -1503,6 +1527,9 @@ void HasAttrs::ReadAttrs(const u1 *&p) {
} else if (attr_name == "Record") {
attributes.push_back(
RecordAttribute::Read(p, attribute_name, attribute_length));
} else if (attr_name == "PermittedSubclasses") {
attributes.push_back(
PermittedSubclassesAttribute::Read(p, attribute_name));
} else if (attr_name == "com.google.devtools.ijar.KeepForCompile") {
auto attr = new KeepForCompileAttribute;
attr->attribute_name_ = attribute_name;
Expand Down
1 change: 1 addition & 0 deletions third_party/ijar/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sh_test(
# TODO(cushon): build from source once we have a JDK 11
"nestmates/nestmates.jar",
"records/records.jar",
"sealed/sealed.jar",
":source_debug_extension.jar",
":keep_for_compile_lib.jar",
":largest_regular.jar",
Expand Down
13 changes: 13 additions & 0 deletions third_party/ijar/test/ijar_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ NESTMATES_JAR=$IJAR_SRCDIR/test/nestmates/nestmates.jar
NESTMATES_IJAR=$TEST_TMPDIR/nestmates_interface.jar
RECORDS_JAR=$IJAR_SRCDIR/test/records/records.jar
RECORDS_IJAR=$TEST_TMPDIR/records_interface.jar
SEALED_JAR=$IJAR_SRCDIR/test/sealed/sealed.jar
SEALED_IJAR=$TEST_TMPDIR/sealed_interface.jar
SOURCEDEBUGEXT_JAR=$IJAR_SRCDIR/test/source_debug_extension.jar
SOURCEDEBUGEXT_IJAR=$TEST_TMPDIR/source_debug_extension.jar
CENTRAL_DIR_LARGEST_REGULAR=$IJAR_SRCDIR/test/largest_regular.jar
Expand Down Expand Up @@ -545,6 +547,17 @@ function test_records_attribute() {
expect_log "Record" "Records not preserved!"
}

function test_sealed_attribute() {
ls $IJAR $SEALED_JAR

# Check that Java 16 PermittedSubclasses attributes are preserved
$IJAR $SEALED_JAR $SEALED_IJAR || fail "ijar failed"

$JAVAP -classpath $SEALED_IJAR -v SealedTest >& $TEST_log \
|| fail "javap failed"
expect_log "PermittedSubclasses" "PermittedSubclasses not preserved!"
}

function test_source_debug_extension_attribute() {
# Check that SourceDebugExtension attributes are dropped without a warning
$IJAR $SOURCEDEBUGEXT_JAR $SOURCEDEBUGEXT_IJAR >& $TEST_log || fail "ijar failed"
Expand Down
22 changes: 22 additions & 0 deletions third_party/ijar/test/sealed/SealedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021 The Bazel Authors. All rights reserved.
//
// 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.

/** A class that results in sealed attributes in Java 17. */
sealed class SealedTest permits SealedTest.Foo, SealedTest.Bar {
static final class Foo extends SealedTest {
}

static final class Bar extends SealedTest {
}
}
Binary file added third_party/ijar/test/sealed/sealed.jar
Binary file not shown.

0 comments on commit 70ae390

Please sign in to comment.