PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/maven-3.0.5/maven-compat/src/main/java/org/apache/maven/project/interpolation/StringSearchModelInterpolator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 407 lines | 342 code | 42 blank | 23 comment | 59 complexity | 299f0b6f52be4e4315fcc122603eebe3 MD5 | raw file
  1. package org.apache.maven.project.interpolation;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.maven.model.Model;
  21. import org.apache.maven.project.ProjectBuilderConfiguration;
  22. import org.apache.maven.project.path.PathTranslator;
  23. import org.codehaus.plexus.component.annotations.Component;
  24. import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
  25. import org.codehaus.plexus.interpolation.Interpolator;
  26. import org.codehaus.plexus.interpolation.StringSearchInterpolator;
  27. import org.codehaus.plexus.interpolation.ValueSource;
  28. import org.codehaus.plexus.logging.Logger;
  29. import java.io.File;
  30. import java.lang.reflect.Array;
  31. import java.lang.reflect.Field;
  32. import java.security.AccessController;
  33. import java.security.PrivilegedAction;
  34. import java.util.ArrayList;
  35. import java.util.Collection;
  36. import java.util.LinkedList;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.WeakHashMap;
  40. @Deprecated
  41. @Component( role = ModelInterpolator.class )
  42. public class StringSearchModelInterpolator
  43. extends AbstractStringBasedModelInterpolator
  44. {
  45. private static final Map<Class<?>, Field[]> fieldsByClass = new WeakHashMap<Class<?>, Field[]>();
  46. private static final Map<Class<?>, Boolean> fieldIsPrimitiveByClass = new WeakHashMap<Class<?>, Boolean>();
  47. public StringSearchModelInterpolator()
  48. {
  49. }
  50. public StringSearchModelInterpolator( PathTranslator pathTranslator )
  51. {
  52. super( pathTranslator );
  53. }
  54. public Model interpolate( Model model, File projectDir, ProjectBuilderConfiguration config, boolean debugEnabled )
  55. throws ModelInterpolationException
  56. {
  57. interpolateObject( model, model, projectDir, config, debugEnabled );
  58. return model;
  59. }
  60. protected void interpolateObject( Object obj, Model model, File projectDir, ProjectBuilderConfiguration config,
  61. boolean debugEnabled )
  62. throws ModelInterpolationException
  63. {
  64. try
  65. {
  66. List<ValueSource> valueSources = createValueSources( model, projectDir, config );
  67. List<InterpolationPostProcessor> postProcessors = createPostProcessors( model, projectDir, config );
  68. InterpolateObjectAction action =
  69. new InterpolateObjectAction( obj, valueSources, postProcessors, debugEnabled,
  70. this, getLogger() );
  71. ModelInterpolationException error = AccessController.doPrivileged( action );
  72. if ( error != null )
  73. {
  74. throw error;
  75. }
  76. }
  77. finally
  78. {
  79. getInterpolator().clearAnswers();
  80. }
  81. }
  82. protected Interpolator createInterpolator()
  83. {
  84. StringSearchInterpolator interpolator = new StringSearchInterpolator();
  85. interpolator.setCacheAnswers( true );
  86. return interpolator;
  87. }
  88. private static final class InterpolateObjectAction implements PrivilegedAction<ModelInterpolationException>
  89. {
  90. private final boolean debugEnabled;
  91. private final LinkedList<Object> interpolationTargets;
  92. private final StringSearchModelInterpolator modelInterpolator;
  93. private final Logger logger;
  94. private final List<ValueSource> valueSources;
  95. private final List<InterpolationPostProcessor> postProcessors;
  96. public InterpolateObjectAction( Object target, List<ValueSource> valueSources,
  97. List<InterpolationPostProcessor> postProcessors, boolean debugEnabled,
  98. StringSearchModelInterpolator modelInterpolator, Logger logger )
  99. {
  100. this.valueSources = valueSources;
  101. this.postProcessors = postProcessors;
  102. this.debugEnabled = debugEnabled;
  103. this.interpolationTargets = new LinkedList<Object>();
  104. interpolationTargets.add( target );
  105. this.modelInterpolator = modelInterpolator;
  106. this.logger = logger;
  107. }
  108. public ModelInterpolationException run()
  109. {
  110. while ( !interpolationTargets.isEmpty() )
  111. {
  112. Object obj = interpolationTargets.removeFirst();
  113. try
  114. {
  115. traverseObjectWithParents( obj.getClass(), obj );
  116. }
  117. catch ( ModelInterpolationException e )
  118. {
  119. return e;
  120. }
  121. }
  122. return null;
  123. }
  124. @SuppressWarnings( "unchecked" )
  125. private void traverseObjectWithParents( Class<?> cls, Object target )
  126. throws ModelInterpolationException
  127. {
  128. if ( cls == null )
  129. {
  130. return;
  131. }
  132. if ( cls.isArray() )
  133. {
  134. evaluateArray( target );
  135. }
  136. else if ( isQualifiedForInterpolation( cls ) )
  137. {
  138. Field[] fields = fieldsByClass.get( cls );
  139. if ( fields == null )
  140. {
  141. fields = cls.getDeclaredFields();
  142. fieldsByClass.put( cls, fields );
  143. }
  144. for ( int i = 0; i < fields.length; i++ )
  145. {
  146. Class<?> type = fields[i].getType();
  147. if ( isQualifiedForInterpolation( fields[i], type ) )
  148. {
  149. boolean isAccessible = fields[i].isAccessible();
  150. fields[i].setAccessible( true );
  151. try
  152. {
  153. try
  154. {
  155. if ( String.class == type )
  156. {
  157. String value = (String) fields[i].get( target );
  158. if ( value != null )
  159. {
  160. String interpolated = modelInterpolator.interpolateInternal( value, valueSources, postProcessors, debugEnabled );
  161. if ( !interpolated.equals( value ) )
  162. {
  163. fields[i].set( target, interpolated );
  164. }
  165. }
  166. }
  167. else if ( Collection.class.isAssignableFrom( type ) )
  168. {
  169. Collection<Object> c = (Collection<Object>) fields[i].get( target );
  170. if ( c != null && !c.isEmpty() )
  171. {
  172. List<Object> originalValues = new ArrayList<Object>( c );
  173. try
  174. {
  175. c.clear();
  176. }
  177. catch ( UnsupportedOperationException e )
  178. {
  179. if ( debugEnabled && logger != null )
  180. {
  181. logger.debug( "Skipping interpolation of field: " + fields[i] + " in: "
  182. + cls.getName() + "; it is an unmodifiable collection." );
  183. }
  184. continue;
  185. }
  186. for ( Object value : originalValues )
  187. {
  188. if ( value != null )
  189. {
  190. if ( String.class == value.getClass() )
  191. {
  192. String interpolated =
  193. modelInterpolator.interpolateInternal( (String) value,
  194. valueSources,
  195. postProcessors,
  196. debugEnabled );
  197. if ( !interpolated.equals( value ) )
  198. {
  199. c.add( interpolated );
  200. }
  201. else
  202. {
  203. c.add( value );
  204. }
  205. }
  206. else
  207. {
  208. c.add( value );
  209. if ( value.getClass().isArray() )
  210. {
  211. evaluateArray( value );
  212. }
  213. else
  214. {
  215. interpolationTargets.add( value );
  216. }
  217. }
  218. }
  219. else
  220. {
  221. // add the null back in...not sure what else to do...
  222. c.add( value );
  223. }
  224. }
  225. }
  226. }
  227. else if ( Map.class.isAssignableFrom( type ) )
  228. {
  229. Map<Object, Object> m = (Map<Object, Object>) fields[i].get( target );
  230. if ( m != null && !m.isEmpty() )
  231. {
  232. for ( Map.Entry<Object, Object> entry : m.entrySet() )
  233. {
  234. Object value = entry.getValue();
  235. if ( value != null )
  236. {
  237. if ( String.class == value.getClass() )
  238. {
  239. String interpolated =
  240. modelInterpolator.interpolateInternal( (String) value,
  241. valueSources,
  242. postProcessors,
  243. debugEnabled );
  244. if ( !interpolated.equals( value ) )
  245. {
  246. try
  247. {
  248. entry.setValue( interpolated );
  249. }
  250. catch ( UnsupportedOperationException e )
  251. {
  252. if ( debugEnabled && logger != null )
  253. {
  254. logger.debug( "Skipping interpolation of field: "
  255. + fields[i] + " (key: " + entry.getKey() + ") in: "
  256. + cls.getName()
  257. + "; it is an unmodifiable collection." );
  258. }
  259. continue;
  260. }
  261. }
  262. }
  263. else
  264. {
  265. if ( value.getClass().isArray() )
  266. {
  267. evaluateArray( value );
  268. }
  269. else
  270. {
  271. interpolationTargets.add( value );
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. else
  279. {
  280. Object value = fields[i].get( target );
  281. if ( value != null )
  282. {
  283. if ( fields[i].getType().isArray() )
  284. {
  285. evaluateArray( value );
  286. }
  287. else
  288. {
  289. interpolationTargets.add( value );
  290. }
  291. }
  292. }
  293. }
  294. catch ( IllegalArgumentException e )
  295. {
  296. throw new ModelInterpolationException( "Failed to interpolate field: " + fields[i]
  297. + " on class: " + cls.getName(), e );
  298. }
  299. catch ( IllegalAccessException e )
  300. {
  301. throw new ModelInterpolationException( "Failed to interpolate field: " + fields[i]
  302. + " on class: " + cls.getName(), e );
  303. }
  304. }
  305. finally
  306. {
  307. fields[i].setAccessible( isAccessible );
  308. }
  309. }
  310. }
  311. traverseObjectWithParents( cls.getSuperclass(), target );
  312. }
  313. }
  314. private boolean isQualifiedForInterpolation( Class<?> cls )
  315. {
  316. return !cls.getPackage().getName().startsWith( "java" );
  317. }
  318. private boolean isQualifiedForInterpolation( Field field, Class<?> fieldType )
  319. {
  320. if ( !fieldIsPrimitiveByClass.containsKey( fieldType ) )
  321. {
  322. fieldIsPrimitiveByClass.put( fieldType, Boolean.valueOf( fieldType.isPrimitive() ) );
  323. }
  324. if ( fieldIsPrimitiveByClass.get( fieldType ).booleanValue() )
  325. {
  326. return false;
  327. }
  328. // if ( fieldType.isPrimitive() )
  329. // {
  330. // return false;
  331. // }
  332. if ( "parent".equals( field.getName() ) )
  333. {
  334. return false;
  335. }
  336. return true;
  337. }
  338. private void evaluateArray( Object target )
  339. throws ModelInterpolationException
  340. {
  341. int len = Array.getLength( target );
  342. for ( int i = 0; i < len; i++ )
  343. {
  344. Object value = Array.get( target, i );
  345. if ( value != null )
  346. {
  347. if ( String.class == value.getClass() )
  348. {
  349. String interpolated =
  350. modelInterpolator.interpolateInternal( (String) value, valueSources, postProcessors,
  351. debugEnabled );
  352. if ( !interpolated.equals( value ) )
  353. {
  354. Array.set( target, i, interpolated );
  355. }
  356. }
  357. else
  358. {
  359. interpolationTargets.add( value );
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }