tez-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ss...@apache.org
Subject git commit: TEZ-1298. Add parameterized constructor capabilities in ReflectionUtils. Contributed by Jonathan Eagles.
Date Mon, 21 Jul 2014 21:25:03 GMT
Repository: tez
Updated Branches:
  refs/heads/master d31937514 -> f11cff6f0


TEZ-1298. Add parameterized constructor capabilities in ReflectionUtils.
Contributed by Jonathan Eagles.


Project: http://git-wip-us.apache.org/repos/asf/tez/repo
Commit: http://git-wip-us.apache.org/repos/asf/tez/commit/f11cff6f
Tree: http://git-wip-us.apache.org/repos/asf/tez/tree/f11cff6f
Diff: http://git-wip-us.apache.org/repos/asf/tez/diff/f11cff6f

Branch: refs/heads/master
Commit: f11cff6f0d17c2507b1b4c992c1cb61afc8f23b4
Parents: d319375
Author: Siddharth Seth <sseth@apache.org>
Authored: Mon Jul 21 14:24:39 2014 -0700
Committer: Siddharth Seth <sseth@apache.org>
Committed: Mon Jul 21 14:24:39 2014 -0700

----------------------------------------------------------------------
 .../org/apache/tez/common/ReflectionUtils.java  | 141 +++++++++++++++++++
 .../apache/tez/common/TestReflectionUtils.java  |  89 ++++++++++++
 .../org/apache/tez/common/ReflectionUtils.java  | 115 ---------------
 .../apache/tez/runtime/TestReflectionUtils.java |  69 ---------
 4 files changed, 230 insertions(+), 184 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tez/blob/f11cff6f/tez-api/src/main/java/org/apache/tez/common/ReflectionUtils.java
