1
1
/*
2
- * Copyright (c) 2003, 2018 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2003, 2020 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
27
27
28
28
import java .lang .instrument .UnmodifiableModuleException ;
29
29
import java .lang .reflect .Method ;
30
+ import java .lang .reflect .Modifier ;
30
31
import java .lang .reflect .AccessibleObject ;
31
32
import java .lang .instrument .ClassFileTransformer ;
32
33
import java .lang .instrument .ClassDefinition ;
@@ -441,12 +442,6 @@ public Object run() {
441
442
//
442
443
// 1) declared with a signature of (String, Instrumentation)
443
444
// 2) declared with a signature of (String)
444
- // 3) inherited with a signature of (String, Instrumentation)
445
- // 4) inherited with a signature of (String)
446
- //
447
- // So the declared version of either 1-arg or 2-arg always takes
448
- // primary precedence over an inherited version. After that, the
449
- // 2-arg version takes precedence over the 1-arg version.
450
445
//
451
446
// If no method is found then we throw the NoSuchMethodException
452
447
// from the first attempt so that the exception text indicates
@@ -470,45 +465,25 @@ public Object run() {
470
465
try {
471
466
m = javaAgentClass .getDeclaredMethod (methodname ,
472
467
new Class <?>[] { String .class });
473
- } catch (NoSuchMethodException x ) {
474
- // ignore this exception because we'll try
475
- // two arg inheritance next
476
- }
477
- }
478
-
479
- if (m == null ) {
480
- // now try the inherited 2-arg method
481
- try {
482
- m = javaAgentClass .getMethod ( methodname ,
483
- new Class <?>[] {
484
- String .class ,
485
- java .lang .instrument .Instrumentation .class
486
- }
487
- );
488
- twoArgAgent = true ;
489
- } catch (NoSuchMethodException x ) {
490
- // ignore this exception because we'll try
491
- // one arg inheritance next
492
- }
493
- }
494
-
495
- if (m == null ) {
496
- // finally try the inherited 1-arg method
497
- try {
498
- m = javaAgentClass .getMethod (methodname ,
499
- new Class <?>[] { String .class });
500
468
} catch (NoSuchMethodException x ) {
501
469
// none of the methods exists so we throw the
502
470
// first NoSuchMethodException as per 5.0
503
471
throw firstExc ;
504
472
}
505
473
}
506
474
507
- // the premain method should not be required to be public,
508
- // make it accessible so we can call it
509
- // Note: The spec says the following:
510
- // The agent class must implement a public static premain method...
511
- setAccessible (m , true );
475
+ // reject non-public premain or agentmain method
476
+ if (!Modifier .isPublic (m .getModifiers ())) {
477
+ String msg = "method " + classname + "." + methodname + " must be declared public" ;
478
+ throw new IllegalAccessException (msg );
479
+ }
480
+
481
+ if (!Modifier .isPublic (javaAgentClass .getModifiers ()) &&
482
+ !javaAgentClass .getModule ().isNamed ()) {
483
+ // If the java agent class is in an unnamed module, the java agent class can be non-public.
484
+ // Suppress access check upon the invocation of the premain/agentmain method.
485
+ setAccessible (m , true );
486
+ }
512
487
513
488
// invoke the 1 or 2-arg method
514
489
if (twoArgAgent ) {
0 commit comments