diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java index 590f2dab06..1f3ada29d1 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java @@ -88,7 +88,8 @@ public class StringSearchModelInterpolator return interpolator; } - private static final class InterpolateObjectAction implements PrivilegedAction + private static final class InterpolateObjectAction + implements PrivilegedAction { private final LinkedList interpolationTargets; @@ -125,7 +126,6 @@ public class StringSearchModelInterpolator return null; } - @SuppressWarnings( "unchecked" ) private void traverseObjectWithParents( Class cls, Object target ) { if ( cls == null ) @@ -146,158 +146,7 @@ public class StringSearchModelInterpolator { synchronized ( currentField ) { - boolean isAccessible = currentField.isAccessible(); - currentField.setAccessible( true ); - try - { - if ( String.class == type ) - { - String value = (String) currentField.get( target ); - if ( value != null && !Modifier.isFinal( currentField.getModifiers() ) ) - { - String interpolated = - modelInterpolator.interpolateInternal( value, valueSources, postProcessors, - problems ); - - if ( !interpolated.equals( value ) ) - { - currentField.set( target, interpolated ); - } - } - } - else if ( Collection.class.isAssignableFrom( type ) ) - { - Collection c = (Collection) currentField.get( target ); - if ( c != null && !c.isEmpty() ) - { - List originalValues = new ArrayList( c ); - try - { - c.clear(); - } - catch ( UnsupportedOperationException e ) - { - continue; - } - - for ( Object value : originalValues ) - { - if ( value != null ) - { - if ( String.class == value.getClass() ) - { - String interpolated = - modelInterpolator.interpolateInternal( (String) value, - valueSources, - postProcessors, - problems ); - - if ( !interpolated.equals( value ) ) - { - c.add( interpolated ); - } - else - { - c.add( value ); - } - } - else - { - c.add( value ); - if ( value.getClass().isArray() ) - { - evaluateArray( value ); - } - else - { - interpolationTargets.add( value ); - } - } - } - else - { - // add the null back in...not sure what else to do... - c.add( value ); - } - } - } - } - else if ( Map.class.isAssignableFrom( type ) ) - { - Map m = (Map) currentField.get( target ); - if ( m != null && !m.isEmpty() ) - { - for ( Map.Entry entry : m.entrySet() ) - { - Object value = entry.getValue(); - - if ( value != null ) - { - if ( String.class == value.getClass() ) - { - String interpolated = - modelInterpolator.interpolateInternal( (String) value, - valueSources, - postProcessors, - problems ); - - if ( !interpolated.equals( value ) ) - { - try - { - entry.setValue( interpolated ); - } - catch ( UnsupportedOperationException e ) - { - continue; - } - } - } - else - { - if ( value.getClass().isArray() ) - { - evaluateArray( value ); - } - else - { - interpolationTargets.add( value ); - } - } - } - } - } - } - else - { - Object value = currentField.get( target ); - if ( value != null ) - { - if ( currentField.getType().isArray() ) - { - evaluateArray( value ); - } - else - { - interpolationTargets.add( value ); - } - } - } - } - catch ( IllegalArgumentException e ) - { - problems.add( Severity.ERROR, "Failed to interpolate field3: " + currentField - + " on class: " + cls.getName(), null, e ); - } - catch ( IllegalAccessException e ) - { - problems.add( Severity.ERROR, "Failed to interpolate field4: " + currentField - + " on class: " + cls.getName(), null, e ); - } - finally - { - currentField.setAccessible( isAccessible ); - } + interpolateField( cls, target, currentField, type ); } } } @@ -306,6 +155,177 @@ public class StringSearchModelInterpolator } } + private void interpolateField( Class cls, Object target, Field field, Class type ) + { + boolean isAccessible = field.isAccessible(); + field.setAccessible( true ); + try + { + if ( String.class == type ) + { + interpolateStringField( target, field ); + } + else if ( Collection.class.isAssignableFrom( type ) ) + { + interpolateCollectionField( target, field ); + } + else if ( Map.class.isAssignableFrom( type ) ) + { + interpolateMapField( target, field ); + } + else + { + Object value = field.get( target ); + if ( value != null ) + { + if ( field.getType().isArray() ) + { + evaluateArray( value ); + } + else + { + interpolationTargets.add( value ); + } + } + } + } + catch ( IllegalArgumentException e ) + { + problems.add( Severity.ERROR, "Failed to interpolate field3: " + field + " on class: " + cls.getName(), + null, e ); + } + catch ( IllegalAccessException e ) + { + problems.add( Severity.ERROR, "Failed to interpolate field4: " + field + " on class: " + cls.getName(), + null, e ); + } + finally + { + field.setAccessible( isAccessible ); + } + } + + private void interpolateStringField( Object target, Field field ) + throws IllegalAccessException + { + String value = (String) field.get( target ); + if ( value == null || Modifier.isFinal( field.getModifiers() ) ) + { + return; + } + + String interpolated = + modelInterpolator.interpolateInternal( value, valueSources, postProcessors, problems ); + + if ( !interpolated.equals( value ) ) + { + field.set( target, interpolated ); + } + } + + private void interpolateCollectionField( Object target, Field field ) + throws IllegalAccessException + { + @SuppressWarnings( "unchecked" ) + Collection c = (Collection) field.get( target ); + if ( c == null || c.isEmpty() ) + { + return; + } + + List originalValues = new ArrayList( c ); + try + { + c.clear(); + } + catch ( UnsupportedOperationException e ) + { + return; + } + + for ( Object value : originalValues ) + { + if ( value == null ) + { + // add the null back in...not sure what else to do... + c.add( value ); + } + else if ( String.class == value.getClass() ) + { + String interpolated = + modelInterpolator.interpolateInternal( (String) value, valueSources, postProcessors, problems ); + + if ( !interpolated.equals( value ) ) + { + c.add( interpolated ); + } + else + { + c.add( value ); + } + } + else + { + c.add( value ); + if ( value.getClass().isArray() ) + { + evaluateArray( value ); + } + else + { + interpolationTargets.add( value ); + } + } + } + } + + private void interpolateMapField( Object target, Field field ) + throws IllegalAccessException + { + @SuppressWarnings( "unchecked" ) + Map m = (Map) field.get( target ); + if ( m == null || m.isEmpty() ) + { + return; + } + + for ( Map.Entry entry : m.entrySet() ) + { + Object value = entry.getValue(); + + if ( value == null ) + { + continue; + } + + if ( String.class == value.getClass() ) + { + String interpolated = + modelInterpolator.interpolateInternal( (String) value, valueSources, postProcessors, problems ); + + if ( !interpolated.equals( value ) ) + { + try + { + entry.setValue( interpolated ); + } + catch ( UnsupportedOperationException e ) + { + continue; + } + } + } + else if ( value.getClass().isArray() ) + { + evaluateArray( value ); + } + else + { + interpolationTargets.add( value ); + } + } + } + private Field[] getFields( Class cls ) { Field[] fields = fieldsByClass.get( cls ); diff --git a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java index b0545d1441..5d24f185bd 100644 --- a/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java +++ b/maven-model-builder/src/test/java/org/apache/maven/model/interpolation/StringSearchModelInterpolatorTest.java @@ -432,7 +432,7 @@ public class StringSearchModelInterpolatorTest } } - @SuppressWarnings({"UnusedDeclaration", "unused"}) + @SuppressWarnings( "unused" ) private static final class ObjectWithMixedProtection { private List values1; @@ -475,7 +475,6 @@ public class StringSearchModelInterpolatorTest assertProblemFree( problems ); } - @SuppressWarnings({"UnusedDeclaration"}) static class ClassWithFinalField { public static final String CONSTANT = "${expression}";