Skip to content

Commit

Permalink
Pull checkstyle#242: Metadata Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurabdg committed Aug 18, 2020
1 parent 11bf406 commit 6eb6e0a
Show file tree
Hide file tree
Showing 10 changed files with 422 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
*/target/
metadata-gen/
net.sf.eclipsecs.core/lib/metadata-gen-1.0-SNAPSHOT.jar
.idea/
*.iml
*.prefs
.checkstyle
2 changes: 1 addition & 1 deletion net.sf.eclipsecs.checkstyle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
3 changes: 3 additions & 0 deletions net.sf.eclipsecs.core/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="lib/dom4j-2.1.3.jar"/>
<classpathentry exported="true" kind="lib" path="lib/metadata-gen-1.0-SNAPSHOT.jar"/>
<classpathentry exported="true" kind="lib" path="lib/reflections-0.9.10.jar"/>
<classpathentry exported="true" kind="lib" path="lib/snakeyaml-1.26.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
5 changes: 4 additions & 1 deletion net.sf.eclipsecs.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ Bundle-SymbolicName: net.sf.eclipsecs.core;singleton:=true
Bundle-Version: 8.34.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ClassPath: .,
lib/dom4j-2.1.3.jar
lib/dom4j-2.1.3.jar,
lib/snakeyaml-1.26.jar,
lib/reflections-0.9.10.jar,
lib/metadata-gen-1.0-SNAPSHOT.jar
Bundle-Activator: net.sf.eclipsecs.core.CheckstylePlugin
Bundle-ActivationPolicy: lazy
Bundle-Vendor: Eclipse Checkstyle Project
Expand Down
6 changes: 5 additions & 1 deletion net.sf.eclipsecs.core/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ bin.includes = .,\
plugin.xml,\
schema/,\
core.properties,\
lib/dom4j-2.1.3.jar
lib/dom4j-2.1.3.jar, \
lib/reflections-0.9.10.jar, \
lib/snakeyaml-1.26.jar, \
lib/metadata-gen-1.0-SNAPSHOT.jar

source.. = src/

Binary file added net.sf.eclipsecs.core/lib/reflections-0.9.10.jar
Binary file not shown.
Binary file added net.sf.eclipsecs.core/lib/snakeyaml-1.26.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package net.sf.eclipsecs.core.config.meta;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
import com.puppycrawl.tools.checkstyle.PackageObjectFactory;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;

import net.sf.eclipsecs.core.CheckstylePlugin;

public final class CheckUtil {
private CheckUtil() {
}

public static String getModifiableTokens(String checkName) {
final Object checkResult = getCheck(checkName);
String result = null;
if (AbstractJavadocCheck.class.isAssignableFrom(checkResult.getClass())) {
final AbstractJavadocCheck javadocCheck = (AbstractJavadocCheck) checkResult;
final List<Integer> modifiableJavadocTokens = subtractTokens(javadocCheck.getAcceptableJavadocTokens(),
javadocCheck.getRequiredJavadocTokens());
result = getTokens(JavadocUtil::getTokenName, modifiableJavadocTokens);
}
else if (AbstractCheck.class.isAssignableFrom(checkResult.getClass())) {
final AbstractCheck check = (AbstractCheck) checkResult;
final List<Integer> modifiableTokens = subtractTokens(check.getAcceptableTokens(),
check.getRequiredTokens());
result = getTokens(TokenUtil::getTokenName, modifiableTokens);
}
else {
throw new IllegalStateException("Exception caused in CheckUtil.getCheck, "
+ "method executed in wrong context, heirarchy of check class missing");
}
return result;
}

private static AbstractCheck getCheck(String checkName) {
final ClassLoader classLoader = CheckstylePlugin.getDefault()
.getAddonExtensionClassLoader();
try {
final Set<String> packageNames = PackageNamesLoader.getPackageNames(classLoader);
return (AbstractCheck) new PackageObjectFactory(packageNames, classLoader)
.createModule(checkName);
}
catch (CheckstyleException ex) {
throw new IllegalStateException("exception occured during load of " + checkName, ex);
}
}

private static List<Integer> subtractTokens(int[] tokens, int... requiredTokens) {
Set<Integer> requiredTokensSet = new HashSet<>(Arrays.stream(requiredTokens)
.boxed()
.collect(Collectors.toList()));
return Arrays.stream(tokens)
.boxed()
.filter(token -> !requiredTokensSet.contains(token))
.collect(Collectors.toList());
}

private static String getTokens(Function<Integer, String> function, List<Integer> modifiableTokens) {
return modifiableTokens.stream()
.map(tokenInteger -> function.apply(tokenInteger))
.collect(Collectors.joining(","));
}
}

Loading

0 comments on commit 6eb6e0a

Please sign in to comment.