|
| 1 | +package net.sf.eclipsecs.core.config.meta; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import com.puppycrawl.tools.checkstyle.PackageNamesLoader; |
| 11 | +import com.puppycrawl.tools.checkstyle.PackageObjectFactory; |
| 12 | +import com.puppycrawl.tools.checkstyle.api.AbstractCheck; |
| 13 | +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; |
| 14 | +import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck; |
| 15 | +import com.puppycrawl.tools.checkstyle.utils.JavadocUtil; |
| 16 | +import com.puppycrawl.tools.checkstyle.utils.TokenUtil; |
| 17 | + |
| 18 | +import net.sf.eclipsecs.core.CheckstylePlugin; |
| 19 | + |
| 20 | +public final class CheckUtil { |
| 21 | + private CheckUtil() { |
| 22 | + } |
| 23 | + |
| 24 | + public static String getModifiableTokens(String checkName) { |
| 25 | + final Object checkResult = getCheck(checkName); |
| 26 | + String result = null; |
| 27 | + if (AbstractJavadocCheck.class.isAssignableFrom(checkResult.getClass())) { |
| 28 | + final AbstractJavadocCheck javadocCheck = (AbstractJavadocCheck) checkResult; |
| 29 | + final List<Integer> modifiableJavadocTokens = subtractTokens(javadocCheck.getAcceptableJavadocTokens(), |
| 30 | + javadocCheck.getRequiredJavadocTokens()); |
| 31 | + result = getTokens(JavadocUtil::getTokenName, modifiableJavadocTokens); |
| 32 | + } |
| 33 | + else if (AbstractCheck.class.isAssignableFrom(checkResult.getClass())) { |
| 34 | + final AbstractCheck check = (AbstractCheck) checkResult; |
| 35 | + final List<Integer> modifiableTokens = subtractTokens(check.getAcceptableTokens(), |
| 36 | + check.getRequiredTokens()); |
| 37 | + result = getTokens(TokenUtil::getTokenName, modifiableTokens); |
| 38 | + } |
| 39 | + else { |
| 40 | + throw new IllegalStateException("Exception caused in CheckUtil.getCheck, " |
| 41 | + + "method executed in wrong context, heirarchy of check class missing"); |
| 42 | + } |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + private static AbstractCheck getCheck(String checkName) { |
| 47 | + final ClassLoader classLoader = CheckstylePlugin.getDefault() |
| 48 | + .getAddonExtensionClassLoader(); |
| 49 | + try { |
| 50 | + final Set<String> packageNames = PackageNamesLoader.getPackageNames(classLoader); |
| 51 | + return (AbstractCheck) new PackageObjectFactory(packageNames, classLoader) |
| 52 | + .createModule(checkName); |
| 53 | + } |
| 54 | + catch (CheckstyleException ex) { |
| 55 | + throw new IllegalStateException("exception occured during load of " + checkName, ex); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static List<Integer> subtractTokens(int[] tokens, int... requiredTokens) { |
| 60 | + Set<Integer> requiredTokensSet = new HashSet<>(Arrays.stream(requiredTokens) |
| 61 | + .boxed() |
| 62 | + .collect(Collectors.toList())); |
| 63 | + return Arrays.stream(tokens) |
| 64 | + .boxed() |
| 65 | + .filter(token -> !requiredTokensSet.contains(token)) |
| 66 | + .collect(Collectors.toList()); |
| 67 | + } |
| 68 | + |
| 69 | + private static String getTokens(Function<Integer, String> function, List<Integer> modifiableTokens) { |
| 70 | + return modifiableTokens.stream() |
| 71 | + .map(tokenInteger -> function.apply(tokenInteger)) |
| 72 | + .collect(Collectors.joining(",")); |
| 73 | + } |
| 74 | +} |
| 75 | + |
0 commit comments