/
MapConverter.java
130 lines (121 loc) · 5.96 KB
/
MapConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*******************************************************************************
* Copyright (c) 2010, 2015 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
*
* Minimal facade required to be binary-compatible with legacy Plexus API
*******************************************************************************/
package org.codehaus.plexus.component.configurator.converters.composite;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.component.configurator.ConfigurationListener;
import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter;
import org.codehaus.plexus.component.configurator.converters.ParameterizedConfigurationConverter;
import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.eclipse.sisu.inject.Logs;
public class MapConverter
extends AbstractConfigurationConverter
implements ParameterizedConfigurationConverter
{
public boolean canConvert( final Class<?> type )
{
return Map.class.isAssignableFrom( type ) && !Properties.class.isAssignableFrom( type );
}
public Object fromConfiguration( final ConverterLookup lookup, final PlexusConfiguration configuration,
final Class<?> type, final Class<?> enclosingType, final ClassLoader loader,
final ExpressionEvaluator evaluator, final ConfigurationListener listener )
throws ComponentConfigurationException
{
return fromConfiguration( lookup, configuration, type, null, enclosingType, loader, evaluator, listener );
}
public Object fromConfiguration( final ConverterLookup lookup, final PlexusConfiguration configuration,
final Class<?> type, final Type[] typeArguments, final Class<?> enclosingType,
final ClassLoader loader, final ExpressionEvaluator evaluator,
final ConfigurationListener listener )
throws ComponentConfigurationException
{
final Object value = fromExpression( configuration, evaluator, type );
if ( null != value )
{
return value;
}
try
{
final Map<Object, Object> map = instantiateMap( configuration, type, loader );
final Class<?> elementType = findElementType( typeArguments );
if ( Object.class == elementType || String.class == elementType )
{
for ( int i = 0, size = configuration.getChildCount(); i < size; i++ )
{
final PlexusConfiguration element = configuration.getChild( i );
map.put( element.getName(), fromExpression( element, evaluator ) );
}
return map;
}
// handle maps with complex element types...
final ConfigurationConverter converter = lookup.lookupConverterForType( elementType );
for ( int i = 0, size = configuration.getChildCount(); i < size; i++ )
{
Object elementValue;
final PlexusConfiguration element = configuration.getChild( i );
try
{
elementValue = converter.fromConfiguration( lookup, element, elementType, enclosingType, //
loader, evaluator, listener );
}
// TEMP: remove when http://jira.codehaus.org/browse/MSHADE-168 is fixed
catch ( final ComponentConfigurationException e )
{
elementValue = fromExpression( element, evaluator );
Logs.warn( "Map in " + enclosingType + " declares value type as: {} but saw: {} at runtime",
elementType, null != elementValue ? elementValue.getClass() : null );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
map.put( element.getName(), elementValue );
}
return map;
}
catch ( final ComponentConfigurationException e )
{
if ( null == e.getFailedConfiguration() )
{
e.setFailedConfiguration( configuration );
}
throw e;
}
}
@SuppressWarnings( "unchecked" )
private Map<Object, Object> instantiateMap( final PlexusConfiguration configuration, final Class<?> type,
final ClassLoader loader )
throws ComponentConfigurationException
{
final Class<?> implType = getClassForImplementationHint( type, configuration, loader );
if ( null == implType || Modifier.isAbstract( implType.getModifiers() ) )
{
return new TreeMap<Object, Object>();
}
final Object impl = instantiateObject( implType );
failIfNotTypeCompatible( impl, type, configuration );
return (Map<Object, Object>) impl;
}
private static Class<?> findElementType( final Type[] typeArguments )
{
if ( null != typeArguments && typeArguments.length > 1 && typeArguments[1] instanceof Class<?> )
{
return (Class<?>) typeArguments[1];
}
return Object.class;
}
}