23
23
import org .eclipse .core .resources .IResource ;
24
24
import org .eclipse .core .resources .ResourcesPlugin ;
25
25
import org .eclipse .core .resources .variableresolvers .PathVariableResolver ;
26
- import org .eclipse .core .runtime .*;
26
+ import org .eclipse .core .runtime .CoreException ;
27
+ import org .eclipse .core .runtime .IConfigurationElement ;
28
+ import org .eclipse .core .runtime .IExtension ;
29
+ import org .eclipse .core .runtime .IExtensionPoint ;
30
+ import org .eclipse .core .runtime .IStatus ;
31
+ import org .eclipse .core .runtime .Platform ;
32
+ import org .eclipse .core .runtime .Status ;
27
33
import org .eclipse .osgi .util .NLS ;
28
34
29
35
/**
33
39
public class ProjectVariableProviderManager {
34
40
35
41
public static class Descriptor {
36
- PathVariableResolver provider = null ;
37
- String name = null ;
38
- String value = null ;
42
+ private final PathVariableResolver provider ;
43
+ private final String name ;
44
+ private final String value ;
39
45
40
46
public Descriptor (IExtension extension , IConfigurationElement element ) throws RuntimeException , CoreException {
41
47
name = element .getAttribute ("variable" ); //$NON-NLS-1$
42
48
value = element .getAttribute ("value" ); //$NON-NLS-1$
49
+ PathVariableResolver p = null ;
43
50
try {
44
51
String classAttribute = "class" ; //$NON-NLS-1$
45
52
if (element .getAttribute (classAttribute ) != null )
46
- provider = (PathVariableResolver ) element .createExecutableExtension (classAttribute );
53
+ p = (PathVariableResolver ) element .createExecutableExtension (classAttribute );
47
54
} catch (CoreException e ) {
48
55
Policy .log (e );
49
56
}
57
+ provider = p ;
50
58
if (name == null )
51
59
fail (NLS .bind (Messages .mapping_invalidDef , extension .getUniqueIdentifier ()));
52
60
}
@@ -74,30 +82,25 @@ public String[] getVariableNames(String variable, IResource resource) {
74
82
}
75
83
}
76
84
77
- private static Map <String , Descriptor > descriptors ;
78
- private static Descriptor [] descriptorsArray ;
79
- private static ProjectVariableProviderManager instance = new ProjectVariableProviderManager ();
85
+ private static final Map <String , Descriptor > descriptors = getDescriptorMap () ;
86
+ private static final Descriptor [] descriptorsArray = descriptors . values (). toArray ( Descriptor []:: new ) ;
87
+ private static final ProjectVariableProviderManager instance = new ProjectVariableProviderManager ();
80
88
81
89
public static ProjectVariableProviderManager getDefault () {
82
90
return instance ;
83
91
}
84
92
85
93
public Descriptor [] getDescriptors () {
86
- lazyInitialize ();
87
94
return descriptorsArray ;
88
95
}
89
96
90
- protected void lazyInitialize () {
91
- if (descriptors != null )
92
- return ;
97
+ private static Map <String , Descriptor > getDescriptorMap () {
93
98
IExtensionPoint point = Platform .getExtensionRegistry ().getExtensionPoint (ResourcesPlugin .PI_RESOURCES , ResourcesPlugin .PT_VARIABLE_PROVIDERS );
94
99
IExtension [] extensions = point .getExtensions ();
95
- descriptors = new HashMap <>(extensions .length * 2 + 1 );
100
+ Map < String , Descriptor > d = new HashMap <>(extensions .length * 2 + 1 );
96
101
for (IExtension extension : extensions ) {
97
102
IConfigurationElement [] elements = extension .getConfigurationElements ();
98
- int count = elements .length ;
99
- for (int j = 0 ; j < count ; j ++) {
100
- IConfigurationElement element = elements [j ];
103
+ for (IConfigurationElement element : elements ) {
101
104
String elementName = element .getName ();
102
105
if (elementName .equalsIgnoreCase ("variableResolver" )) { //$NON-NLS-1$
103
106
Descriptor desc = null ;
@@ -107,15 +110,14 @@ protected void lazyInitialize() {
107
110
Policy .log (e );
108
111
}
109
112
if (desc != null )
110
- descriptors .put (desc .getName (), desc );
113
+ d .put (desc .getName (), desc );
111
114
}
112
115
}
113
116
}
114
- descriptorsArray = descriptors . values (). toArray ( new Descriptor [ descriptors . size ()] );
117
+ return Map . copyOf ( d );
115
118
}
116
119
117
120
public Descriptor findDescriptor (String name ) {
118
- lazyInitialize ();
119
121
Descriptor result = descriptors .get (name );
120
122
return result ;
121
123
}
0 commit comments