Skip to content

Commit

Permalink
Properly reading and applying Signature attribute/type for patterns (#14
Browse files Browse the repository at this point in the history
)
  • Loading branch information
lahodaj authored Jan 28, 2025
1 parent 1831b6c commit f902c8f
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,10 @@ protected void read(Symbol sym, int attrLen) {
} finally {
readingClassAttr = false;
}
} else if (sym.type.hasTag(TypeTag.PATTERN)){
Type mtype = poolReader.getType(nextChar());
sym.type = new PatternType(mtype.getParameterTypes(), syms.voidType, syms.methodClass);
//TODO: no thrown types for PatternType
} else {
List<Type> thrown = sym.type.getThrownTypes();
sym.type = poolReader.getType(nextChar());
Expand Down Expand Up @@ -1377,10 +1381,12 @@ protected void read(Symbol sym, int attrLen) {
parameterNameIndicesMp = null;
parameterAccessFlags = null;

MethodSymbol msym = (MethodSymbol) sym;
msym.type = new PatternType(patternType.getParameterTypes(), syms.voidType, syms.methodClass);

readMemberAttrs(sym);

MethodSymbol msym = (MethodSymbol) sym;
msym.bindings = computeParamsFromAttribute(msym, patternType.getParameterTypes(), 0);
msym.bindings = computeParamsFromAttribute(msym, msym.type.asPatternType().getBindingTypes(), 0);

parameterAnnotations = oldParameterAnnotations;
parameterNameIndicesLvt = oldParameterNameIndicesLvt;
Expand All @@ -1400,14 +1406,10 @@ protected void read(Symbol sym, int attrLen) {
if (msym.patternFlags.contains(PatternFlags.DECONSTRUCTOR)) {
//TODO: should check the method is static, and has a reasonable first/only parameter?
//reconstitue the deconstructor back:
MethodType mtype = msym.type.asMethodType();
mtype.argtypes = mtype.argtypes.tail;
msym.flags_field &= ~Flags.STATIC;
}

// todo: check if special handling is needed similar to generic methods for binding types

msym.type = new PatternType(patternType.getParameterTypes(), syms.voidType, syms.methodClass);
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2025, 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.
*
* 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.
*/

import toolbox.*;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* @test
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.jdeps/com.sun.tools.javap
* @enablePreview
* @build toolbox.ToolBox toolbox.JavapTask
* @run main SeparateCompilation
*/
public class SeparateCompilation extends TestRunner {
private ToolBox tb;
private static final String SOURCE_VERSION = System.getProperty("java.specification.version");

SeparateCompilation() {
super(System.err);
tb = new ToolBox();
}

public static void main(String... args) throws Exception {
new SeparateCompilation().runTests();
}

public void runTests() throws Exception {
runTests(m -> new Object[] { Paths.get(m.getName()) });
}

@Test
public void testOne(Path base) throws Exception {
Path current = base.resolve(".");
Path lib = current.resolve("lib");
Path libSrc = lib.resolve("src");
Path libClasses = lib.resolve("classes");

tb.writeJavaFiles(libSrc,
"""
package lib;
import java.util.List;
public class Lib {
public pattern Lib(List<? extends String> values) {
match Lib(null);
}
}
""");

Files.createDirectories(libClasses);

new JavacTask(tb)
.options("-XDrawDiagnostics", "--enable-preview", "--release", SOURCE_VERSION)
.outdir(libClasses)
.files(tb.findJavaFiles(libSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);

Path test = current.resolve("test");
Path testSrc = test.resolve("src");
Path testClasses = test.resolve("classes");

tb.writeJavaFiles(testSrc,
"""
package test;
import java.util.List;
import lib.Lib;
public class Test {
public List<? extends String> convert(Object o) {
return o instanceof Lib(List<? extends String> data) ? data : null;
}
}
""");

Files.createDirectories(testClasses);

new JavacTask(tb)
.options("--enable-preview", "--release", SOURCE_VERSION,
"--class-path", libClasses.toString())
.outdir(testClasses)
.files(tb.findJavaFiles(testSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
}

}

0 comments on commit f902c8f

Please sign in to comment.