changeset 180:a487953b6d66

8129434: NPE at PeripheralDescriptorImpl Summary: add guard code Reviewed-by: alkonsta
author snazarki
date Mon, 22 Jun 2015 17:10:30 +0300
parents d56cb669f4f1
children 1e2cf9f41b8b
files src/share/classes/com/oracle/dio/impl/PeripheralDescriptorImpl.java
diffstat 1 files changed, 18 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/oracle/dio/impl/PeripheralDescriptorImpl.java	Mon Jun 22 17:07:36 2015 +0300
+++ b/src/share/classes/com/oracle/dio/impl/PeripheralDescriptorImpl.java	Mon Jun 22 17:10:30 2015 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 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
@@ -24,13 +24,15 @@
  */
 
 package com.oracle.dio.impl;
+import java.util.Objects;
 import java.util.Set;
 
+import com.oracle.dio.utils.Constants;
+import com.oracle.dio.utils.Logging;
+
 import jdk.dio.Device;
 import jdk.dio.DeviceConfig;
 import jdk.dio.DeviceDescriptor;
-import com.oracle.dio.utils.Constants;
-import com.oracle.dio.utils.Logging;
 
 import romizer.*;
 
@@ -42,7 +44,7 @@
  */
 @SerializeMe
 @DontRenameClass
-public class PeripheralDescriptorImpl<C extends DeviceConfig<T>, T extends Device<? super T>> implements DeviceDescriptor<T> {
+public final class PeripheralDescriptorImpl<C extends DeviceConfig<T>, T extends Device<? super T>> implements DeviceDescriptor<T> {
 
     private C config;
     private String clazz;
@@ -50,15 +52,18 @@
     private String name;
     private String[] props;
 
+    // need for serializator
     public PeripheralDescriptorImpl() {
     }
 
     public PeripheralDescriptorImpl(int id, String name, C config, Class<T> intf, String[] props) {
+        Objects.requireNonNull(config);
         this.config = config;
-        if (null == config) {
-            throw new NullPointerException();
+        // see DeviceManager.openWithConfig where intf may be null
+        // however register/open-with-name/open-with-id always provides correct intf
+        if (null != intf) {
+            this.clazz = intf.getName();
         }
-        this.clazz = intf.getName();
         this.id = id;
         this.name = name;
         this.props = (props == null) ? props : props.clone();
@@ -72,10 +77,12 @@
 
     @Override
     public Class<T> getInterface() {
-        try {
-            return (Class<T>)Class.forName(clazz);
-        } catch (ClassNotFoundException | RuntimeException e) {
-            Logging.reportError("Can't restore class at PeripheralDescriptorImpl");
+        if (null != clazz) {
+            try {
+                return (Class<T>)Class.forName(clazz);
+            } catch (ClassNotFoundException | RuntimeException e) {
+                Logging.reportError("Can't restore class at PeripheralDescriptorImpl");
+            }
         }
         return null;
     }