Skip to content

Commit f00a748

Browse files
author
Roger Riggs
committed
8304915: Create jdk.internal.util.Architecture enum and apply
Reviewed-by: erikj, mdoerr, amitkumar
1 parent 7d07d19 commit f00a748

File tree

7 files changed

+354
-68
lines changed

7 files changed

+354
-68
lines changed

make/modules/java.base/gensrc/GensrcMisc.gmk

+25-3
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,33 @@ $(eval $(call SetupTextFileProcessing, BUILD_VERSION_JAVA, \
5050
@@VENDOR_URL_VM_BUG@@ => $(VENDOR_URL_VM_BUG), \
5151
))
5252

53+
54+
# Normalize OPENJDK_TARGET_CPU name to match jdk.internal.util.Architecture enum
55+
ifneq ($(filter $(OPENJDK_TARGET_CPU), ppc64le), )
56+
OPENJDK_TARGET_ARCH_CANONICAL = ppc64
57+
else ifneq ($(filter $(OPENJDK_TARGET_CPU), s390x), )
58+
OPENJDK_TARGET_ARCH_CANONICAL = s390
59+
else ifneq ($(filter $(OPENJDK_TARGET_CPU), x86_64 amd64), )
60+
OPENJDK_TARGET_ARCH_CANONICAL = x64
61+
else
62+
OPENJDK_TARGET_ARCH_CANONICAL := $(OPENJDK_TARGET_CPU)
63+
endif
64+
65+
# Normalize OPENJDK_TARGET_OS operating system name to match jdk.internal.util.OperatingSystem enum
66+
ifeq ($(OPENJDK_TARGET_OS), macosx)
67+
OPENJDK_TARGET_OS_CANONICAL = macos
68+
else
69+
OPENJDK_TARGET_OS_CANONICAL := $(OPENJDK_TARGET_OS)
70+
endif
71+
5372
$(eval $(call SetupTextFileProcessing, BUILD_PLATFORMPROPERTIES_JAVA, \
54-
SOURCE_FILES := $(TOPDIR)/src/java.base/share/classes/jdk/internal/util/OperatingSystemProps.java.template, \
55-
OUTPUT_FILE := $(SUPPORT_OUTPUTDIR)/gensrc/java.base/jdk/internal/util/OperatingSystemProps.java, \
73+
SOURCE_FILES := $(TOPDIR)/src/java.base/share/classes/jdk/internal/util/PlatformProps.java.template, \
74+
OUTPUT_FILE := $(SUPPORT_OUTPUTDIR)/gensrc/java.base/jdk/internal/util/PlatformProps.java, \
5675
REPLACEMENTS := \
57-
@@OPENJDK_TARGET_OS@@ => $(OPENJDK_TARGET_OS), \
76+
@@OPENJDK_TARGET_OS@@ => $(OPENJDK_TARGET_OS_CANONICAL) ; \
77+
@@OPENJDK_TARGET_CPU@@ => $(OPENJDK_TARGET_ARCH_CANONICAL) ; \
78+
@@OPENJDK_TARGET_CPU_ENDIAN@@ => $(OPENJDK_TARGET_CPU_ENDIAN) ; \
79+
@@OPENJDK_TARGET_CPU_BITS@@ => $(OPENJDK_TARGET_CPU_BITS), \
5880
))
5981

6082
TARGETS += $(BUILD_VERSION_JAVA) $(BUILD_PLATFORMPROPERTIES_JAVA)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright (c) 2023, 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+
package jdk.internal.util;
24+
25+
import jdk.internal.vm.annotation.ForceInline;
26+
import java.util.Locale;
27+
28+
/**
29+
* System architecture enum values.
30+
* Each architecture, except OTHER, has a matching {@code public static boolean isXXX()} method
31+
* that is true when running on that architecture.
32+
* The values of `OPENJDK_TARGET_CPU` from the build are mapped to the
33+
* architecture values.
34+
*/
35+
public enum Architecture {
36+
OTHER, // An unknown architecture not specifically named
37+
X64, // Represents AMD64 and X86_64
38+
X86,
39+
AARCH64,
40+
RISCV64,
41+
S390,
42+
PPC64,
43+
;
44+
45+
private static Architecture CURRENT_ARCH = initArch(PlatformProps.CURRENT_ARCH_STRING);
46+
47+
/**
48+
* {@return {@code true} if the current architecture is X64, Aka amd64}
49+
*/
50+
@ForceInline
51+
public static boolean isX64() {
52+
return PlatformProps.TARGET_ARCH_IS_X64;
53+
}
54+
55+
/**
56+
* {@return {@code true} if the current architecture is X86}
57+
*/
58+
@ForceInline
59+
public static boolean isX86() {
60+
return PlatformProps.TARGET_ARCH_IS_X86;
61+
}
62+
63+
/**
64+
* {@return {@code true} if the current architecture is RISCV64}
65+
*/
66+
@ForceInline
67+
public static boolean isRISCV64() {
68+
return PlatformProps.TARGET_ARCH_IS_RISCV64;
69+
}
70+
71+
/**
72+
* {@return {@code true} if the current architecture is S390}
73+
*/
74+
@ForceInline
75+
public static boolean isS390() {
76+
return PlatformProps.TARGET_ARCH_IS_S390;
77+
}
78+
79+
/**
80+
* {@return {@code true} if the current architecture is PPC64}
81+
* Use {@link #isLittleEndian()} to determine big or little endian.
82+
*/
83+
@ForceInline
84+
public static boolean isPPC64() {
85+
return PlatformProps.TARGET_ARCH_IS_PPC64;
86+
}
87+
88+
/**
89+
* {@return {@code true} if the current architecture is AARCH64}
90+
*/
91+
@ForceInline
92+
public static boolean isAARCH64() {
93+
return PlatformProps.TARGET_ARCH_IS_AARCH64;
94+
}
95+
96+
/**
97+
* {@return the current architecture}
98+
*/
99+
public static Architecture current() {
100+
return CURRENT_ARCH;
101+
}
102+
103+
/**
104+
* {@return {@code true} if the current architecture is 64-bit}
105+
*/
106+
@ForceInline
107+
public static boolean is64bit() {
108+
return PlatformProps.TARGET_ARCH_BITS == 64;
109+
}
110+
111+
/**
112+
* {@return {@code true} if the current architecture is little-endian}
113+
*/
114+
@ForceInline
115+
public static boolean isLittleEndian() {
116+
return PlatformProps.TARGET_ARCH_LITTLE_ENDIAN;
117+
}
118+
119+
120+
/**
121+
* Returns the Architecture of the built architecture.
122+
* Build time names are mapped to respective uppercase enum values.
123+
* Names not recognized are mapped to Architecture.OTHER.
124+
*/
125+
private static Architecture initArch(String archName) {
126+
try {
127+
return Architecture.valueOf(archName.toUpperCase(Locale.ROOT));
128+
} catch (IllegalArgumentException ile) {
129+
return Architecture.OTHER;
130+
}
131+
}
132+
}

src/java.base/share/classes/jdk/internal/util/OperatingSystem.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
*/
2323
package jdk.internal.util;
2424

25-
import jdk.internal.util.OperatingSystemProps;
25+
import jdk.internal.util.PlatformProps;
2626
import jdk.internal.vm.annotation.ForceInline;
2727

28+
import java.util.Locale;
29+
2830
/**
2931
* Enumeration of operating system types and testing for the current OS.
3032
* The enumeration can be used to dispatch to OS specific code or values.
@@ -80,45 +82,54 @@ public enum OperatingSystem {
8082
AIX,
8183
;
8284

83-
// Cache a copy of the array for lightweight indexing
84-
private static final OperatingSystem[] osValues = OperatingSystem.values();
85+
private static final OperatingSystem CURRENT_OS = initOS(PlatformProps.CURRENT_OS_STRING);
8586

8687
/**
8788
* {@return {@code true} if built for the Linux operating system}
8889
*/
8990
@ForceInline
9091
public static boolean isLinux() {
91-
return OperatingSystemProps.TARGET_OS_IS_LINUX;
92+
return PlatformProps.TARGET_OS_IS_LINUX;
9293
}
9394

9495
/**
9596
* {@return {@code true} if built for the Mac OS X operating system}
9697
*/
9798
@ForceInline
9899
public static boolean isMacOS() {
99-
return OperatingSystemProps.TARGET_OS_IS_MACOSX;
100+
return PlatformProps.TARGET_OS_IS_MACOS;
100101
}
101102

102103
/**
103104
* {@return {@code true} if built for the Windows operating system}
104105
*/
105106
@ForceInline
106107
public static boolean isWindows() {
107-
return OperatingSystemProps.TARGET_OS_IS_WINDOWS;
108+
return PlatformProps.TARGET_OS_IS_WINDOWS;
108109
}
109110

110111
/**
111112
* {@return {@code true} if built for the AIX operating system}
112113
*/
113114
@ForceInline
114115
public static boolean isAix() {
115-
return OperatingSystemProps.TARGET_OS_IS_AIX;
116+
return PlatformProps.TARGET_OS_IS_AIX;
116117
}
117118

118119
/**
119120
* {@return the current operating system}
120121
*/
121122
public static OperatingSystem current() {
122-
return osValues[OperatingSystemProps.CURRENT_OS_ORDINAL];
123+
return CURRENT_OS;
124+
}
125+
126+
/**
127+
* Returns the OperatingSystem of the build.
128+
* Build time names are mapped to respective uppercase enum values.
129+
* Names not recognized throw ExceptionInInitializerError with IllegalArgumentException.
130+
*/
131+
private static OperatingSystem initOS(String osName) {
132+
return OperatingSystem.valueOf(osName.toUpperCase(Locale.ROOT));
123133
}
134+
124135
}

src/java.base/share/classes/jdk/internal/util/OperatingSystemProps.java.template

-46
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2023, 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+
package jdk.internal.util;
24+
25+
/**
26+
* The corresponding source file is generated by GensrcMisc.gmk for java.base.
27+
* @see OperatingSystem
28+
* @see Architecture
29+
*/
30+
class PlatformProps {
31+
32+
// Name of the current OperatingSystem enum as substituted by the build
33+
static final String CURRENT_OS_STRING = "@@OPENJDK_TARGET_OS@@";
34+
35+
// Precomputed booleans for each Operating System
36+
static final boolean TARGET_OS_IS_LINUX = "@@OPENJDK_TARGET_OS@@" == "linux";
37+
static final boolean TARGET_OS_IS_MACOS = "@@OPENJDK_TARGET_OS@@" == "macos";
38+
static final boolean TARGET_OS_IS_WINDOWS = "@@OPENJDK_TARGET_OS@@" == "windows";
39+
static final boolean TARGET_OS_IS_AIX = "@@OPENJDK_TARGET_OS@@" == "aix";
40+
41+
// The Architecture value for the current architecture
42+
static final String CURRENT_ARCH_STRING = "@@OPENJDK_TARGET_CPU@@";
43+
44+
// Architecture.is64Bit() uses this value
45+
static final int TARGET_ARCH_BITS = @@OPENJDK_TARGET_CPU_BITS@@;
46+
47+
// Architecture.isLittleEndian value from the build
48+
static final boolean TARGET_ARCH_LITTLE_ENDIAN = "@@OPENJDK_TARGET_CPU_ENDIAN@@" == "little";
49+
50+
// Precomputed booleans for each Architecture, shared with jdk.internal.util.Architecture
51+
// The variables are named to match the Architecture value names, and
52+
// the values chosen to match the build values.
53+
static final boolean TARGET_ARCH_IS_X64 = "@@OPENJDK_TARGET_CPU@@" == "x64";
54+
static final boolean TARGET_ARCH_IS_X86 = "@@OPENJDK_TARGET_CPU@@" == "x86";
55+
static final boolean TARGET_ARCH_IS_AARCH64 = "@@OPENJDK_TARGET_CPU@@" == "aarch64";
56+
static final boolean TARGET_ARCH_IS_RISCV64 = "@@OPENJDK_TARGET_CPU@@" == "riscv64";
57+
static final boolean TARGET_ARCH_IS_S390 = "@@OPENJDK_TARGET_CPU@@" == "s390";
58+
static final boolean TARGET_ARCH_IS_PPC64 = "@@OPENJDK_TARGET_CPU@@" == "ppc64";
59+
}

src/jdk.attach/windows/classes/sun/tools/attach/AttachProviderImpl.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,16 +37,6 @@
3737
public class AttachProviderImpl extends HotSpotAttachProvider {
3838

3939
public AttachProviderImpl() {
40-
String os = System.getProperty("os.name");
41-
if (os.startsWith("Windows 9") || os.equals("Windows Me")) {
42-
throw new RuntimeException(
43-
"This provider is not supported on this version of Windows");
44-
}
45-
String arch = System.getProperty("os.arch");
46-
if (!arch.equals("x86") && !arch.equals("amd64") && !arch.equals("aarch64")) {
47-
throw new RuntimeException(
48-
"This provider is not supported on this processor architecture");
49-
}
5040
}
5141

5242
public String name() {

0 commit comments

Comments
 (0)