o Fixing a NullPointerException in the value extractor if the specified getter

didn't exists. The value extractor will now return null if the getter doesn't
  exists.
o Properly implemented the method caching in the value exctrator.
o Changed the RegexBasedModelInterpolator so it would properly handle null
  values. It used to convert null to "null" and then insert that, now it will
  leave the expression as is.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163696 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Trygve Laugstol 2005-03-28 14:37:41 +00:00
parent 39a96456cd
commit 16ea6ce332
4 changed files with 55 additions and 48 deletions

View File

@ -1,20 +1,19 @@
package org.apache.maven.project.interpolation;
/* ====================================================================
* Copyright 2001-2004 The Apache Software Foundation.
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* 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
* 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
* 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.
* ====================================================================
* 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.
*/
import org.apache.maven.model.Model;
@ -32,12 +31,12 @@ import java.util.regex.Pattern;
/**
* @author jdcasey Created on Feb 3, 2005
* @version $Id$
*/
public class RegexBasedModelInterpolator
extends AbstractLogEnabled
implements ModelInterpolator
{
private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom|project\\.)?([^}]+)\\}" );
/**
@ -89,10 +88,11 @@ public class RegexBasedModelInterpolator
String wholeExpr = matcher.group( 0 );
String realExpr = matcher.group( 2 );
String value = null;
Object value = null;
try
{
value = String.valueOf( ReflectionValueExtractor.evaluate( realExpr, model ) );
value = ReflectionValueExtractor.evaluate( realExpr, model );
}
catch ( Exception e )
{
@ -105,15 +105,14 @@ public class RegexBasedModelInterpolator
if ( value != null )
{
result = StringUtils.replace( result, wholeExpr, value );
result = StringUtils.replace( result, wholeExpr, String.valueOf( value ) );
// could use:
// result = matcher.replaceFirst( value );
// but this could result in multiple lookups of value, and replaceAll is not correct behaviour
// result = matcher.replaceFirst( stringValue );
// but this could result in multiple lookups of stringValue, and replaceAll is not correct behaviour
matcher.reset( result );
}
}
return result;
}
}

View File

@ -1,43 +1,41 @@
package org.apache.maven.util.introspection;
/* ====================================================================
* Copyright 2001-2004 The Apache Software Foundation.
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* 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
* 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
* 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.
* ====================================================================
* 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.
*/
import org.codehaus.plexus.util.StringUtils;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.codehaus.plexus.util.StringUtils;
/**
* Using simple dotted expressions extract the values from a MavenProject
* instance, For example we might want to extract a value like:
* project.build.sourceDirectory
*
*
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
* @version $Id: ReflectionValueExtractor.java,v 1.2 2005/03/01 07:05:33 brett
* Exp $
* @version $Id$
*/
public class ReflectionValueExtractor
{
private static Class[] args = new Class[0];
private static Class[] args = new Class[ 0 ];
private static Object[] params = new Object[0];
private static Object[] params = new Object[ 0 ];
private static ClassMap classMap;
@ -48,7 +46,8 @@ public class ReflectionValueExtractor
}
// TODO: don't throw Exception
public static Object evaluate( String expression, Object root ) throws Exception
public static Object evaluate( String expression, Object root )
throws Exception
{
// ----------------------------------------------------------------------
// Remove the leading "project" token
@ -75,6 +74,11 @@ public class ReflectionValueExtractor
Method method = classMap.findMethod( methodName, args );
if ( method == null )
{
return null;
}
value = method.invoke( value, params );
}
@ -88,8 +92,10 @@ public class ReflectionValueExtractor
if ( classMap == null )
{
classMap = new ClassMap( clazz );
classMaps.put( clazz, classMap );
}
return classMap;
}
}
}

View File

@ -16,17 +16,18 @@ package org.apache.maven.project.interpolation;
* limitations under the License.
*/
import junit.framework.TestCase;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import junit.framework.TestCase;
/**
* @author jdcasey
* @version $Id$
*/
public class RegexBasedModelInterpolatorTest
extends TestCase
{
public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion()
throws ModelInterpolationException
{
@ -75,5 +76,4 @@ public class RegexBasedModelInterpolatorTest
assertEquals( "foo-3.8.1", ( (Dependency) out.getDependencies().get( 0 ) ).getVersion() );
}
}
}

View File

@ -33,15 +33,11 @@ public class ReflectionValueExtractorTest
{
private MavenProject project;
private MavenProjectBuilder builder;
protected void setUp()
throws Exception
{
super.setUp();
builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
File f = getFileForClasspathResource( "pom.xml" );
project = getProject( f );
@ -88,4 +84,10 @@ public class ReflectionValueExtractorTest
assertNotNull( build );
}
public void testValueExtractorWithAInvalidExpression()
throws Exception
{
assertNull( ReflectionValueExtractor.evaluate( "project.foo", project ) );
}
}