Return-Path: Delivered-To: apmail-felix-commits-archive@www.apache.org Received: (qmail 61116 invoked from network); 4 Feb 2008 10:59:03 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Feb 2008 10:59:03 -0000 Received: (qmail 12355 invoked by uid 500); 4 Feb 2008 10:58:50 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 12335 invoked by uid 500); 4 Feb 2008 10:58:50 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 12321 invoked by uid 99); 4 Feb 2008 10:58:50 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Feb 2008 02:58:50 -0800 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Feb 2008 10:58:30 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 9FC1D1A9832; Mon, 4 Feb 2008 02:58:37 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r618233 - in /felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin: PropertyHandler.java SCRDescriptorMojo.java tags/ClassUtil.java tags/JavaField.java tags/cl/ClassLoaderJavaField.java tags/qdox/QDoxJavaField.java Date: Mon, 04 Feb 2008 10:58:35 -0000 To: commits@felix.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080204105837.9FC1D1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cziegeler Date: Mon Feb 4 02:58:33 2008 New Revision: 618233 URL: http://svn.apache.org/viewvc?rev=618233&view=rev Log: Initial support for property value definition through arrays. Added: felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/ClassUtil.java (with props) Modified: felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/PropertyHandler.java felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaField.java felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/cl/ClassLoaderJavaField.java felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/qdox/QDoxJavaField.java Modified: felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/PropertyHandler.java URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/PropertyHandler.java?rev=618233&r1=618232&r2=618233&view=diff ============================================================================== --- felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/PropertyHandler.java (original) +++ felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/PropertyHandler.java Mon Feb 4 02:58:33 2008 @@ -24,7 +24,8 @@ import org.apache.felix.scrplugin.om.Property; import org.apache.felix.scrplugin.om.metatype.AttributeDefinition; import org.apache.felix.scrplugin.om.metatype.OCD; -import org.apache.felix.scrplugin.tags.*; +import org.apache.felix.scrplugin.tags.JavaField; +import org.apache.felix.scrplugin.tags.JavaTag; import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.plexus.util.StringUtils; import org.osgi.service.cm.ConfigurationAdmin; @@ -40,7 +41,7 @@ * @param component * @param ocd */ - public void doProperty(JavaTag property, String name, Component component, OCD ocd) + public void doProperty(JavaTag property, String name, Component component, OCD ocd, JavaField javaField) throws MojoExecutionException { final Property prop = new Property(property); prop.setName(name); @@ -53,7 +54,7 @@ // now we check for a value ref attribute final String valueRef = property.getNamedParameter(Constants.PROPERTY_VALUE_REF); if ( valueRef != null ) { - prop.setValue(this.getPropertyValueRef(property.getJavaClassDescription(), prop, valueRef)); + this.setPropertyValueRef(property, prop, valueRef); } else { // check for multivalue - these can either be values or value refs final List values = new ArrayList(); @@ -64,11 +65,22 @@ if (key.startsWith(Constants.PROPERTY_MULTIVALUE_PREFIX) ) { values.add(entry.getValue()); } else if ( key.startsWith(Constants.PROPERTY_MULTIVALUE_REF_PREFIX) ) { - values.add(this.getPropertyValueRef(property.getJavaClassDescription(), prop, (String)entry.getValue())); + final String[] stringValues = this.getPropertyValueRef(property, prop, (String)entry.getValue()); + if ( stringValues != null ) { + for(int i=0; i 0 ) { prop.setMultiValue((String[])values.toArray(new String[values.size()])); + } else { + // we have no value, valueRef or values so let's try to + // get the value of the field if a name attribute is specified + if ( property.getNamedParameter(Constants.PROPERTY_NAME) != null && javaField != null ) { + this.setPropertyValueRef(property, prop, javaField.getName()); + } } } } @@ -159,14 +171,25 @@ return defaultName; } - public String getPropertyValueRef(JavaClassDescription desc, Property prop, String valueRef) + public void setPropertyValueRef(final JavaTag tag, Property property, String valueRef) + throws MojoExecutionException { + final String[] values = this.getPropertyValueRef(tag, property, valueRef); + if ( values != null && values.length == 1 ) { + property.setValue(values[0]); + } else if ( values != null && values.length > 1 ) { + property.setMultiValue(values); + } + } + + public String[] getPropertyValueRef(final JavaTag tag, Property prop, String valueRef) throws MojoExecutionException { int classSep = valueRef.lastIndexOf('.'); if ( classSep == -1 ) { // local variable - final JavaField field = desc.getFieldByName(valueRef); + // TODO - This could be a static import! + final JavaField field = tag.getJavaClassDescription().getFieldByName(valueRef); if ( field == null ) { - throw new MojoExecutionException("Property references unknown field " + valueRef + " in class " + desc.getName()); + throw new MojoExecutionException("Property references unknown field " + valueRef + " in class " + tag.getJavaClassDescription().getName()); } // determine type (if not set explicitly) if ( prop.getType() == null ) { @@ -197,7 +220,7 @@ throw new MojoExecutionException("Referencing values from foreign classes not supported yet."); } - public void testProperty(Map properties, JavaTag property, String defaultName, boolean isInspectedClass) + public void testProperty(Map properties, JavaTag property, String defaultName, JavaField field, boolean isInspectedClass) throws MojoExecutionException { final String propName = this.getPropertyName(property, defaultName); @@ -209,7 +232,7 @@ throw new MojoExecutionException("Duplicate definition for property " + propName + " in class " + property.getJavaClassDescription().getName()); } } else { - properties.put(propName, property); + properties.put(propName, new Object[] {property, field}); } } } Modified: felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java?rev=618233&r1=618232&r2=618233&view=diff ============================================================================== --- felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java (original) +++ felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/SCRDescriptorMojo.java Mon Feb 4 02:58:33 2008 @@ -233,7 +233,7 @@ // properties final JavaTag[] props = currentDescription.getTagsByName(Constants.PROPERTY, false); for (int i=0; i < props.length; i++) { - this.propertyHandler.testProperty(properties, props[i], null, description == currentDescription); + this.propertyHandler.testProperty(properties, props[i], null, null, description == currentDescription); } // references @@ -254,9 +254,12 @@ if (tag != null) { String defaultName = null; if ( "java.lang.String".equals(fields[i].getType()) ) { - defaultName = fields[i].getInitializationExpression(); + final String[] initValues = fields[i].getInitializationExpression(); + if ( initValues != null && initValues.length == 1 ) { + defaultName = initValues[0]; + } } - this.propertyHandler.testProperty(properties, tag, defaultName, description == currentDescription); + this.propertyHandler.testProperty(properties, tag, defaultName, fields[i], description == currentDescription); } } @@ -268,8 +271,9 @@ while ( propIter.hasNext() ) { final Map.Entry entry = (Map.Entry)propIter.next(); final String propName = entry.getKey().toString(); - final JavaTag tag = (JavaTag)entry.getValue(); - this.propertyHandler.doProperty(tag, propName, component, ocd); + final Object[] values = (Object[])entry.getValue(); + final JavaTag tag = (JavaTag)values[0]; + this.propertyHandler.doProperty(tag, propName, component, ocd, (JavaField)values[1]); } // process references Added: felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/ClassUtil.java URL: http://svn.apache.org/viewvc/felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/ClassUtil.java?rev=618233&view=auto ============================================================================== --- felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/ClassUtil.java (added) +++ felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/ClassUtil.java Mon Feb 4 02:58:33 2008 @@ -0,0 +1,58 @@ +/* + * 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.felix.scrplugin.tags; + +import java.lang.reflect.Array; +import java.lang.reflect.Field; + +/** + * Utility class. + */ +public class ClassUtil { + + /** + * Try to get the initial value of a static field + * @param clazz The class. + * @param fieldName The name of the field. + * @return The initial value or null. + */ + public static String[] getInitializationExpression(Class clazz, String fieldName) { + try { + final Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + final Object value = field.get(null); + if ( value != null ) { + if ( value.getClass().isArray() ) { + final String[] values = new String[Array.getLength(value)]; + for(int i=0; i