com.squeakysand.commons.lang.ClassHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squeakysand-commons Show documentation
Show all versions of squeakysand-commons Show documentation
Classes, interfaces and enums that assist with everyday Java development tasks.
The newest version!
/*
* Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
*
* Licensed 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 com.squeakysand.commons.lang;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassHelper {
private static final Logger LOG = LoggerFactory.getLogger(ClassHelper.class);
public static Class> getCommonAncestorClass(Class> c1, Class> c2) {
LOG.trace("invoked with: {}, {}", c1, c2);
ClassMetaInfo c1metaInfo = new ClassMetaInfo(c1);
ClassMetaInfo c2metaInfo = new ClassMetaInfo(c2);
List> c1hierarchy = c1metaInfo.getClassHierarchy();
List> c2hierarchy = c2metaInfo.getClassHierarchy();
Class> result = null;
for (int i = 0; i < c1hierarchy.size() && i < c2hierarchy.size(); i++) {
Class> c1ancestor = c1hierarchy.get(i);
Class> c2ancestor = c2hierarchy.get(i);
if (c1ancestor.equals(c2ancestor)) {
result = c1ancestor;
} else {
break;
}
}
LOG.trace("result: {}", result);
return result;
}
public static Collection> getCommonInterfaces(Class> c1, Class> c2) {
LOG.trace("invoked with: {}, {}", c1, c2);
ClassMetaInfo c1metaInfo = new ClassMetaInfo(c1);
ClassMetaInfo c2metaInfo = new ClassMetaInfo(c2);
Set> c1interfaces = c1metaInfo.getImplementedInterfaces();
Set> c2interfaces = c2metaInfo.getImplementedInterfaces();
Collection> result = CollectionUtils.intersection(c1interfaces, c2interfaces);
LOG.trace("result: {}", ToStringHelper.toString(result));
return result;
}
public static boolean classesImplementCommonInterface(Class> commonInterface, Class>... classes) {
if (commonInterface == null) {
throw new IllegalArgumentException("i cannot be null");
}
if (!commonInterface.isInterface()) {
throw new IllegalArgumentException("i must be an interface type");
}
if (classes == null || classes.length == 0) {
throw new IllegalArgumentException("must pass at least 1 class to test against");
}
LOG.trace("invoked with: i = {}, classes = {}", commonInterface, ToStringHelper.toString(classes));
boolean result = true;
for (Class> klass : classes) {
if (!commonInterface.isAssignableFrom(klass)) {
result = false;
break;
}
}
LOG.trace("result: {}", result);
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy