You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2013/11/25 04:38:12 UTC
svn commit: r1545145 - in /commons/proper/lang/trunk/src: changes/
main/java/org/apache/commons/lang3/reflect/
test/java/org/apache/commons/lang3/reflect/
test/java/org/apache/commons/lang3/reflect/testbed/
Author: sebb
Date: Mon Nov 25 03:38:11 2013
New Revision: 1545145
URL: http://svn.apache.org/r1545145
Log:
LANG-863 Method returns number of inheritance hops between parent and subclass
Added:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java (with props)
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/InheritanceUtilsTest.java (with props)
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherChild.java (with props)
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherParent.java (with props)
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/Grandchild.java (with props)
Modified:
commons/proper/lang/trunk/src/changes/changes.xml
Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1545145&r1=1545144&r2=1545145&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Mon Nov 25 03:38:11 2013
@@ -22,6 +22,7 @@
<body>
<release version="3.2" date="TBA" description="Next release">
+ <action issue="LANG-863" type="add" due-to="Daneel S. Yaitskov" dev="sebb">Method returns number of inheritance hops between parent and subclass</action>
<action issue="LANG-932" type="fix" due-to="Ville Skyttä" dev="sebb">Spelling fixes</action>
<action issue="LANG-931" type="update" dev="britter" due-to="Christoph Schneegans">Misleading Javadoc comment in StrBuilderReader class</action>
<action issue="LANG-929" type="fix">OctalUnescaper tried to parse all of \279</action>
Added: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java?rev=1545145&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java (added)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java Mon Nov 25 03:38:11 2013
@@ -0,0 +1,60 @@
+/*
+ * 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.commons.lang3.reflect;
+
+import org.apache.commons.lang3.BooleanUtils;
+
+/**
+ * <p>Utility methods focusing on inheritance.</p>
+ *
+ * @since 3.2
+ */
+public class InheritanceUtils {
+
+ public InheritanceUtils() {
+ super();
+ }
+
+ /**
+ * <p>Returns the number of inheritance hops between two classes.</p>
+ *
+ * @param child the child class, may be {@code null}
+ * @param parent the parent class, may be {@code null}
+ * @return the number of generations between the child and parent; 0 if the same class;
+ * -1 if the classes are not related as child and parent (includes where either class is null)
+ * @since 3.2
+ */
+ public static int distance(final Class<?> child, final Class<?> parent) {
+ if (child == null || parent == null) {
+ return -1;
+ }
+
+ if (child.equals(parent)) {
+ return 0;
+ }
+
+ final Class<?> cParent = child.getSuperclass();
+ int d = BooleanUtils.toInteger(parent.equals(cParent));
+
+ if (d == 1) {
+ return d;
+ } else {
+ d += distance(cParent, parent);
+ return d > 0 ? d + 1 : -1;
+ }
+ }
+}
Propchange: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/InheritanceUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/InheritanceUtilsTest.java?rev=1545145&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/InheritanceUtilsTest.java (added)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/InheritanceUtilsTest.java Mon Nov 25 03:38:11 2013
@@ -0,0 +1,79 @@
+/*
+ * 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.commons.lang3.reflect;
+
+import org.apache.commons.lang3.reflect.testbed.AnotherChild;
+import org.apache.commons.lang3.reflect.testbed.AnotherParent;
+import org.apache.commons.lang3.reflect.testbed.Grandchild;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Unit tests InheritanceUtils
+ */
+public class InheritanceUtilsTest {
+
+ @Test
+ public void testConstructor() throws Exception {
+ assertNotNull(InheritanceUtils.class.newInstance());
+ }
+
+ @Test
+ public void testDistanceGreaterThanZero() {
+ assertEquals(1, InheritanceUtils.distance(AnotherChild.class, AnotherParent.class));
+ assertEquals(1, InheritanceUtils.distance(Grandchild.class, AnotherChild.class));
+ assertEquals(2, InheritanceUtils.distance(Grandchild.class, AnotherParent.class));
+ assertEquals(3, InheritanceUtils.distance(Grandchild.class, Object.class));
+ }
+
+ @Test
+ public void testDistanceEqual() {
+ assertEquals(0, InheritanceUtils.distance(AnotherChild.class, AnotherChild.class));
+ }
+
+ @Test
+ public void testDistanceEqualObject() {
+ assertEquals(0, InheritanceUtils.distance(Object.class, Object.class));
+ }
+
+ @Test
+ public void testDistanceNullChild() {
+ assertEquals(-1, InheritanceUtils.distance(null, Object.class));
+ }
+
+ @Test
+ public void testDistanceNullParent() {
+ assertEquals(-1, InheritanceUtils.distance(Object.class, null));
+ }
+
+ @Test
+ public void testDistanceNullParentNullChild() {
+ assertEquals(-1, InheritanceUtils.distance(null, null));
+ }
+
+ @Test
+ public void testDistanceDisjoint() {
+ assertEquals(-1, InheritanceUtils.distance(Boolean.class, String.class));
+ }
+
+ @Test
+ public void testDistanceReverseParentChild() {
+ assertEquals(-1, InheritanceUtils.distance(Object.class, Grandchild.class));
+ }
+}
Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/InheritanceUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/InheritanceUtilsTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherChild.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherChild.java?rev=1545145&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherChild.java (added)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherChild.java Mon Nov 25 03:38:11 2013
@@ -0,0 +1,23 @@
+/*
+ * 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.commons.lang3.reflect.testbed;
+
+/**
+ *
+ */
+public class AnotherChild extends AnotherParent {
+}
Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherChild.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherChild.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherParent.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherParent.java?rev=1545145&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherParent.java (added)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherParent.java Mon Nov 25 03:38:11 2013
@@ -0,0 +1,23 @@
+/*
+ * 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.commons.lang3.reflect.testbed;
+
+/**
+ *
+ */
+public class AnotherParent {
+}
Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherParent.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/AnotherParent.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/Grandchild.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/Grandchild.java?rev=1545145&view=auto
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/Grandchild.java (added)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/Grandchild.java Mon Nov 25 03:38:11 2013
@@ -0,0 +1,23 @@
+/*
+ * 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.commons.lang3.reflect.testbed;
+
+/**
+ *
+ */
+public class Grandchild extends AnotherChild {
+}
Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/Grandchild.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/testbed/Grandchild.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision