Skip to content

Commit e3f700f

Browse files
Merge tag 'jdk-23+35' into ola/24.1_jdk-23+35
Added tag jdk-23+35 for changeset 946c6cc
2 parents b4a67a6 + 946c6cc commit e3f700f

File tree

5 files changed

+237
-63
lines changed

5 files changed

+237
-63
lines changed

src/java.desktop/share/classes/javax/swing/text/html/CSS.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,10 @@ Object getInternalCSSValue(CSS.Attribute key, String value) {
844844
}
845845

846846
static Object mergeTextDecoration(String value) {
847+
if (value.startsWith("none")) {
848+
return null;
849+
}
850+
847851
boolean underline = value.contains("underline");
848852
boolean strikeThrough = value.contains("line-through");
849853
if (!underline && !strikeThrough) {

test/jdk/java/lang/instrument/NativeMethodPrefixAgent.java

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,70 +35,69 @@ class NativeMethodPrefixAgent {
3535

3636
static ClassFileTransformer t0, t1, t2;
3737
static Instrumentation inst;
38-
private static Throwable agentError;
38+
private static Throwable agentError; // to be accessed/updated in a synchronized block
3939

40-
public static void checkErrors() {
40+
private static final String CLASS_TO_TRANSFORM = "NativeMethodPrefixApp$Dummy";
41+
42+
public static synchronized void checkErrors() {
4143
if (agentError != null) {
4244
throw new RuntimeException("Agent error", agentError);
4345
}
4446
}
4547

48+
private static synchronized void trackError(final Throwable t) {
49+
if (agentError == null) {
50+
agentError = t;
51+
return;
52+
}
53+
if (agentError != t) {
54+
agentError.addSuppressed(t);
55+
}
56+
}
57+
4658
static class Tr implements ClassFileTransformer {
4759
private static final ClassDesc CD_StringIdCallbackReporter = ClassDesc.ofInternalName("bootreporter/StringIdCallbackReporter");
4860
private static final MethodTypeDesc MTD_void_String_int = MethodTypeDesc.of(CD_void, CD_String, CD_int);
4961
final String trname;
5062
final int transformId;
63+
private final String nativeMethodPrefix;
5164

5265
Tr(int transformId) {
5366
this.trname = "tr" + transformId;
5467
this.transformId = transformId;
68+
this.nativeMethodPrefix = "wrapped_" + trname + "_";
5569
}
5670

57-
public byte[]
58-
transform(
59-
ClassLoader loader,
60-
String className,
61-
Class<?> classBeingRedefined,
62-
ProtectionDomain protectionDomain,
63-
byte[] classfileBuffer) {
64-
boolean redef = classBeingRedefined != null;
65-
System.out.println(trname + ": " +
66-
(redef? "Retransforming " : "Loading ") + className);
67-
if (className != null) {
68-
try {
69-
byte[] newcf = Instrumentor.instrFor(classfileBuffer)
70-
.addNativeMethodTrackingInjection(
71-
"wrapped_" + trname + "_", (name, h) -> {
72-
h.loadConstant(name);
73-
h.loadConstant(transformId);
74-
h.invokestatic(
75-
CD_StringIdCallbackReporter,
76-
"tracker",
77-
MTD_void_String_int);
78-
})
79-
.apply();
80-
/*** debugging ...
81-
if (newcf != null) {
82-
String fname = trname + (redef?"_redef" : "") + "/" + className;
83-
System.err.println("dumping to: " + fname);
84-
write_buffer(fname + "_before.class", classfileBuffer);
85-
write_buffer(fname + "_instr.class", newcf);
86-
}
87-
***/
88-
89-
return redef? null : newcf;
90-
} catch (Throwable ex) {
91-
if (agentError == null) {
92-
agentError = ex;
93-
}
94-
System.err.println("ERROR: Injection failure: " + ex);
95-
ex.printStackTrace();
71+
@Override
72+
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
73+
ProtectionDomain protectionDomain, byte[] classfileBuffer) {
74+
75+
try {
76+
// we only transform a specific application class
77+
if (!className.equals(CLASS_TO_TRANSFORM)) {
78+
return null;
79+
}
80+
if (classBeingRedefined != null) {
9681
return null;
9782
}
83+
// use a byte code generator which creates wrapper methods,
84+
// with a configured native method prefix, for each native method on the
85+
// class being transformed
86+
final Instrumentor byteCodeGenerator = Instrumentor.instrFor(classfileBuffer)
87+
.addNativeMethodTrackingInjection(nativeMethodPrefix,
88+
(name, cb) -> {
89+
cb.loadConstant(name);
90+
cb.loadConstant(transformId);
91+
cb.invokestatic(CD_StringIdCallbackReporter,
92+
"tracker", MTD_void_String_int);
93+
});
94+
// generate the bytecode
95+
return byteCodeGenerator.apply();
96+
} catch (Throwable t) {
97+
trackError(t);
98+
return null;
9899
}
99-
return null;
100100
}
101-
102101
}
103102

104103
// for debugging

test/jdk/java/lang/instrument/NativeMethodPrefixApp.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
*/
2323

2424

25-
import java.io.File;
2625
import java.nio.file.Path;
27-
import java.lang.management.*;
2826

2927
import bootreporter.*;
3028
import jdk.test.lib.helpers.ClassFileInstaller;
@@ -33,27 +31,24 @@
3331

3432
/*
3533
* @test
36-
* @bug 6263319
34+
* @bug 6263319 8334167
3735
* @summary test setNativeMethodPrefix
3836
* @requires ((vm.opt.StartFlightRecording == null) | (vm.opt.StartFlightRecording == false)) & ((vm.opt.FlightRecorder == null) | (vm.opt.FlightRecorder == false))
39-
* @modules java.management
40-
* java.instrument
37+
* @modules java.instrument
4138
* @library /test/lib
4239
* @build bootreporter.StringIdCallback bootreporter.StringIdCallbackReporter
4340
* asmlib.Instrumentor NativeMethodPrefixAgent
4441
* @enablePreview
4542
* @comment The test uses asmlib/Instrumentor.java which relies on ClassFile API PreviewFeature.
46-
* @run driver/timeout=240 NativeMethodPrefixApp roleDriver
47-
* @comment The test uses a higher timeout to prevent test timeouts noted in JDK-6528548
43+
* @run main/native NativeMethodPrefixApp roleDriver
4844
*/
4945
public class NativeMethodPrefixApp implements StringIdCallback {
5046

51-
// This test is fragile like a golden file test.
52-
// It assumes that a specific non-native library method will call a specific
53-
// native method. The below may need to be updated based on library changes.
54-
static String goldenNativeMethodName = "getStartupTime";
55-
47+
// we expect this native method, which is part of this test's application,
48+
// to be instrumented and invoked
49+
static String goldenNativeMethodName = "fooBarNativeMethod";
5650
static boolean[] gotIt = {false, false, false};
51+
private static final String testLibraryPath = System.getProperty("test.nativepath");
5752

5853
public static void main(String[] args) throws Exception {
5954
if (args.length == 1) {
@@ -68,21 +63,19 @@ public static void main(String[] args) throws Exception {
6863
launchApp(agentJar);
6964
} else {
7065
System.err.println("running app");
66+
System.loadLibrary("NativeMethodPrefix"); // load the native library
7167
new NativeMethodPrefixApp().run();
7268
}
7369
}
7470

7571
private static Path createAgentJar() throws Exception {
76-
final String testClassesDir = System.getProperty("test.classes");
7772
final Path agentJar = Path.of("NativeMethodPrefixAgent.jar");
7873
final String manifest = """
7974
Manifest-Version: 1.0
8075
Premain-Class: NativeMethodPrefixAgent
8176
Can-Retransform-Classes: true
8277
Can-Set-Native-Method-Prefix: true
83-
"""
84-
+ "Boot-Class-Path: " + testClassesDir.replace(File.separatorChar, '/') + "/"
85-
+ "\n";
78+
""";
8679
System.out.println("Manifest is:\n" + manifest);
8780
// create the agent jar
8881
ClassFileInstaller.writeJar(agentJar.getFileName().toString(),
@@ -96,6 +89,7 @@ private static void launchApp(final Path agentJar) throws Exception {
9689
final OutputAnalyzer oa = ProcessTools.executeTestJava(
9790
"--enable-preview", // due to usage of ClassFile API PreviewFeature in the agent
9891
"-javaagent:" + agentJar.toString(),
92+
"-Djava.library.path=" + testLibraryPath,
9993
NativeMethodPrefixApp.class.getName());
10094
oa.shouldHaveExitValue(0);
10195
// make available stdout/stderr in the logs, even in case of successful completion
@@ -105,10 +99,11 @@ private static void launchApp(final Path agentJar) throws Exception {
10599
private void run() throws Exception {
106100
StringIdCallbackReporter.registerCallback(this);
107101
System.err.println("start");
108-
109-
java.lang.reflect.Array.getLength(new short[5]);
110-
RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
111-
System.err.println(mxbean.getVmVendor());
102+
final long val = new Dummy().callSomeNativeMethod();
103+
if (val != 42) {
104+
throw new RuntimeException("unexpected return value " + val
105+
+ " from native method, expected 42");
106+
}
112107

113108
NativeMethodPrefixAgent.checkErrors();
114109

@@ -128,4 +123,13 @@ public void tracker(String name, int id) {
128123
System.err.println("Tracked #" + id + ": " + name);
129124
}
130125
}
126+
127+
private static class Dummy {
128+
129+
private long callSomeNativeMethod() {
130+
return fooBarNativeMethod();
131+
}
132+
133+
private native long fooBarNativeMethod();
134+
}
131135
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
#include <stdio.h>
25+
#include "jni.h"
26+
27+
JNIEXPORT jlong JNICALL
28+
Java_NativeMethodPrefixApp_00024Dummy_fooBarNativeMethod(JNIEnv *env, jclass clazz)
29+
{
30+
fprintf(stderr, "native method called\n");
31+
return 42;
32+
}
33+
34+
JNIEXPORT jint JNICALL
35+
JNI_OnLoad(JavaVM *vm, void *reserved)
36+
{
37+
fprintf(stderr, "native library loaded\n");
38+
return JNI_VERSION_1_1; // this native library needs the very basic JNI support
39+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.Dimension;
25+
import java.awt.Graphics;
26+
import java.awt.image.BufferedImage;
27+
import java.io.File;
28+
import java.io.IOException;
29+
30+
import javax.imageio.ImageIO;
31+
import javax.swing.JEditorPane;
32+
import javax.swing.text.View;
33+
import javax.swing.text.html.CSS;
34+
35+
/*
36+
* @test
37+
* @bug 8335967
38+
* @summary Tests 'text-decoration: none' is respected
39+
* @run main HTMLTextDecorationNone
40+
*/
41+
public final class HTMLTextDecorationNone {
42+
private static final String HTML = """
43+
<!DOCTYPE html>
44+
<html lang="en">
45+
<head>
46+
<meta charset="UTF-8">
47+
<title>text-decoration: none (&lt;a&gt;)</title>
48+
<style>
49+
a.none { text-decoration: none }
50+
</style>
51+
</head>
52+
<body>
53+
<p><a href="https://openjdk.org/">underlined</a></p>
54+
<p><a href="https://openjdk.org/" style="text-decoration: none">not underlined</a></p>
55+
<p><a href="https://openjdk.org/" class="none">not underlined</a></p>
56+
<p style="text-decoration: underline"><a
57+
href="https://openjdk.org/" style="text-decoration: none">underlined?</a></p>
58+
<p style="text-decoration: underline"><a
59+
href="https://openjdk.org/" class="none">underlined?</a></p>
60+
</body>
61+
</html>
62+
""";
63+
64+
private static final boolean[] underlined = {true, false, false, true, true};
65+
66+
public static void main(String[] args) {
67+
final JEditorPane html = new JEditorPane("text/html", HTML);
68+
html.setEditable(false);
69+
70+
final Dimension size = html.getPreferredSize();
71+
html.setSize(size);
72+
73+
BufferedImage image = new BufferedImage(size.width, size.height,
74+
BufferedImage.TYPE_INT_RGB);
75+
Graphics g = image.createGraphics();
76+
// Paint the editor pane to ensure all views are created
77+
html.paint(g);
78+
g.dispose();
79+
80+
int errorCount = 0;
81+
String firstError = null;
82+
83+
System.out.println("----- Views -----");
84+
final View bodyView = html.getUI()
85+
.getRootView(html)
86+
.getView(1)
87+
.getView(1);
88+
for (int i = 0; i < bodyView.getViewCount(); i++) {
89+
View pView = bodyView.getView(i);
90+
View contentView = getContentView(pView);
91+
92+
Object decorationAttr =
93+
contentView.getAttributes()
94+
.getAttribute(CSS.Attribute.TEXT_DECORATION);
95+
String decoration = decorationAttr == null
96+
? "none" : decorationAttr.toString();
97+
98+
System.out.println(i + ": " + decoration);
99+
if (decoration.contains("underline") != underlined[i]) {
100+
errorCount++;
101+
if (firstError == null) {
102+
firstError = "Line " + i + ": " + decoration + " vs "
103+
+ (underlined[i] ? "underline" : "none");
104+
}
105+
}
106+
}
107+
108+
if (errorCount > 0) {
109+
saveImage(image);
110+
throw new RuntimeException(errorCount + " error(s) found, "
111+
+ "the first one: " + firstError);
112+
}
113+
}
114+
115+
private static View getContentView(View parent) {
116+
View view = parent.getView(0);
117+
return view.getViewCount() > 0
118+
? getContentView(view)
119+
: view;
120+
}
121+
122+
private static void saveImage(BufferedImage image) {
123+
try {
124+
ImageIO.write(image, "png",
125+
new File("html.png"));
126+
} catch (IOException ignored) { }
127+
}
128+
}

0 commit comments

Comments
 (0)