LANG-863 Method returns number of inheritance hops between parent and subclass
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1545145 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7c919293f7
commit
cfb488a05f
|
@ -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>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
}
|
|
@ -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 {
|
||||
}
|
|
@ -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 {
|
||||
}
|
Loading…
Reference in New Issue