mirror of https://github.com/apache/maven.git
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:
parent
39a96456cd
commit
16ea6ce332
|
@ -1,20 +1,19 @@
|
||||||
package org.apache.maven.project.interpolation;
|
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* 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
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
* ====================================================================
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.apache.maven.model.Model;
|
import org.apache.maven.model.Model;
|
||||||
|
@ -32,12 +31,12 @@ import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author jdcasey Created on Feb 3, 2005
|
* @author jdcasey Created on Feb 3, 2005
|
||||||
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class RegexBasedModelInterpolator
|
public class RegexBasedModelInterpolator
|
||||||
extends AbstractLogEnabled
|
extends AbstractLogEnabled
|
||||||
implements ModelInterpolator
|
implements ModelInterpolator
|
||||||
{
|
{
|
||||||
|
|
||||||
private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom|project\\.)?([^}]+)\\}" );
|
private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom|project\\.)?([^}]+)\\}" );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,10 +88,11 @@ public class RegexBasedModelInterpolator
|
||||||
String wholeExpr = matcher.group( 0 );
|
String wholeExpr = matcher.group( 0 );
|
||||||
String realExpr = matcher.group( 2 );
|
String realExpr = matcher.group( 2 );
|
||||||
|
|
||||||
String value = null;
|
Object value = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
value = String.valueOf( ReflectionValueExtractor.evaluate( realExpr, model ) );
|
value = ReflectionValueExtractor.evaluate( realExpr, model );
|
||||||
}
|
}
|
||||||
catch ( Exception e )
|
catch ( Exception e )
|
||||||
{
|
{
|
||||||
|
@ -105,15 +105,14 @@ public class RegexBasedModelInterpolator
|
||||||
|
|
||||||
if ( value != null )
|
if ( value != null )
|
||||||
{
|
{
|
||||||
result = StringUtils.replace( result, wholeExpr, value );
|
result = StringUtils.replace( result, wholeExpr, String.valueOf( value ) );
|
||||||
// could use:
|
// could use:
|
||||||
// result = matcher.replaceFirst( value );
|
// result = matcher.replaceFirst( stringValue );
|
||||||
// but this could result in multiple lookups of value, and replaceAll is not correct behaviour
|
// but this could result in multiple lookups of stringValue, and replaceAll is not correct behaviour
|
||||||
matcher.reset( result );
|
matcher.reset( result );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,43 +1,41 @@
|
||||||
package org.apache.maven.util.introspection;
|
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* 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
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
* ====================================================================
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.codehaus.plexus.util.StringUtils;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
import org.codehaus.plexus.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Using simple dotted expressions extract the values from a MavenProject
|
* Using simple dotted expressions extract the values from a MavenProject
|
||||||
* instance, For example we might want to extract a value like:
|
* instance, For example we might want to extract a value like:
|
||||||
* project.build.sourceDirectory
|
* project.build.sourceDirectory
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
|
* @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
|
* @version $Id$
|
||||||
* Exp $
|
|
||||||
*/
|
*/
|
||||||
public class ReflectionValueExtractor
|
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;
|
private static ClassMap classMap;
|
||||||
|
|
||||||
|
@ -48,7 +46,8 @@ public class ReflectionValueExtractor
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: don't throw Exception
|
// 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
|
// Remove the leading "project" token
|
||||||
|
@ -75,6 +74,11 @@ public class ReflectionValueExtractor
|
||||||
|
|
||||||
Method method = classMap.findMethod( methodName, args );
|
Method method = classMap.findMethod( methodName, args );
|
||||||
|
|
||||||
|
if ( method == null )
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
value = method.invoke( value, params );
|
value = method.invoke( value, params );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,8 +92,10 @@ public class ReflectionValueExtractor
|
||||||
if ( classMap == null )
|
if ( classMap == null )
|
||||||
{
|
{
|
||||||
classMap = new ClassMap( clazz );
|
classMap = new ClassMap( clazz );
|
||||||
|
|
||||||
|
classMaps.put( clazz, classMap );
|
||||||
}
|
}
|
||||||
|
|
||||||
return classMap;
|
return classMap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,17 +16,18 @@ package org.apache.maven.project.interpolation;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
import org.apache.maven.model.Dependency;
|
import org.apache.maven.model.Dependency;
|
||||||
import org.apache.maven.model.Model;
|
import org.apache.maven.model.Model;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author jdcasey
|
* @author jdcasey
|
||||||
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class RegexBasedModelInterpolatorTest
|
public class RegexBasedModelInterpolatorTest
|
||||||
extends TestCase
|
extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion()
|
public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion()
|
||||||
throws ModelInterpolationException
|
throws ModelInterpolationException
|
||||||
{
|
{
|
||||||
|
@ -75,5 +76,4 @@ public class RegexBasedModelInterpolatorTest
|
||||||
|
|
||||||
assertEquals( "foo-3.8.1", ( (Dependency) out.getDependencies().get( 0 ) ).getVersion() );
|
assertEquals( "foo-3.8.1", ( (Dependency) out.getDependencies().get( 0 ) ).getVersion() );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -33,15 +33,11 @@ public class ReflectionValueExtractorTest
|
||||||
{
|
{
|
||||||
private MavenProject project;
|
private MavenProject project;
|
||||||
|
|
||||||
private MavenProjectBuilder builder;
|
|
||||||
|
|
||||||
protected void setUp()
|
protected void setUp()
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
|
||||||
builder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
|
|
||||||
|
|
||||||
File f = getFileForClasspathResource( "pom.xml" );
|
File f = getFileForClasspathResource( "pom.xml" );
|
||||||
|
|
||||||
project = getProject( f );
|
project = getProject( f );
|
||||||
|
@ -88,4 +84,10 @@ public class ReflectionValueExtractorTest
|
||||||
|
|
||||||
assertNotNull( build );
|
assertNotNull( build );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testValueExtractorWithAInvalidExpression()
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
assertNull( ReflectionValueExtractor.evaluate( "project.foo", project ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue