Skip to content

Commit aa12c8f

Browse files
committed
6642881: Improve performance of Class.getClassLoader()
Add classLoader to java/lang/Class instance for fast access Reviewed-by: alanb, lfoltan, rriggs, vlivanov, twisti, mchung, jfranck, dholmes
1 parent 6c8c561 commit aa12c8f

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

jdk/src/share/classes/java/lang/Class.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2014, 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
@@ -130,11 +130,15 @@ public final class Class<T> implements java.io.Serializable,
130130
}
131131

132132
/*
133-
* Constructor. Only the Java Virtual Machine creates Class
134-
* objects.
133+
* Private constructor. Only the Java Virtual Machine creates Class objects.
134+
* This constructor is not used and prevents the default constructor being
135+
* generated.
135136
*/
136-
private Class() {}
137-
137+
private Class(ClassLoader loader) {
138+
// Initialize final field for classLoader. The initialization value of non-null
139+
// prevents future JIT optimizations from assuming this final field is null.
140+
classLoader = loader;
141+
}
138142

139143
/**
140144
* Converts the object to a string. The string representation is the
@@ -677,8 +681,10 @@ public ClassLoader getClassLoader() {
677681
}
678682

679683
// Package-private to allow ClassLoader access
680-
native ClassLoader getClassLoader0();
684+
ClassLoader getClassLoader0() { return classLoader; }
681685

686+
// Initialized in JVM not by private constructor
687+
private final ClassLoader classLoader;
682688

683689
/**
684690
* Returns an array of {@code TypeVariable} objects that represent the

jdk/src/share/classes/java/lang/reflect/AccessibleObject.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2014, 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
@@ -129,16 +129,24 @@ public void setAccessible(boolean flag) throws SecurityException {
129129
setAccessible0(this, flag);
130130
}
131131

132-
/* Check that you aren't exposing java.lang.Class.<init>. */
132+
/* Check that you aren't exposing java.lang.Class.<init> or sensitive
133+
fields in java.lang.Class. */
133134
private static void setAccessible0(AccessibleObject obj, boolean flag)
134135
throws SecurityException
135136
{
136137
if (obj instanceof Constructor && flag == true) {
137138
Constructor<?> c = (Constructor<?>)obj;
138139
if (c.getDeclaringClass() == Class.class) {
139-
throw new SecurityException("Can not make a java.lang.Class" +
140+
throw new SecurityException("Cannot make a java.lang.Class" +
140141
" constructor accessible");
141142
}
143+
} else if (obj instanceof Field && flag == true) {
144+
Field f = (Field)obj;
145+
if (f.getDeclaringClass() == Class.class &&
146+
f.getName().equals("classLoader")) {
147+
throw new SecurityException("Cannot make java.lang.Class.classLoader" +
148+
" accessible");
149+
}
142150
}
143151
obj.override = flag;
144152
}

jdk/src/share/javavm/export/jvm.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2014, 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
@@ -426,9 +426,6 @@ JVM_GetClassName(JNIEnv *env, jclass cls);
426426
JNIEXPORT jobjectArray JNICALL
427427
JVM_GetClassInterfaces(JNIEnv *env, jclass cls);
428428

429-
JNIEXPORT jobject JNICALL
430-
JVM_GetClassLoader(JNIEnv *env, jclass cls);
431-
432429
JNIEXPORT jboolean JNICALL
433430
JVM_IsInterface(JNIEnv *env, jclass cls);
434431

jdk/src/share/native/common/check_code.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2014, 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
@@ -1357,16 +1357,9 @@ verify_opcode_operands(context_type *context, unsigned int inumber, int offset)
13571357
}
13581358
(*env)->DeleteLocalRef(env, super);
13591359

1360-
/* The optimizer make cause this to happen on local code */
1360+
/* The optimizer may cause this to happen on local code */
13611361
if (not_found) {
1362-
#ifdef BROKEN_JAVAC
1363-
jobject loader = JVM_GetClassLoader(env, context->class);
1364-
int has_loader = (loader != 0);
1365-
(*env)->DeleteLocalRef(env, loader);
1366-
if (has_loader)
1367-
#endif /* BROKEN_JAVAC */
1368-
CCerror(context,
1369-
"Illegal use of nonvirtual function call");
1362+
CCerror(context, "Illegal use of nonvirtual function call");
13701363
}
13711364
}
13721365
}

jdk/src/share/native/java/lang/Class.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2014, 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
@@ -45,7 +45,6 @@ extern jboolean VerifyFixClassname(char *utf_name);
4545
#define CLS "Ljava/lang/Class;"
4646
#define CPL "Lsun/reflect/ConstantPool;"
4747
#define STR "Ljava/lang/String;"
48-
#define JCL "Ljava/lang/ClassLoader;"
4948
#define FLD "Ljava/lang/reflect/Field;"
5049
#define MHD "Ljava/lang/reflect/Method;"
5150
#define CTR "Ljava/lang/reflect/Constructor;"
@@ -56,7 +55,6 @@ static JNINativeMethod methods[] = {
5655
{"getName0", "()" STR, (void *)&JVM_GetClassName},
5756
{"getSuperclass", "()" CLS, NULL},
5857
{"getInterfaces0", "()[" CLS, (void *)&JVM_GetClassInterfaces},
59-
{"getClassLoader0", "()" JCL, (void *)&JVM_GetClassLoader},
6058
{"isInterface", "()Z", (void *)&JVM_IsInterface},
6159
{"getSigners", "()[" OBJ, (void *)&JVM_GetClassSigners},
6260
{"setSigners", "([" OBJ ")V", (void *)&JVM_SetClassSigners},
@@ -81,7 +79,6 @@ static JNINativeMethod methods[] = {
8179
#undef OBJ
8280
#undef CLS
8381
#undef STR
84-
#undef JCL
8582
#undef FLD
8683
#undef MHD
8784
#undef CTR

0 commit comments

Comments
 (0)