sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1490042 - in /sis/branches/JDK7: core/sis-utility/src/main/java/org/apache/sis/util/logging/ core/sis-utility/src/test/java/org/apache/sis/util/logging/ storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ storage/sis-netcdf/sr...
Date Wed, 05 Jun 2013 22:00:59 GMT
Author: desruisseaux
Date: Wed Jun  5 22:00:58 2013
New Revision: 1490042

URL: http://svn.apache.org/r1490042
Log:
Infers the LogRecord class and method names from the stack trace.

Modified:
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/logging/WarningListenersTest.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java?rev=1490042&r1=1490041&r2=1490042&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java
[UTF-8] Wed Jun  5 22:00:58 2013
@@ -130,25 +130,51 @@ public class WarningListeners<S> impleme
      * Reports a warning represented by the given message and exception.
      * At least one of {@code message} and {@code exception} shall be non-null.
      *
-     * @param methodName The name of the method in which the warning occurred.
      * @param message    The message to log, or {@code null} if none.
      * @param exception  The exception to log, or {@code null} if none.
      */
-    public void warning(final String methodName, String message, final Exception exception)
{
+    public void warning(String message, final Exception exception) {
+        final StackTraceElement[] trace;
         if (exception != null) {
+            trace = exception.getStackTrace();
             message = Exceptions.formatChainedMessages(getLocale(), message, exception);
             if (message == null) {
                 message = exception.toString();
             }
+        } else {
+            trace = Thread.currentThread().getStackTrace();
         }
         ArgumentChecks.ensureNonEmpty("message", message);
         final LogRecord record = new LogRecord(Level.WARNING, message);
-        record.setSourceClassName(getClass().getCanonicalName());
-        record.setSourceMethodName(methodName);
+        for (int i=0; i<trace.length; i++) {
+            StackTraceElement e = trace[i];
+            if (isPublic(e)) {
+                record.setSourceClassName(e.getClassName());
+                record.setSourceMethodName(e.getMethodName());
+                break;
+            }
+        }
         warning(record);
     }
 
     /**
+     * Returns {@code true} if the given stack trace element describes a method considered
part of public API.
+     * This method is invoked in order to infer the class and method names to declare in
a {@link LogRecord}.
+     *
+     * <p>The current implementation compares the class name against a hard-coded list
of classes to hide.
+     * This implementation may change in any future SIS version.</p>
+     *
+     * @param  e A stack trace element.
+     * @return {@code true} if the class and method specified by the given element can be
considered public API.
+     */
+    private static boolean isPublic(final StackTraceElement e) {
+        final String classname  = e.getClassName();
+        return !classname.equals("org.apache.sis.util.logging.WarningListeners") &&
+               !classname.contains(".internal.") && !classname.startsWith("java")
&&
+                classname.indexOf('$') < 0 && e.getMethodName().indexOf('$') <
0;
+    }
+
+    /**
      * Adds a listener to be notified when a warning occurred.
      * When a warning occurs, there is a choice:
      *

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/logging/WarningListenersTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/logging/WarningListenersTest.java?rev=1490042&r1=1490041&r2=1490042&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/logging/WarningListenersTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/logging/WarningListenersTest.java
[UTF-8] Wed Jun  5 22:00:58 2013
@@ -93,13 +93,13 @@ public final strictfp class WarningListe
     }
 
     /**
-     * Tests {@link WarningListeners#warning(String, String, Exception)} with a registered
listener.
+     * Tests {@link WarningListeners#warning(String, Exception)} with a registered listener.
      */
     @Test
     @DependsOnMethod("testAddAndRemoveWarningListener")
     public void testWarning() {
         listeners.addWarningListener(this);
-        listeners.warning("testWarning", "The message", null);
+        listeners.warning("The message", null);
         listeners.removeWarningListener(this);
         assertNotNull("Listener has not been notified.", warning);
         assertEquals("testWarning", warning.getSourceMethodName());

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java?rev=1490042&r1=1490041&r2=1490042&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
[UTF-8] Wed Jun  5 22:00:58 2013
@@ -112,7 +112,7 @@ public abstract class Decoder implements
         try {
             return Double.valueOf(value);
         } catch (NumberFormatException e) {
-            listeners.warning("numericValue", null, e);
+            listeners.warning(null, e);
         }
         return null;
     }
@@ -142,7 +142,7 @@ public abstract class Decoder implements
         if (unit != null) try {
             return Units.valueOf(unit);
         } catch (IllegalArgumentException e) {
-            listeners.warning("unitValue", null, e);
+            listeners.warning(null, e);
         }
         return null;
     }

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java?rev=1490042&r1=1490041&r2=1490042&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
[UTF-8] Wed Jun  5 22:00:58 2013
@@ -620,7 +620,7 @@ public final class ChannelDecoder extend
             if (attribute.value instanceof String) try {
                 return JDK8.parseDateTime((String) attribute.value, DEFAULT_TIMEZONE_IS_UTC);
             } catch (IllegalArgumentException e) {
-                listeners.warning("dateValue", null, e);
+                listeners.warning(null, e);
             }
         }
         return null;
@@ -647,7 +647,7 @@ public final class ChannelDecoder extend
                 }
             }
         } catch (ConversionException | IllegalArgumentException e) {
-            listeners.warning("numberToDate", null, e);
+            listeners.warning(null, e);
         }
         return dates;
     }

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java?rev=1490042&r1=1490041&r2=1490042&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
[UTF-8] Wed Jun  5 22:00:58 2013
@@ -229,7 +229,7 @@ public final class DecoderWrapper extend
                         try {
                             date = CalendarDateFormatter.isoStringToCalendarDate(Calendar.proleptic_gregorian,
value);
                         } catch (IllegalArgumentException e) {
-                            listeners.warning("dateValue", null, e);
+                            listeners.warning(null, e);
                             continue;
                         }
                         return new Date(date.getMillis());
@@ -254,7 +254,7 @@ public final class DecoderWrapper extend
         try {
             unit = new DateUnit(symbol);
         } catch (Exception e) { // Declared by the DateUnit constructor.
-            listeners.warning("numberToDate", null, e);
+            listeners.warning(null, e);
             return dates;
         }
         for (int i=0; i<values.length; i++) {
@@ -341,7 +341,7 @@ public final class DecoderWrapper extend
      */
     @Override
     public void setError(final String message) {
-        listeners.warning(null, message, null);
+        listeners.warning(message, null);
     }
 
     /**

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java?rev=1490042&r1=1490041&r2=1490042&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
[UTF-8] Wed Jun  5 22:00:58 2013
@@ -270,7 +270,7 @@ final class MetadataReader {
             resource.setFunction(OnLineFunction.INFORMATION);
             return resource;
         } catch (URISyntaxException e) {
-            decoder.listeners.warning("createOnlineResource", null, e);
+            decoder.listeners.warning(null, e);
         }
         return null;
     }
@@ -675,7 +675,7 @@ final class MetadataReader {
             }
             extent.getTemporalElements().add(t);
         } catch (UnsupportedOperationException e) {
-            decoder.listeners.warning("createExtent", null, e);
+            decoder.listeners.warning(null, e);
         }
         /*
          * Add the geographic identifier, if present.
@@ -698,7 +698,7 @@ final class MetadataReader {
         if (source != null) try {
             return source.getConverterToAny(target);
         } catch (ConversionException e) {
-            decoder.listeners.warning("getConverterTo", null, e);
+            decoder.listeners.warning(null, e);
         }
         return null;
     }



Mime
View raw message