001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.annotations; 016 017 import java.util.ArrayList; 018 import java.util.Collections; 019 import java.util.List; 020 021 import org.apache.hivemind.ApplicationRuntimeException; 022 import org.apache.hivemind.Location; 023 import org.apache.tapestry.enhance.EnhancementOperation; 024 import org.apache.tapestry.spec.IComponentSpecification; 025 026 /** 027 * Recognizes the {@link org.apache.tapestry.annotations.Meta} annotation, and converts it into 028 * properties on the specification. It is desirable to have meta data in base classes be "merged" 029 * with meta data from sub classes, with the sub classes overriding any conflicting elements from 030 * the base class. What we do is work our way up the inheritance tree to java.lang.Object and work 031 * with each Meta annotation we find. 032 * 033 * @author Howard M. Lewis Ship 034 * @since 4.0 035 */ 036 public class MetaAnnotationWorker implements ClassAnnotationEnhancementWorker 037 { 038 039 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec, 040 Class baseClass, Location location) 041 { 042 List<Meta> metas = assembleMetas(baseClass); 043 044 for (Meta meta : metas) 045 applyPropertiesFromMeta(meta, spec, location); 046 } 047 048 private List<Meta> assembleMetas(Class baseClass) 049 { 050 Class searchClass = baseClass; 051 List<Meta> result = new ArrayList<Meta>(); 052 Meta lastMeta = null; 053 054 while (true) 055 { 056 Meta meta = (Meta) searchClass.getAnnotation(Meta.class); 057 058 // When reach a class that doesn't define or inherit a @Meta, we're done 059 if (meta == null) 060 break; 061 062 // Cheap approach, based on annotation inheritance, for determining 063 // whether a meta is already in the result list 064 065 if (meta != lastMeta) 066 result.add(meta); 067 068 searchClass = searchClass.getSuperclass(); 069 } 070 071 Collections.reverse(result); 072 073 return result; 074 } 075 076 private void applyPropertiesFromMeta(Meta meta, IComponentSpecification spec, Location location) 077 { 078 String[] pairs = meta.value(); 079 080 for (String pair : pairs) 081 { 082 int equalx = pair.indexOf('='); 083 084 // The only big problem is that the location will be the location of the @Meta in 085 // the subclass, even if the @Meta with a problem is from a base class. 086 087 if (equalx < 0) 088 throw new ApplicationRuntimeException(AnnotationMessages.missingEqualsInMeta(pair), 089 location, null); 090 091 String key = pair.substring(0, equalx); 092 String value = pair.substring(equalx + 1); 093 094 spec.setProperty(key, value); 095 } 096 } 097 }