----------------------------------------------------------------------
diff --git a/tez-api/src/main/java/org/apache/tez/common/ReflectionUtils.java b/tez-api/src/main/java/org/apache/tez/common/ReflectionUtils.java
new file mode 100644
index 0000000..0fc529b
--- /dev/null
+++ b/tez-api/src/main/java/org/apache/tez/common/ReflectionUtils.java
@@ -0,0 +1,141 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.tez.common;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.hadoop.classification.InterfaceAudience.Private;
+import org.apache.tez.dag.api.TezUncheckedException;
+
+@Private
+public class ReflectionUtils {
+
+  private static final Map<String, Class<?>> CLAZZ_CACHE = new ConcurrentHashMap<String,
Class<?>>();
+
+  @Private
+  public static Class<?> getClazz(String className) {
+    Class<?> clazz = CLAZZ_CACHE.get(className);
+    if (clazz == null) {
+      try {
+        clazz = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
+      } catch (ClassNotFoundException e) {
+        throw new TezUncheckedException("Unable to load class: " + className, e);
+      }
+    }
+    return clazz;
+  }
+
+  private static <T> T getNewInstance(Class<T> clazz) {
+    T instance;
+    try {
+      instance = clazz.newInstance();
+    } catch (InstantiationException e) {
+      throw new TezUncheckedException(
+          "Unable to instantiate class with 0 arguments: " + clazz.getName(), e);
+    } catch (IllegalAccessException e) {
+      throw new TezUncheckedException(
+          "Unable to instantiate class with 0 arguments: " + clazz.getName(), e);
+    }
+    return instance;
+  }
+
+  private static <T> T getNewInstance(Class<T> clazz, Class<?>[] parameterTypes,
Object[] parameters) {
+    T instance;
+    try {
+      Constructor<T> constructor = clazz.getConstructor(parameterTypes);
+      instance = constructor.newInstance(parameters);
+    } catch (InstantiationException e) {
+      throw new TezUncheckedException(
+          "Unable to instantiate class with " + parameters.length + " arguments: " + clazz.getName(),
e);
+    } catch (IllegalAccessException e) {
+      throw new TezUncheckedException(
+          "Unable to instantiate class with " + parameters.length + " arguments: " + clazz.getName(),
e);
+    } catch (NoSuchMethodException e) {
+      throw new TezUncheckedException(
+          "Unable to instantiate class with " + parameters.length + " arguments: " + clazz.getName(),
e);
+    } catch (InvocationTargetException e) {
+      throw new TezUncheckedException(
+          "Unable to instantiate class with " + parameters.length + " arguments: " + clazz.getName(),
e);
+    }
+    return instance;
+  }
+
+  @Private
+  public static <T> T createClazzInstance(String className) {
+    Class<?> clazz = getClazz(className);
+    @SuppressWarnings("unchecked")
+    T instance = (T) getNewInstance(clazz);
+    return instance;
+  }
+
+  @Private
+  public static <T> T createClazzInstance(String className, Class<?>[] parameterTypes,
Object[] parameters) {
+    Class<?> clazz = getClazz(className);
+    @SuppressWarnings("unchecked")
+    T instance = (T) getNewInstance(clazz, parameterTypes, parameters);
+    return instance;
+  }
+
+  @Private
+  public static synchronized void addResourcesToClasspath(List<URL> urls) {
+    ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread
+        .currentThread().getContextClassLoader());
+    Thread.currentThread().setContextClassLoader(classLoader);
+  }
+
+  // Parameters for addResourcesToSystemClassLoader
+  private static final Class<?>[] parameters = new Class[]{URL.class};
+  private static Method sysClassLoaderMethod = null;
+
+  @Private
+  public static synchronized void addResourcesToSystemClassLoader(List<URL> urls) {
+    URLClassLoader sysLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
+    if (sysClassLoaderMethod == null) {
+      Class<?> sysClass = URLClassLoader.class;
+      Method method;
+      try {
+        method = sysClass.getDeclaredMethod("addURL", parameters);
+      } catch (SecurityException e) {
+        throw new TezUncheckedException("Failed to get handle on method addURL", e);
+      } catch (NoSuchMethodException e) {
+        throw new TezUncheckedException("Failed to get handle on method addURL", e);
+      }
+      method.setAccessible(true);
+      sysClassLoaderMethod = method;
+    }
+    for (URL url : urls) {
+      try {
+        sysClassLoaderMethod.invoke(sysLoader, new Object[] { url });
+      } catch (IllegalArgumentException e) {
+        throw new TezUncheckedException("Failed to invoke addURL for rsrc: " + url, e);
+      } catch (IllegalAccessException e) {
+        throw new TezUncheckedException("Failed to invoke addURL for rsrc: " + url, e);
+      } catch (InvocationTargetException e) {
+        throw new TezUncheckedException("Failed to invoke addURL for rsrc: " + url, e);
+      }
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/tez/blob/f11cff6f/tez-api/src/test/java/org/apache/tez/common/TestReflectionUtils.java
----------------------------------------------------------------------
diff --git a/tez-api/src/test/java/org/apache/tez/common/TestReflectionUtils.java b/tez-api/src/test/java/org/apache/tez/common/TestReflectionUtils.java
new file mode 100644
index 0000000..3f7846d
--- /dev/null
+++ b/tez-api/src/test/java/org/apache/tez/common/TestReflectionUtils.java
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.tez.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collections;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.tez.dag.api.TezException;
+import org.junit.Test;
+
+public class TestReflectionUtils {
+
+  public static class ParameterizedConstructorClass {
+    final String first;
+    final int second;
+    public ParameterizedConstructorClass(String first, int second) {
+      this.first = first;
+      this.second = second;
+    }
+  }
+
+  @Test
+  public void testConstructorWithParameters()
+  {
+    Class<?>[] parameterTypes = new Class[] { String.class, Integer.TYPE };
+    Object[] parameters = new Object[] { new String("test"), 1 };
+    ParameterizedConstructorClass instance = ReflectionUtils.createClazzInstance(
+        ParameterizedConstructorClass.class.getName(), parameterTypes, parameters);
+    assertEquals("Class not constructed with first parameter correctly", instance.first,
"test");
+    assertEquals("Class not constructed with second parameter correctly", instance.second,
1);
+  }
+
+  @Test
+  public void testAddResourceToClasspath() throws IOException, TezException {
+
+    String rsrcName = "dummyfile.xml";
+    FileSystem localFs = FileSystem.getLocal(new Configuration());
+    Path p = new Path(rsrcName);
+    p = localFs.makeQualified(p);
+
+    localFs.delete(p, false);
+
+    try {
+      URL loadedUrl = null;
+
+      loadedUrl = Thread.currentThread().getContextClassLoader().getResource(rsrcName);
+      assertNull(loadedUrl);
+
+      // Add parent to classpath since we're not adding a jar
+      assertTrue(localFs.createNewFile(p));
+      String urlForm = p.toUri().toURL().toString();
+      urlForm = urlForm.substring(0, urlForm.lastIndexOf('/') + 1);
+      URL url = new URL(urlForm);
+
+      ReflectionUtils.addResourcesToClasspath(Collections.singletonList(url));
+
+      loadedUrl = Thread.currentThread().getContextClassLoader().getResource(rsrcName);
+
+      assertNotNull(loadedUrl);
+    } finally {
+      localFs.delete(p, false);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/tez/blob/f11cff6f/tez-common/src/main/java/org/apache/tez/common/ReflectionUtils.java
----------------------------------------------------------------------
diff --git a/tez-common/src/main/java/org/apache/tez/common/ReflectionUtils.java b/tez-common/src/main/java/org/apache/tez/common/ReflectionUtils.java
deleted file mode 100644
index 3b9ac81..0000000
--- a/tez-common/src/main/java/org/apache/tez/common/ReflectionUtils.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 org.apache.tez.common;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.apache.hadoop.classification.InterfaceAudience.Private;
-import org.apache.tez.dag.api.TezUncheckedException;
-
-@Private
-public class ReflectionUtils {
-  
-  private static final Map<String, Class<?>> CLAZZ_CACHE = new ConcurrentHashMap<String,
Class<?>>();
-
-  @Private
-  public static Class<?> getClazz(String className) {
-    Class<?> clazz = CLAZZ_CACHE.get(className);
-    if (clazz == null) {
-      try {
-        clazz = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
-      } catch (ClassNotFoundException e) {
-        throw new TezUncheckedException("Unable to load class: " + className, e);
-      }
-    }
-    return clazz;
-  }
-
-  private static <T> T getNewInstance(Class<T> clazz) {
-    T instance;
-    try {
-      instance = clazz.newInstance();
-    } catch (InstantiationException e) {
-      throw new TezUncheckedException(
-          "Unable to instantiate class with 0 arguments: " + clazz.getName(), e);
-    } catch (IllegalAccessException e) {
-      throw new TezUncheckedException(
-          "Unable to instantiate class with 0 arguments: " + clazz.getName(), e);
-    }
-    return instance;
-  }
-
-  @Private
-  public static <T> T createClazzInstance(String className) {
-    Class<?> clazz = getClazz(className);
-    @SuppressWarnings("unchecked")
-    T instance = (T) getNewInstance(clazz);
-    return instance;
-  }
-  
-  
-  @Private
-  public static synchronized void addResourcesToClasspath(List<URL> urls) {
-    ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread
-        .currentThread().getContextClassLoader());
-    Thread.currentThread().setContextClassLoader(classLoader);
-  }
-  
-  
-  // Parameters for addResourcesToSystemClassLoader
-  private static final Class<?>[] parameters = new Class[]{URL.class};
-  private static Method sysClassLoaderMethod = null;
-
-
-  @Private
-  public static synchronized void addResourcesToSystemClassLoader(List<URL> urls) {
-    URLClassLoader sysLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
-    if (sysClassLoaderMethod == null) {
-      Class<?> sysClass = URLClassLoader.class;
-      Method method;
-      try {
-        method = sysClass.getDeclaredMethod("addURL", parameters);
-      } catch (SecurityException e) {
-        throw new TezUncheckedException("Failed to get handle on method addURL", e);
-      } catch (NoSuchMethodException e) {
-        throw new TezUncheckedException("Failed to get handle on method addURL", e);
-      }
-      method.setAccessible(true);
-      sysClassLoaderMethod = method;
-    }
-    for (URL url : urls) {
-      try {
-        sysClassLoaderMethod.invoke(sysLoader, new Object[] { url });
-      } catch (IllegalArgumentException e) {
-        throw new TezUncheckedException("Failed to invoke addURL for rsrc: " + url, e);
-      } catch (IllegalAccessException e) {
-        throw new TezUncheckedException("Failed to invoke addURL for rsrc: " + url, e);
-      } catch (InvocationTargetException e) {
-        throw new TezUncheckedException("Failed to invoke addURL for rsrc: " + url, e);
-      }
-    }
-  }
-  
-}

http://git-wip-us.apache.org/repos/asf/tez/blob/f11cff6f/tez-runtime-internals/src/test/java/org/apache/tez/runtime/TestReflectionUtils.java
----------------------------------------------------------------------
diff --git a/tez-runtime-internals/src/test/java/org/apache/tez/runtime/TestReflectionUtils.java
b/tez-runtime-internals/src/test/java/org/apache/tez/runtime/TestReflectionUtils.java
deleted file mode 100644
index d10bd29..0000000
--- a/tez-runtime-internals/src/test/java/org/apache/tez/runtime/TestReflectionUtils.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 org.apache.tez.runtime;
-
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import java.io.IOException;
-import java.net.URL;
-import java.util.Collections;
-
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.fs.FileSystem;
-import org.apache.hadoop.fs.Path;
-import org.apache.tez.common.ReflectionUtils;
-import org.apache.tez.dag.api.TezException;
-import org.junit.Test;
-
-public class TestReflectionUtils {
-
-  @Test
-  public void testAddResourceToClasspath() throws IOException, TezException {
-
-    String rsrcName = "dummyfile.xml";
-    FileSystem localFs = FileSystem.getLocal(new Configuration());
-    Path p = new Path(rsrcName);
-    p = localFs.makeQualified(p);
-
-    localFs.delete(p, false);
-
-    try {
-      URL loadedUrl = null;
-
-      loadedUrl = Thread.currentThread().getContextClassLoader().getResource(rsrcName);
-      assertNull(loadedUrl);
-
-      // Add parent to classpath since we're not adding a jar
-      assertTrue(localFs.createNewFile(p));
-      String urlForm = p.toUri().toURL().toString();
-      urlForm = urlForm.substring(0, urlForm.lastIndexOf('/') + 1);
-      URL url = new URL(urlForm);
-
-      ReflectionUtils.addResourcesToClasspath(Collections.singletonList(url));
-
-      loadedUrl = Thread.currentThread().getContextClassLoader().getResource(rsrcName);
-
-      assertNotNull(loadedUrl);
-    } finally {
-      localFs.delete(p, false);
-    }
-  }
-}


Mime
View raw message