Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 89187 invoked from network); 22 Sep 2006 21:57:44 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 22 Sep 2006 21:57:44 -0000 Received: (qmail 4224 invoked by uid 500); 22 Sep 2006 21:57:42 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 4147 invoked by uid 500); 22 Sep 2006 21:57:42 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 4135 invoked by uid 99); 22 Sep 2006 21:57:41 -0000 Received: from idunn.apache.osuosl.org (HELO idunn.apache.osuosl.org) (140.211.166.84) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 22 Sep 2006 14:57:41 -0700 X-ASF-Spam-Status: No, hits=0.0 required=5.0 tests= Received: from [192.87.106.226] ([192.87.106.226:60877] helo=ajax.apache.org) by idunn.apache.osuosl.org (ecelerity 2.1.1.8 r(12930)) with ESMTP id F8/30-24206-45C54154 for ; Fri, 22 Sep 2006 14:57:41 -0700 Received: from ajax.apache.org (localhost [127.0.0.1]) by ajax.apache.org (Postfix) with ESMTP id 0EBD2D495B for ; Fri, 22 Sep 2006 22:57:38 +0100 (BST) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Apache Wiki To: commons-dev@jakarta.apache.org Date: Fri, 22 Sep 2006 21:57:38 -0000 Message-ID: <20060922215738.8969.33585@ajax.apache.org> Subject: [Jakarta-commons Wiki] Update of "Betwixt/TipsAndHints/HierarchalClassNormalizer" by JesseSweetland X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Dear Wiki user, You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification. The following page has been changed by JesseSweetland: http://wiki.apache.org/jakarta-commons/Betwixt/TipsAndHints/HierarchalClassNormalizer New page: This ClassNormalizer implementation will check the class that is passed into it and all of its super classes and interfaces to determine which class should actually be introspected. It looks for .betwixt mappings for each class and returns the first class that has a .betwixt mapping. This allows Betwixt to gracefully defer to super class and interface mappings. A RuntimeException is thrown if no mappings are found. This is intended to serve as a sanity check to make sure that everything has a mapping of some sort (either directly or on a super class or interface). The rationale is that if the default Betwixt behavior was being used then a customer ClassNormalizer probably isn't necessary or useful. (For example, how would you know which super class or interface to pick?) However, this behavior can be overridden with the second constructor form to return the same class that was passed in if no mappings are found. {{{ import java.util.*; import org.apache.commons.betwixt.strategy.*; import org.apache.commons.logging.*; /** * @author Jesse Sweetland */ public class HierarchalClassNormalizer extends ClassNormalizer { private Log _log = LogFactory.getLog(getClass()); private boolean _exceptionIfNoMapping = true; public HierarchalClassNormalizer() { this(true); } /** * @param exceptionIfNoMapping true to throw a * RuntimeException if no mappings are found; * false to return the class that is being * normalized */ public HierarchalClassNormalizer(boolean exceptionIfNoMapping) { _exceptionIfNoMapping = exceptionIfNoMapping; } private Set getSuperClassesAndInterfaces(Class c) { Set superClassesAndInterfaces = new LinkedHashSet(); Class temp = c; while(temp != null) { superClassesAndInterfaces.add(temp); for(Class ifc: temp.getInterfaces()) { superClassesAndInterfaces.add(ifc); } temp = temp.getSuperclass(); } return superClassesAndInterfaces; } protected String getDotBetwixtResourcePath(Class c) { return c.getName().substring(c.getName().lastIndexOf('.') + 1) + ".betwixt"; } public Class getNormalizedClass(Object o) { if(o == null) throw new NullPointerException(); Class c = o.getClass(); for(Class d: getSuperClassesAndInterfaces(c)) { // Check to see if there is a betwixt file for this class String dotBetwixtResourcePath = getDotBetwixtResourcePath(d); if(d.getResourceAsStream(dotBetwixtResourcePath) != null) { _log.debug("Class " + c.getName() + " => " + d.getName()); return d; } } if(_exceptionIfNoMapping) { throw new RuntimeException("No .betwixt mapping found for class " + c.getName() + " or any of its superclasses or interfaces"); } else { return c; } } public Class normalize(Class c) { for(Class d: getSuperClassesAndInterfaces(c)) { // Check to see if there is a betwixt file for this class String dotBetwixtResourcePath = getDotBetwixtResourcePath(d); if(d.getResourceAsStream(dotBetwixtResourcePath) != null) { _log.debug("Class " + c.getName() + " => " + d.getName()); return d; } } if(_exceptionIfNoMapping) { throw new RuntimeException("No .betwixt mapping found for class " + c.getName() + " or any of its superclasses or interfaces"); } else { return c; } } } }}} --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org