Skip to content

Commit

Permalink
Issue #145: update extractor to keep id same as in example if it was …
Browse files Browse the repository at this point in the history
…defined
  • Loading branch information
piyush kumar sadangi authored and romani committed Aug 15, 2024
1 parent 70fd33d commit 4aeced1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 106 deletions.
4 changes: 2 additions & 2 deletions IllegalType/all-examples-in-one/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@
</module>

<module name="IllegalType">
<property name="id" value="example7"/>
<property name="id" value="IllegalTypeOptionalAsField"/>
<property name="id" value="example7"/>
<property name="illegalClassNames" value="java.util.Optional"/>
<property name="tokens" value="VARIABLE_DEF"/>
</module>

<module name="SuppressionXpathSingleFilter">
<property name="id" value="example7"/>
<property name="id" value="IllegalTypeOptionalAsField"/>
<property name="id" value="example7"/>
<property name="query" value="//METHOD_DEF//*"/>
</module>
Expand Down
8 changes: 4 additions & 4 deletions PatternVariableName/all-examples-in-one/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@

<module name="PatternVariableName">
<property name="format" value="^[A-Z][A-Z0-9]*$"/>
<property name="id" value="example4"/>
<property name="id" value="FinalPatternVariableName"/>
<property name="id" value="example4"/>
</module>

<module name="PatternVariableName">
<property name="format" value="^([a-z][a-zA-Z0-9]*|_)$"/>
<property name="id" value="example4"/>
<property name="id" value="NonFinalPatternVariableName"/>
<property name="id" value="example4"/>
</module>

<module name="SuppressionXpathSingleFilter">
<property name="id" value="example4"/>
<property name="id" value="FinalPatternVariableName"/>
<property name="id" value="example4"/>
<property name="query" value="//PATTERN_VARIABLE_DEF[ not(./MODIFIERS/FINAL)]/IDENT"/>
</module>

<module name="SuppressionXpathSingleFilter">
<property name="id" value="example4"/>
<property name="id" value="NonFinalPatternVariableName"/>
<property name="id" value="example4"/>
<property name="query" value="//PATTERN_VARIABLE_DEF[ (./MODIFIERS/FINAL)]/IDENT"/>
</module>
Expand Down
8 changes: 8 additions & 0 deletions extractor/config/pmd/pmd-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@

<rule ref="category/java/multithreading.xml"/>


<rule ref="category/java/performance.xml">
<!-- AvoidInstantiatingObjectsInLoops is excluded because object creation within loops
is sometimes necessary for functionality, and the associated performance impact
Expand All @@ -100,4 +101,11 @@
</properties>
</rule>

<rule ref="category/java/errorprone.xml/EmptyCatchBlock">
<properties>
<property name="allowCommentedBlocks" value="true" />
<property name="allowExceptionNameRegex" value="^(ignored|expected)$" />
</properties>
</rule>

</ruleset>
160 changes: 60 additions & 100 deletions extractor/src/main/java/com/example/extractor/ConfigSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ public static String serializeConfigToString(final String exampleFilePath,
*/
public static String serializeAllInOneConfigToString(
final String[] exampleFilePaths,
final String templateFilePath)
throws Exception {
final String templateFilePath) throws Exception {
final List<Configuration> combinedChildren = new ArrayList<>();
boolean isTreeWalker = true;

Expand All @@ -279,9 +278,10 @@ public static String serializeAllInOneConfigToString(
if (targetModule != null) {
isTreeWalker &= isTreeWalkerConfig(xmlConfig);
for (final Configuration child : targetModule.getChildren()) {
final Configuration modifiedChild =
addIdProperty(child, "example" + (index + 1));
combinedChildren.add(modifiedChild);
// Use the new copyConfiguration method
final Configuration newChild =
copyConfiguration(child, "example" + (index + 1));
combinedChildren.add(newChild);
}
}
}
Expand All @@ -302,6 +302,33 @@ public static String serializeAllInOneConfigToString(
return TemplateProcessor.replacePlaceholders(template, combinedModuleContent, isTreeWalker);
}

/**
* Creates a deep copy of the given configuration.
*
* @param config the configuration to copy
* @param newId the new ID to assign to the copied configuration
* @return a new {@link Configuration} that is a deep copy of the provided configuration
* @throws CheckstyleException if copying fails
*/
private static Configuration copyConfiguration(final Configuration config, final String newId) {
final DefaultConfiguration newConfig = new DefaultConfiguration(config.getName());

for (final String name : config.getPropertyNames()) {
try {
final String value = config.getProperty(name);
newConfig.addProperty(name, value);
}
catch (CheckstyleException ex) {
// Property not found, skipping
}
}

// Set the new ID
newConfig.addProperty("id", newId);

return newConfig;
}

/**
* Builds the combined XML content for multiple Checkstyle module children.
*
Expand Down Expand Up @@ -346,21 +373,44 @@ private static String buildProperties(
final Configuration config,
final String indent) throws CheckstyleException {
final String[] propertyNames = config.getPropertyNames();
// Estimate 50 characters per property (name, value, XML tags)
final StringBuilder builder = new StringBuilder(propertyNames.length * 50);
final List<String> sortedPropertyNames = new ArrayList<>(Arrays.asList(propertyNames));
Collections.sort(sortedPropertyNames);

for (final String propertyName : sortedPropertyNames) {
final String propertyValue = config.getProperty(propertyName);
if (builder.length() > 0) {
builder.append('\n');
if ("id".equals(propertyName)) {
final List<String> idValues =
new ArrayList<>(Arrays.asList(propertyValue.split(",")));
Collections.sort(idValues);
for (final String value : idValues) {
appendProperty(builder, indent, propertyName, value.trim());
}
}
else {
appendProperty(builder, indent, propertyName, propertyValue);
}
builder.append(indent).append("<property name=\"").append(propertyName)
.append("\" value=\"").append(propertyValue).append("\"/>");
}
return builder.toString();
}

/**
* Appends a property in XML format to the provided StringBuilder.
*
* @param builder the StringBuilder to append to
* @param indent the indentation to apply before the property tag
* @param name the name of the property
* @param value the value of the property
*/
private static void appendProperty(final StringBuilder builder, final String indent,
final String name, final String value) {
if (builder.length() > 0) {
builder.append('\n');
}
builder.append(indent).append("<property name=\"").append(name)
.append("\" value=\"").append(value).append("\"/>");
}

/**
* Retrieves the target module from the given configuration,
* prioritizing the TreeWalker module if present.
Expand Down Expand Up @@ -484,94 +534,4 @@ private static String getSpecificModuleName(final Configuration config) {

return result;
}

/**
* Adds an "id" property to the given configuration.
*
* @param config The original configuration to be modified.
* @param idValue The value of the "id" property to be added.
* @return A new configuration with the added "id" property.
*/
private static Configuration addIdProperty(final Configuration config, final String idValue) {
return new IdPropertyAddingConfiguration(config, idValue);
}

/**
* This class is a decorator for the Configuration object.
* It adds an "id" property to the existing Configuration.
*/
private static class IdPropertyAddingConfiguration implements Configuration {
/**
* Identifier for serialized class version.
*/
private static final long serialVersionUID = 1L;

/**
* The original Configuration object that is being decorated.
*/
private final Configuration config;

/**
* The value of the "id" property to be added.
*/
private final String idValue;

/**
* Constructs an IdPropertyAddingConfiguration instance.
*
* @param config The original Configuration object to be decorated.
* @param idValue The value of the "id" property to be added.
*/
IdPropertyAddingConfiguration(final Configuration config, final String idValue) {
this.config = config;
this.idValue = idValue;
}

@Override
public String getName() {
return config.getName();
}

@Override
public Map<String, String> getMessages() {
return Collections.emptyMap();
}

@Override
public String[] getAttributeNames() {
return new String[0];
}

@Override
public String getAttribute(final String name) {
return null;
}

@Override
public String[] getPropertyNames() {
final List<String> propertyNames =
new ArrayList<>(Arrays.asList(config.getPropertyNames()));
propertyNames.add("id");
return propertyNames.toArray(new String[0]);
}

@Override
public String getProperty(final String name) throws CheckstyleException {
final String propertyValue;

if ("id".equals(name)) {
propertyValue = idValue;
}
else {
propertyValue = config.getProperty(name);
}

return propertyValue;
}

@Override
public Configuration[] getChildren() {
return config.getChildren();
}
}
}

0 comments on commit 4aeced1

Please sign in to comment.