changeset 13499:cc8fc46f258b

6744127: NullPointerException at com.sun.tools.jdi.EventRequestManagerImpl.request Reviewed-by: jbachorik, sspitsyn Contributed-by: harsha.wardhana.b@oracle.com
author jbachorik
date Mon, 04 Jan 2016 13:49:11 +0100
parents f3d572034fad
children 21e5acefd22a af335ad593d6
files src/jdk.jdi/share/classes/com/sun/tools/jdi/EventRequestManagerImpl.java
diffstat 1 files changed, 15 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jdi/share/classes/com/sun/tools/jdi/EventRequestManagerImpl.java	Mon Jan 04 13:45:39 2016 +0100
+++ b/src/jdk.jdi/share/classes/com/sun/tools/jdi/EventRequestManagerImpl.java	Mon Jan 04 13:49:11 2016 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2015, 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
@@ -43,7 +43,7 @@
 class EventRequestManagerImpl extends MirrorImpl
                                        implements EventRequestManager
 {
-    List<? extends EventRequest>[] requestLists;
+    private final List<? extends EventRequest>[] requestLists;
     private static int methodExitEventCmd = 0;
 
     static int JDWPtoJDISuspendPolicy(byte jdwpPolicy) {
@@ -83,7 +83,7 @@
         return System.identityHashCode(this);
     }
 
-    abstract class EventRequestImpl extends MirrorImpl implements EventRequest {
+    private abstract class EventRequestImpl extends MirrorImpl implements EventRequest {
         int id;
 
         /*
@@ -734,7 +734,7 @@
         }
         requestLists = new List[highest+1];
         for (int i=0; i <= highest; i++) {
-            requestLists[i] = new ArrayList<>();
+            requestLists[i] = Collections.synchronizedList(new ArrayList<>());
         }
     }
 
@@ -933,22 +933,27 @@
     }
 
     List<? extends EventRequest> unmodifiableRequestList(int eventCmd) {
-        return Collections.unmodifiableList(requestList(eventCmd));
+        // No need of explicit synchronization for requestList here.
+        // It is taken care internally by SynchronizedList class.
+        return Collections.unmodifiableList(new ArrayList<>(requestList(eventCmd)));
     }
 
     EventRequest request(int eventCmd, int requestId) {
         List<? extends EventRequest> rl = requestList(eventCmd);
-        for (int i = rl.size() - 1; i >= 0; i--) {
-            EventRequestImpl er = (EventRequestImpl)rl.get(i);
-            if (er.id == requestId) {
-                return er;
+        synchronized(rl) {   // Refer Collections.synchronizedList javadoc.
+            Iterator<? extends EventRequest> itr = rl.iterator();
+            while (itr.hasNext()){
+                EventRequestImpl er = (EventRequestImpl)itr.next();
+                if (er.id == requestId)
+                    return er;
             }
         }
         return null;
     }
 
-    List<? extends EventRequest>  requestList(int eventCmd) {
+    private List<? extends EventRequest>  requestList(int eventCmd) {
         return requestLists[eventCmd];
     }
 
 }
+