Skip to content

Commit 4d088de

Browse files
committed
Commit to undo the changes I've made since forked the original project. The reason is that I've made a mistake and created a unique PR to solve several issues, which is a bad practice.
1 parent a3937b2 commit 4d088de

File tree

5 files changed

+55
-371
lines changed

5 files changed

+55
-371
lines changed

‎src/main/java/org/codehaus/plexus/interpolation/multi/MultiDelimiterStringSearchInterpolator.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,13 @@ private String interpolate( String input, RecursionInterceptor recursionIntercep
201201

202202
if ( startIdx >= 0 && escapeString != null && escapeString.length() > 0 )
203203
{
204-
intstartEscapeIdx = (startIdx == 0) ? 0 : startIdx - escapeString.length();
204+
intstartEscapeIdx = startIdx == 0 ? 0 : startIdx - escapeString.length();
205205
if ( startEscapeIdx >= 0 )
206206
{
207207
Stringescape = input.substring( startEscapeIdx, startIdx );
208208
if ( escape != null && escapeString.equals( escape ) )
209209
{
210210
result.append( wholeExpr );
211-
if (startEscapeIdx > 0){
212-
--startEscapeIdx;
213-
}
214211
result.replace( startEscapeIdx, startEscapeIdx + escapeString.length(), "" );
215212
continue;
216213
}
Lines changed: 38 additions & 266 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
packageorg.codehaus.plexus.interpolation.reflection;
22

3-
importjava.lang.ref.WeakReference;
4-
importjava.lang.reflect.Array;
5-
importjava.lang.reflect.InvocationTargetException;
6-
importjava.lang.reflect.Method;
7-
importjava.util.List;
8-
importjava.util.Map;
9-
importjava.util.WeakHashMap;
10-
113
/*
124
* Copyright 2001-2006 Codehaus Foundation.
135
*
@@ -26,6 +18,13 @@
2618

2719
importorg.codehaus.plexus.interpolation.util.StringUtils;
2820

21+
importjava.lang.ref.SoftReference;
22+
importjava.lang.ref.WeakReference;
23+
importjava.lang.reflect.Method;
24+
importjava.util.Map;
25+
importjava.util.StringTokenizer;
26+
importjava.util.WeakHashMap;
27+
2928
/**
3029
* <b>NOTE:</b> This class was copied from plexus-utils, to allow this library
3130
* to stand completely self-contained.
@@ -44,317 +43,90 @@ public class ReflectionValueExtractor
4443
privatestaticfinalObject[] OBJECT_ARGS = newObject[0];
4544

4645
/**
47-
* Use a WeakHashMap here, so the keys (Class objects) can be garbage collected. This approach prevents permgen
48-
* space overflows due to retention of discarded classloaders.
46+
* Use a WeakHashMap here, so the keys (Class objects) can be garbage collected.
47+
* This approach prevents permgen space overflows due to retention of discarded
48+
* classloaders.
4949
*/
50-
privatestaticfinalMap<Class<?>, WeakReference<ClassMap>> classMaps =
51-
newWeakHashMap<Class<?>, WeakReference<ClassMap>>();
52-
53-
staticfinalintEOF = -1;
54-
55-
staticfinalcharPROPERTY_START = '.';
56-
57-
staticfinalcharINDEXED_START = '[';
58-
59-
staticfinalcharINDEXED_END = ']';
60-
61-
staticfinalcharMAPPED_START = '(';
62-
63-
staticfinalcharMAPPED_END = ')';
64-
65-
staticclassTokenizer
66-
{
67-
finalStringexpression;
68-
69-
intidx;
70-
71-
publicTokenizer( Stringexpression )
72-
{
73-
this.expression = expression;
74-
}
75-
76-
publicintpeekChar()
77-
{
78-
returnidx < expression.length() ? expression.charAt( idx ) : EOF;
79-
}
80-
81-
publicintskipChar()
82-
{
83-
returnidx < expression.length() ? expression.charAt( idx++ ) : EOF;
84-
}
85-
86-
publicStringnextToken( chardelimiter )
87-
{
88-
intstart = idx;
89-
90-
while ( idx < expression.length() && delimiter != expression.charAt( idx ) )
91-
{
92-
idx++;
93-
}
94-
95-
// delimiter MUST be present
96-
if ( idx <= start || idx >= expression.length() )
97-
{
98-
returnnull;
99-
}
100-
101-
returnexpression.substring( start, idx++ );
102-
}
103-
104-
publicStringnextPropertyName()
105-
{
106-
finalintstart = idx;
107-
108-
while ( idx < expression.length() && Character.isJavaIdentifierPart( expression.charAt( idx ) ) )
109-
{
110-
idx++;
111-
}
112-
113-
// property name does not require delimiter
114-
if ( idx <= start || idx > expression.length() )
115-
{
116-
returnnull;
117-
}
118-
119-
returnexpression.substring( start, idx );
120-
}
121-
122-
publicintgetPosition()
123-
{
124-
returnidx < expression.length() ? idx : EOF;
125-
}
126-
127-
// to make tokenizer look pretty in debugger
128-
@Override
129-
publicStringtoString()
130-
{
131-
returnidx < expression.length() ? expression.substring( idx ) : "<EOF>";
132-
}
133-
}
50+
privatestaticfinalMap<Class<?>, WeakReference<ClassMap>> classMaps = newWeakHashMap<Class<?>, WeakReference<ClassMap>>();
13451

13552
privateReflectionValueExtractor()
13653
{
13754
}
13855

139-
/**
140-
* <p>
141-
* The implementation supports indexed, nested and mapped properties.
142-
* </p>
143-
* <ul>
144-
* <li>nested properties should be defined by a dot, i.e. "user.address.street"</li>
145-
* <li>indexed properties (java.util.List or array instance) should be contains <code>(\\w+)\\[(\\d+)\\]</code>
146-
* pattern, i.e. "user.addresses[1].street"</li>
147-
* <li>mapped properties should be contains <code>(\\w+)\\((.+)\\)</code> pattern, i.e.
148-
* "user.addresses(myAddress).street"</li>
149-
* <ul>
150-
*
151-
* @param expression not null expression
152-
* @param root not null object
153-
* @return the object defined by the expression
154-
* @throws Exception if any
155-
*/
15656
publicstaticObjectevaluate( Stringexpression, Objectroot )
15757
throwsException
15858
{
15959
returnevaluate( expression, root, true );
16060
}
16161

162-
/**
163-
* <p>
164-
* The implementation supports indexed, nested and mapped properties.
165-
* </p>
166-
* <ul>
167-
* <li>nested properties should be defined by a dot, i.e. "user.address.street"</li>
168-
* <li>indexed properties (java.util.List or array instance) should be contains <code>(\\w+)\\[(\\d+)\\]</code>
169-
* pattern, i.e. "user.addresses[1].street"</li>
170-
* <li>mapped properties should be contains <code>(\\w+)\\((.+)\\)</code> pattern, i.e.
171-
* "user.addresses(myAddress).street"</li>
172-
* <ul>
173-
*
174-
* @param expression not null expression
175-
* @param root not null object
176-
* @return the object defined by the expression
177-
* @throws Exception if any
178-
*/
17962
// TODO: don't throw Exception
180-
publicstaticObjectevaluate( Stringexpression, finalObjectroot,finalbooleantrimRootToken )
63+
publicstaticObjectevaluate( Stringexpression, Objectroot, booleantrimRootToken )
18164
throwsException
18265
{
66+
// if the root token refers to the supplied root object parameter, remove it.
67+
if ( trimRootToken )
68+
{
69+
expression = expression.substring( expression.indexOf( '.' ) + 1 );
70+
}
71+
18372
Objectvalue = root;
18473

18574
// ----------------------------------------------------------------------
18675
// Walk the dots and retrieve the ultimate value desired from the
18776
// MavenProject instance.
18877
// ----------------------------------------------------------------------
18978

190-
if ( expression == null || "".equals(expression.trim()) || !Character.isJavaIdentifierStart( expression.charAt( 0 ) ) )
191-
{
192-
returnnull;
193-
}
79+
StringTokenizerparser = newStringTokenizer( expression, "." );
19480

195-
booleanhasDots = expression.indexOf( PROPERTY_START ) >= 0;
196-
197-
finalTokenizertokenizer;
198-
if ( trimRootToken && hasDots )
81+
while ( parser.hasMoreTokens() )
19982
{
200-
tokenizer = newTokenizer( expression );
201-
tokenizer.nextPropertyName();
202-
if ( tokenizer.getPosition() == EOF )
203-
{
204-
returnnull;
205-
}
206-
}
207-
else
208-
{
209-
tokenizer = newTokenizer( "." + expression );
210-
}
83+
Stringtoken = parser.nextToken();
21184

212-
intpropertyPosition = tokenizer.getPosition();
213-
while ( value != null && tokenizer.peekChar() != EOF )
214-
{
215-
switch ( tokenizer.skipChar() )
85+
if ( value == null )
21686
{
217-
caseINDEXED_START:
218-
value =
219-
getIndexedValue( expression, propertyPosition, tokenizer.getPosition(), value,
220-
tokenizer.nextToken( INDEXED_END ) );
221-
break;
222-
caseMAPPED_START:
223-
value =
224-
getMappedValue( expression, propertyPosition, tokenizer.getPosition(), value,
225-
tokenizer.nextToken( MAPPED_END ) );
226-
break;
227-
casePROPERTY_START:
228-
propertyPosition = tokenizer.getPosition();
229-
value = getPropertyValue( value, tokenizer.nextPropertyName() );
230-
break;
231-
default:
232-
// could not parse expression
233-
returnnull;
87+
returnnull;
23488
}
235-
}
236-
237-
returnvalue;
238-
}
23989

240-
privatestaticObjectgetMappedValue( finalStringexpression, finalintfrom, finalintto, finalObjectvalue,
241-
finalStringkey )
242-
throwsException
243-
{
244-
if ( value == null || key == null )
245-
{
246-
returnnull;
247-
}
248-
249-
if ( valueinstanceofMap )
250-
{
251-
Object[] localParams = newObject[]{key };
25290
ClassMapclassMap = getClassMap( value.getClass() );
253-
Methodmethod = classMap.findMethod( "get", localParams );
254-
returnmethod.invoke( value, localParams );
255-
}
25691

257-
finalStringmessage =
258-
String.format( "The token '%s' at position '%d' refers to a java.util.Map, but the value seems is an instance of '%s'",
259-
expression.subSequence( from, to ), from, value.getClass() );
92+
StringmethodBase = StringUtils.capitalizeFirstLetter( token );
26093

261-
thrownewException( message );
262-
}
94+
StringmethodName = "get" + methodBase;
26395

264-
privatestaticObjectgetIndexedValue( finalStringexpression, finalintfrom, finalintto, finalObjectvalue,
265-
finalStringindexStr )
266-
throwsException
267-
{
268-
try
269-
{
270-
intindex = Integer.parseInt( indexStr );
96+
Methodmethod = classMap.findMethod( methodName, CLASS_ARGS );
27197

272-
if ( value.getClass().isArray() )
98+
if ( method == null )
27399
{
274-
returnArray.get( value, index );
275-
}
100+
// perhaps this is a boolean property??
101+
methodName = "is" + methodBase;
276102

277-
if ( valueinstanceofList )
278-
{
279-
ClassMapclassMap = getClassMap( value.getClass() );
280-
// use get method on List interface
281-
Object[] localParams = newObject[]{index };
282-
Methodmethod = classMap.findMethod( "get", localParams );
283-
returnmethod.invoke( value, localParams );
103+
method = classMap.findMethod( methodName, CLASS_ARGS );
284104
}
285-
}
286-
catch ( NumberFormatExceptione )
287-
{
288-
returnnull;
289-
}
290-
catch ( InvocationTargetExceptione )
291-
{
292-
// catch array index issues gracefully, otherwise release
293-
if ( e.getCause() instanceofIndexOutOfBoundsException )
105+
106+
if ( method == null )
294107
{
295108
returnnull;
296109
}
297110

298-
throwe;
299-
}
300-
301-
finalStringmessage =
302-
String.format( "The token '%s' at position '%d' refers to a java.util.List or an array, but the value seems is an instance of '%s'",
303-
expression.subSequence( from, to ), from, value.getClass() );
304-
305-
thrownewException( message );
306-
}
307-
308-
privatestaticObjectgetPropertyValue( Objectvalue, Stringproperty )
309-
throwsException
310-
{
311-
if ( value == null || property == null )
312-
{
313-
returnnull;
111+
value = method.invoke( value, OBJECT_ARGS );
314112
}
315113

316-
ClassMapclassMap = getClassMap( value.getClass() );
317-
StringmethodBase = StringUtils.capitalizeFirstLetter( property );
318-
StringmethodName = "get" + methodBase;
319-
Methodmethod = classMap.findMethod( methodName, CLASS_ARGS );
320-
321-
if ( method == null )
322-
{
323-
// perhaps this is a boolean property??
324-
methodName = "is" + methodBase;
325-
326-
method = classMap.findMethod( methodName, CLASS_ARGS );
327-
}
328-
329-
if ( method == null )
330-
{
331-
returnnull;
332-
}
333-
334-
try
335-
{
336-
returnmethod.invoke( value, OBJECT_ARGS );
337-
}
338-
catch ( InvocationTargetExceptione )
339-
{
340-
throwe;
341-
}
114+
returnvalue;
342115
}
343116

344117
privatestaticClassMapgetClassMap( Class<?> clazz )
345118
{
346-
347-
WeakReference<ClassMap> softRef = classMaps.get( clazz );
119+
WeakReference<ClassMap> ref = classMaps.get( clazz);
348120

349121
ClassMapclassMap;
350122

351-
if ( softRef == null || (classMap = softRef.get()) == null )
123+
if ( ref == null || (classMap = ref.get()) == null )
352124
{
353125
classMap = newClassMap( clazz );
354126

355-
classMaps.put( clazz, newWeakReference<ClassMap>(classMap) );
127+
classMaps.put( clazz, newWeakReference<ClassMap>(classMap) );
356128
}
357129

358130
returnclassMap;
359131
}
360-
}
132+
}

0 commit comments

Comments
(0)