forked from checkstyle/eclipse-cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull checkstyle#242: Metadata Integration
- Loading branch information
Showing
10 changed files
with
422 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,7 @@ | |
*/target/ | ||
metadata-gen/ | ||
net.sf.eclipsecs.core/lib/metadata-gen-1.0-SNAPSHOT.jar | ||
.idea/ | ||
*.iml | ||
*.prefs | ||
.checkstyle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,4 +87,4 @@ | |
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
75 changes: 75 additions & 0 deletions
75
net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/meta/CheckUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(",")); | ||
} | ||
} | ||
|
Oops, something went wrong.