0
# Language Module Setup
1
2
Core language module registration and configuration for integrating Visualforce analysis into PMD. This capability handles language registration, parser creation, and configuration management.
3
4
## Capabilities
5
6
### VfLanguageModule
7
8
Main entry point for PMD's Visualforce language support. This singleton class registers the Visualforce language with PMD and provides core services.
9
10
```java { .api }
11
/**
12
* Main language module for Visualforce that extends PMD's language framework
13
* Implements CpdCapableLanguage for copy-paste detection support
14
*/
15
public class VfLanguageModule extends SimpleLanguageModuleBase implements CpdCapableLanguage {
16
/** Language identifier constant */
17
static final String ID = "visualforce";
18
19
/** Display name constant */
20
static final String NAME = "Salesforce Visualforce";
21
22
/**
23
* Get the singleton instance of the Visualforce language module
24
* @return VfLanguageModule instance registered with PMD
25
*/
26
public static VfLanguageModule getInstance();
27
28
/**
29
* Create a CPD lexer for copy-paste detection in Visualforce files
30
* @param bundle Language property configuration
31
* @return CpdLexer instance for Visualforce tokenization
32
*/
33
@Override
34
public CpdLexer createCpdLexer(LanguagePropertyBundle bundle);
35
36
/**
37
* Create a new property bundle with default Visualforce configuration
38
* @return VfLanguageProperties with default settings
39
*/
40
@Override
41
public LanguagePropertyBundle newPropertyBundle();
42
}
43
```
44
45
**Usage Example:**
46
47
```java
48
import net.sourceforge.pmd.lang.visualforce.VfLanguageModule;
49
import net.sourceforge.pmd.lang.LanguageRegistry;
50
51
// Get the Visualforce language module instance
52
VfLanguageModule vfModule = VfLanguageModule.getInstance();
53
54
// Check if Visualforce language is registered
55
boolean isRegistered = LanguageRegistry.PMD.getLanguageById("visualforce") != null;
56
57
// Create CPD lexer for copy-paste detection
58
VfLanguageProperties properties = new VfLanguageProperties();
59
CpdLexer lexer = vfModule.createCpdLexer(properties);
60
```
61
62
### VfLanguageProperties
63
64
Configuration properties for Visualforce language analysis, including paths to Apex classes and Salesforce objects for type resolution.
65
66
```java { .api }
67
/**
68
* Language-specific properties for Visualforce analysis configuration
69
* Extends PMD's LanguagePropertyBundle with Visualforce-specific settings
70
*/
71
public class VfLanguageProperties extends LanguagePropertyBundle {
72
/**
73
* Directory paths containing Apex classes referenced from Visualforce pages
74
* Environment variable: PMD_VF_APEX_DIRECTORIES
75
* Default: "../classes" (relative to Visualforce directory)
76
*/
77
public static final PropertyDescriptor<List<String>> APEX_DIRECTORIES_DESCRIPTOR =
78
PropertyFactory.stringListProperty("apexDirectories")
79
.desc("Location of Apex Class directories. Absolute or relative to the Visualforce directory.")
80
.defaultValues("../classes")
81
.build();
82
83
/**
84
* Directory paths containing Custom Object definitions referenced from Visualforce pages
85
* Environment variable: PMD_VF_OBJECTS_DIRECTORIES
86
* Default: "../objects" (relative to Visualforce directory)
87
*/
88
public static final PropertyDescriptor<List<String>> OBJECTS_DIRECTORIES_DESCRIPTOR =
89
PropertyFactory.stringListProperty("objectsDirectories")
90
.desc("Location of Custom Object directories. Absolute or relative to the Visualforce directory.")
91
.defaultValues("../objects")
92
.build();
93
94
/**
95
* Create new VfLanguageProperties with default configuration
96
*/
97
public VfLanguageProperties();
98
}
99
```
100
101
**Usage Example:**
102
103
```java
104
import net.sourceforge.pmd.lang.visualforce.VfLanguageProperties;
105
import java.util.Arrays;
106
107
// Create properties with default configuration
108
VfLanguageProperties properties = new VfLanguageProperties();
109
110
// Configure custom Apex directories
111
properties.setProperty(VfLanguageProperties.APEX_DIRECTORIES_DESCRIPTOR,
112
Arrays.asList("/path/to/apex/classes", "/another/apex/path"));
113
114
// Configure custom Object directories
115
properties.setProperty(VfLanguageProperties.OBJECTS_DIRECTORIES_DESCRIPTOR,
116
Arrays.asList("/path/to/objects"));
117
```
118
119
### VfHandler
120
121
Language version handler that provides parser instances for Visualforce analysis.
122
123
```java { .api }
124
/**
125
* Language version handler for Visualforce
126
* Implements PMD's LanguageVersionHandler interface
127
*/
128
public class VfHandler implements LanguageVersionHandler {
129
/**
130
* Create new handler with specified properties
131
* @param properties Configuration properties for parsing
132
*/
133
public VfHandler(VfLanguageProperties properties);
134
135
/**
136
* Get parser instance for Visualforce content
137
* @return VfParser configured with current properties
138
*/
139
@Override
140
public Parser getParser();
141
}
142
```
143
144
**Usage Example:**
145
146
```java
147
import net.sourceforge.pmd.lang.visualforce.*;
148
149
// Create language properties and handler
150
VfLanguageProperties properties = new VfLanguageProperties();
151
VfHandler handler = new VfHandler(properties);
152
153
// Get parser from handler
154
Parser parser = handler.getParser();
155
```
156
157
### Copy-Paste Detection
158
159
Specialized lexer for detecting code duplication in Visualforce files.
160
161
```java { .api }
162
/**
163
* CPD lexer for Visualforce files
164
* Previously known as VfTokenizer in PMD 6
165
* Extends JavaccCpdLexer for token-based copy-paste detection
166
*/
167
public class VfCpdLexer extends JavaccCpdLexer {
168
/**
169
* Create token manager for Visualforce content
170
* @param doc Text document to tokenize
171
* @return TokenManager for Visualforce tokens
172
*/
173
@Override
174
protected TokenManager<JavaccToken> makeLexerImpl(TextDocument doc);
175
}
176
```
177
178
**Usage Example:**
179
180
```java
181
import net.sourceforge.pmd.lang.visualforce.cpd.VfCpdLexer;
182
import net.sourceforge.pmd.lang.document.TextDocument;
183
184
// Create CPD lexer for copy-paste detection
185
VfCpdLexer lexer = new VfCpdLexer();
186
187
// Use with PMD's CPD engine to detect duplicated Visualforce markup
188
// (typically handled automatically by PMD framework)
189
```
190
191
## Service Registration
192
193
The Visualforce language module is automatically registered with PMD through Java's ServiceLoader mechanism:
194
195
- **Service file**: `META-INF/services/net.sourceforge.pmd.lang.Language`
196
- **Implementation**: `net.sourceforge.pmd.lang.visualforce.VfLanguageModule`
197
198
This enables automatic discovery and registration when the pmd-visualforce JAR is on the classpath.