/*
 * Copyright 2016, Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.instrumentation.common;

import java.lang.reflect.InvocationTargetException;
import javax.annotation.Nullable;

/**
 * Instrumentation specific service provider mechanism.
 *
 * <pre>{@code
 * // Initialize a static variable using reflection.
 * static final Foo foo = Provider.newInstance("Foo", new NoopFoo());
 * }</pre>
 */
public final class Provider {
  /**
   * Returns a new instance of the class specified with {@code name} by invoking the empty-argument
   * constructor via reflections. If there are any errors, the {@code defaultValue} is returned.
   */
  @SuppressWarnings("unchecked")
  @Nullable
  public static <T> T newInstance(String name, @Nullable T defaultValue) {
    try {
      Class<?> provider = Class.forName(name);
      return (T) provider.getConstructor().newInstance();
    } catch (ClassNotFoundException e) {
      // No implementation available. Return the default value.
    } catch (IllegalAccessException e) {
      // No implementation available. Return the default value.
    } catch (InstantiationException e) {
      // No implementation available. Return the default value.
    } catch (InvocationTargetException e) {
      // No implementation available. Return the default value.
    } catch (NoSuchMethodException e) {
      // No implementation available. Return the default value.
    }
    return defaultValue;
  }
}

@@ -641,46 +641,59 @@
 ava.
-lang.reflect.InvocationTargetException
+util.logging.Level;%0Aimport java.util.logging.Logger
 ;%0Aim
@@ -971,16 +971,100 @@
 vider %7B%0A
+  private static final Logger logger = Logger.getLogger(Provider.class.getName());%0A%0A
   /**%0A  
@@ -1205,25 +1205,37 @@
  the
-re are any errors
+ specified class is not found
 , th
@@ -1259,16 +1259,21 @@
 alue%7D is
+%0A   *
  returne
@@ -1464,22 +1464,26 @@
 ;%0A      
-return
+T result =
  (T) pro
@@ -1528,108 +1528,62 @@
     
-%7D catch (ClassNotFoundException e) %7B%0A      // No implementation available. Return the default value.
+  logger.fine(%22Loaded: %22 + name);%0A      return result;
 %0A   
@@ -1596,21 +1596,21 @@
 ch (
-IllegalAccess
+ClassNotFound
 Exce
@@ -1630,58 +1630,59 @@
     
-// No implementation available. Return the
+logger.log(Level.FINE, %22Falling back to %22 +
  default
  val
@@ -1681,120 +1681,45 @@
 ault
- v
+V
 alue
-.%0A    %7D catch (InstantiationException e) %7B
+, e);
 %0A      
-// No implementation available. R
+r
 eturn 
-the 
 default
- v
+V
 alue
-.
+;
 %0A   
@@ -1732,24 +1732,8 @@
 ch (
-InvocationTarget
 Exce
@@ -1753,91 +1753,67 @@
     
-// No implementation available. Return the default value.%0A    %7D catch (NoSuchMethod
+if (e instanceof RuntimeException) %7B%0A        throw (Runtime
 Exce
@@ -1821,108 +1821,80 @@
 tion
+)
  e
-) %7B%0A      // No implementation available. Return the default value.%0A    %7D%0A    return defaultValue;
+;%0A      %7D else %7B%0A        throw new RuntimeException(e);%0A      %7D%0A    %7D
 %0A  %7D

Improve error handling in Provider. (#86)