hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject svn commit: r1496228 - in /hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common: ./ src/main/java/org/apache/hadoop/metrics2/lib/ src/main/java/org/apache/hadoop/util/ src/test/java/org/apache/hadoop/util/
Date Mon, 24 Jun 2013 21:43:01 GMT
Author: tucu
Date: Mon Jun 24 21:43:01 2013
New Revision: 1496228

URL: http://svn.apache.org/r1496228
Log:
HADOOP-9661. Allow metrics sources to be extended. (sandyr via tucu)

Modified:
    hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/CHANGES.txt
    hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java
    hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ReflectionUtils.java
    hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestReflectionUtils.java

Modified: hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/CHANGES.txt?rev=1496228&r1=1496227&r2=1496228&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/CHANGES.txt (original)
+++ hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/CHANGES.txt Mon Jun
24 21:43:01 2013
@@ -23,6 +23,8 @@ Release 2.2.0 - UNRELEASED
 
   IMPROVEMENTS
 
+    HADOOP-9661. Allow metrics sources to be extended. (sandyr via tucu)
+
   OPTIMIZATIONS
 
   BUG FIXES

Modified: hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java?rev=1496228&r1=1496227&r2=1496228&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java
(original)
+++ hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/lib/MetricsSourceBuilder.java
Mon Jun 24 21:43:01 2013
@@ -33,6 +33,7 @@ import org.apache.hadoop.metrics2.Metric
 import org.apache.hadoop.metrics2.MetricsSource;
 import org.apache.hadoop.metrics2.annotation.Metric;
 import org.apache.hadoop.metrics2.annotation.Metrics;
+import org.apache.hadoop.util.ReflectionUtils;
 
 /**
  * Helper class to build metrics source object from annotations
@@ -54,10 +55,10 @@ public class MetricsSourceBuilder {
     Class<?> cls = source.getClass();
     registry = initRegistry(source);
 
-    for (Field field : cls.getDeclaredFields()) {
+    for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(cls)) {
       add(source, field);
     }
-    for (Method method : cls.getDeclaredMethods()) {
+    for (Method method : ReflectionUtils.getDeclaredMethodsIncludingInherited(cls)) {
       add(source, method);
     }
   }
@@ -88,7 +89,7 @@ public class MetricsSourceBuilder {
     Class<?> cls = source.getClass();
     MetricsRegistry r = null;
     // Get the registry if it already exists.
-    for (Field field : cls.getDeclaredFields()) {
+    for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(cls)) {
       if (field.getType() != MetricsRegistry.class) continue;
       try {
         field.setAccessible(true);

Modified: hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ReflectionUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ReflectionUtils.java?rev=1496228&r1=1496227&r2=1496228&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ReflectionUtils.java
(original)
+++ hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ReflectionUtils.java
Mon Jun 24 21:43:01 2013
@@ -25,7 +25,10 @@ import java.lang.management.ManagementFa
 import java.lang.management.ThreadInfo;
 import java.lang.management.ThreadMXBean;
 import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -302,4 +305,36 @@ public class ReflectionUtils {
     buffer.moveData();
     dst.readFields(buffer.inBuffer);
   }
+  
+  /**
+   * Gets all the declared fields of a class including fields declared in
+   * superclasses.
+   */
+  public static List<Field> getDeclaredFieldsIncludingInherited(Class<?> clazz)
{
+    List<Field> fields = new ArrayList<Field>();
+    while (clazz != null) {
+      for (Field field : clazz.getDeclaredFields()) {
+        fields.add(field);
+      }
+      clazz = clazz.getSuperclass();
+    }
+    
+    return fields;
+  }
+  
+  /**
+   * Gets all the declared methods of a class including methods declared in
+   * superclasses.
+   */
+  public static List<Method> getDeclaredMethodsIncludingInherited(Class<?> clazz)
{
+    List<Method> methods = new ArrayList<Method>();
+    while (clazz != null) {
+      for (Method method : clazz.getDeclaredMethods()) {
+        methods.add(method);
+      }
+      clazz = clazz.getSuperclass();
+    }
+    
+    return methods;
+  }
 }

Modified: hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestReflectionUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestReflectionUtils.java?rev=1496228&r1=1496227&r2=1496228&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestReflectionUtils.java
(original)
+++ hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestReflectionUtils.java
Mon Jun 24 21:43:01 2013
@@ -18,9 +18,12 @@
 
 package org.apache.hadoop.util;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.HashMap;
+import java.util.List;
 
 import static org.junit.Assert.*;
 import org.junit.Before;
@@ -109,6 +112,51 @@ public class TestReflectionUtils {
     System.gc();
     assertTrue(cacheSize()+" too big", cacheSize()<iterations);
   }
+  
+  @Test
+  public void testGetDeclaredFieldsIncludingInherited() {
+    Parent child = new Parent() {
+      private int childField;
+      @SuppressWarnings("unused")
+      public int getChildField() { return childField; }
+    };
+    
+    List<Field> fields = ReflectionUtils.getDeclaredFieldsIncludingInherited(
+        child.getClass());
+    boolean containsParentField = false;
+    boolean containsChildField = false;
+    for (Field field : fields) {
+      if (field.getName().equals("parentField")) {
+        containsParentField = true;
+      } else if (field.getName().equals("childField")) {
+        containsChildField = true;
+      }
+    }
+    
+    List<Method> methods = ReflectionUtils.getDeclaredMethodsIncludingInherited(
+        child.getClass());
+    boolean containsParentMethod = false;
+    boolean containsChildMethod = false;
+    for (Method method : methods) {
+      if (method.getName().equals("getParentField")) {
+        containsParentMethod = true;
+      } else if (method.getName().equals("getChildField")) {
+        containsChildMethod = true;
+      }
+    }
+    
+    assertTrue("Missing parent field", containsParentField);
+    assertTrue("Missing child field", containsChildField);
+    assertTrue("Missing parent method", containsParentMethod);
+    assertTrue("Missing child method", containsChildMethod);
+  }
+  
+  // Used for testGetDeclaredFieldsIncludingInherited
+  private class Parent {
+    private int parentField;
+    @SuppressWarnings("unused")
+    public int getParentField() { return parentField; }
+  }
     
   private static class LoadedInChild {
   }



Mime
View raw message