-
-
Notifications
You must be signed in to change notification settings - Fork 376
refactor: Excluding decompiler related features from spoon-core #2766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b43b781
refactor: Excluding decopiler related feature from spoon-core
nharrand 19640c5
moving decompiler dependencies
nharrand 464e2ce
code duplication...
nharrand 6534cb7
pseudo update doc
nharrand aa9a73c
add travis build
nharrand db3ae9d
add travis build
nharrand 350116d
script permission update
nharrand d4db63e
attempt at fixing travis
nharrand afc6ebb
create spoon-pom
nharrand e3b0440
Merge branch 'master' of https://github.com/INRIA/spoon into exclude-…
nharrand 08a686e
spoon-pom
nharrand db3801f
stubbornness
nharrand d1c199b
using maven central cfr
nharrand 131991e
update poms
nharrand dc82592
update poms
nharrand f89d53a
more migration between poms
nharrand d90f56c
yet another try
nharrand a239ce3
add maven jxr to get rid of a warning
nharrand 3fd568a
renaming spoon-bytecode to spoon-decompiler
nharrand 6e40f85
update
nharrand f8717dd
add README to spoon-decompiler
nharrand e5c12b2
up
monperrus 19fc41a
up
monperrus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
code duplication...
- Loading branch information
commit 464e2ced3ac1c5a1de3543554063501ac5dabdcc
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
187 changes: 187 additions & 0 deletions
187
spoon-bytecode/src/test/java/spoon/test/architecture/SpoonArchitectureEnforcerTest.java
This file contains hidden or 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,187 @@ | ||
| package spoon.test.architecture; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.junit.Test; | ||
| import spoon.Launcher; | ||
| import spoon.SpoonAPI; | ||
| import spoon.processing.AbstractProcessor; | ||
| import spoon.reflect.code.CtCodeElement; | ||
| import spoon.reflect.code.CtConstructorCall; | ||
| import spoon.reflect.declaration.*; | ||
| import spoon.reflect.reference.CtTypeReference; | ||
| import spoon.reflect.visitor.filter.AbstractFilter; | ||
| import spoon.reflect.visitor.filter.TypeFilter; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| public class SpoonArchitectureEnforcerTest { | ||
|
|
||
| // this test contains all the architectural rules that are valid for the whole src/main/java | ||
| // we put them in the same test in order to only build the full model once | ||
| @Test | ||
| public void testSrcMainJava() { | ||
| Launcher spoon = new Launcher(); | ||
| spoon.getEnvironment().setCommentEnabled(true); | ||
| spoon.addInputResource("src/main/java/"); | ||
|
|
||
| // contract: all non-trivial public methods should be documented with proper API Javadoc | ||
| spoon.buildModel(); | ||
| List<String> notDocumented = new ArrayList<>(); | ||
| for (CtMethod method : spoon.getModel().getElements(new TypeFilter<>(CtMethod.class))) { | ||
|
|
||
| // now we see whether this should be documented | ||
| if (method.hasModifier(ModifierKind.PUBLIC) // public methods should be documented | ||
| && !method.getSimpleName().startsWith("get") // all kinds of setters can be undocumented | ||
| && !method.getSimpleName().startsWith("set") | ||
| && !method.getSimpleName().startsWith("is") | ||
| && !method.getSimpleName().startsWith("add") | ||
| && !method.getSimpleName().startsWith("remove") | ||
| && method.getTopDefinitions().isEmpty() // only the top declarations should be documented (not the overriding methods which are lower in the hierarchy) | ||
| && ( | ||
| method.hasModifier(ModifierKind.ABSTRACT) // all interface methods and abstract class methods must be documented | ||
|
|
||
| // GOOD FIRST ISSUE | ||
| // ideally we want that **all** public methods are documented | ||
| // so far, we have this arbitrary limit in the condition below (35) | ||
| // because it's a huge task to document everything at once | ||
| // so to contribute to Spoon, what you can do is | ||
| // 1) you lower the threshold (eg 33) | ||
| // 2) you run test `documentedTest`, it will output a list on undocumented methods | ||
| // 3) you document those methods | ||
| // 4) you run the test again to check that it passes | ||
| // 4) you commit your changes and create the corresponding pull requests | ||
| || method.filterChildren(new TypeFilter<>(CtCodeElement.class)).list().size() > 35 // means that only large methods must be documented | ||
| )) { | ||
|
|
||
| // OK it should be properly documented | ||
|
|
||
| // is it really well documented? | ||
| if (method.getDocComment().length() <= 15) { // the Javadoc must be at least at least 15 characters (still pretty short...) | ||
| notDocumented.add(method.getParent(CtType.class).getQualifiedName() + "#" + method.getSignature()); | ||
| } | ||
| } | ||
| } | ||
| if (!notDocumented.isEmpty()) { | ||
| fail(notDocumented.size() + " public methods should be documented with proper API documentation: \n" + StringUtils.join(notDocumented, "\n")); | ||
| } | ||
|
|
||
| // contract: Spoon's code never uses TreeSet constructor, because they implicitly depend on Comparable (no static check, only dynamic checks) | ||
| List<CtConstructorCall> treeSetWithoutComparators = spoon.getFactory().Package().getRootPackage().filterChildren(new AbstractFilter<CtConstructorCall>() { | ||
| @Override | ||
| public boolean matches(CtConstructorCall element) { | ||
| return element.getType().getActualClass().equals(TreeSet.class) && element.getArguments().isEmpty(); | ||
| } | ||
| }).list(); | ||
|
|
||
| assertEquals(0, treeSetWithoutComparators.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGoodTestClassNames() { | ||
| // contract: to be run by Maven surefire, all test classes must be called Test* or *Test | ||
| // reference: "By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:" | ||
| // "**/Test*.java" and "**/*Test.java" | ||
| // http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html | ||
| SpoonAPI spoon = new Launcher(); | ||
| spoon.addInputResource("src/test/java/"); | ||
| spoon.buildModel(); | ||
|
|
||
| for (CtMethod<?> meth : spoon.getModel().getElements(new TypeFilter<CtMethod>(CtMethod.class) { | ||
| @Override | ||
| public boolean matches(CtMethod element) { | ||
| return super.matches(element) && element.getAnnotation(Test.class) != null; | ||
| } | ||
| })) { | ||
| assertTrue("naming contract violated for " + meth.getParent(CtClass.class).getSimpleName(), meth.getParent(CtClass.class).getSimpleName().startsWith("Test") || meth.getParent(CtClass.class).getSimpleName().endsWith("Test")); | ||
| } | ||
|
|
||
| // contract: the Spoon test suite does not depend on Junit 3 classes and methods | ||
| // otherwise, intellij automatically selects the junit3 runner, finds nothing | ||
| // and crashes with a dirty exception | ||
| assertEquals(0, spoon.getModel().getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) { | ||
| @Override | ||
| public boolean matches(CtTypeReference element) { | ||
| CtMethod parent = element.getParent(CtMethod.class); | ||
| return "junit.framework.TestCase".equals(element.getQualifiedName()); | ||
| } | ||
| }).size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStaticClasses() { | ||
| // contract: helper classes only have static methods and a private constructor | ||
|
|
||
| SpoonAPI spoon = new Launcher(); | ||
| spoon.addInputResource("src/main/java/"); | ||
| spoon.buildModel(); | ||
|
|
||
| for (CtClass<?> klass : spoon.getModel().getElements(new TypeFilter<CtClass>(CtClass.class) { | ||
| @Override | ||
| public boolean matches(CtClass element) { | ||
| return element.getSuperclass() == null && super.matches(element) && !element.getMethods().isEmpty() | ||
| && element.getElements(new TypeFilter<>(CtMethod.class)).stream().allMatch(x -> x.hasModifier(ModifierKind.STATIC)); | ||
| } | ||
| })) { | ||
| assertTrue("Utility class " + klass.getQualifiedName() + " is missing private constructor", klass.getElements(new TypeFilter<>(CtConstructor.class)).stream().allMatch(x -> x.hasModifier(ModifierKind.PRIVATE))); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testSpecPackage() { | ||
| // contract: when a pull-request introduces a new package, it is made explicit during code review | ||
| // when a pull-request introduces a new package, this test fails and the author has to explicitly declare the new package here | ||
|
|
||
| Set<String> officialPackages = new TreeSet<>(); | ||
| officialPackages.add("spoon.decompiler"); | ||
| officialPackages.add("spoon"); | ||
| officialPackages.add(""); // root package | ||
|
|
||
| SpoonAPI spoon = new Launcher(); | ||
| spoon.addInputResource("src/main/java/"); | ||
| spoon.buildModel(); | ||
| final Set<String> currentPackages = new TreeSet<>(); | ||
| spoon.getModel().processWith(new AbstractProcessor<CtPackage>() { | ||
| @Override | ||
| public void process(CtPackage element) { | ||
| currentPackages.add(element.getQualifiedName()); | ||
| } | ||
| }); | ||
|
|
||
| assertSetEquals("you have created a new package or removed an existing one, please declare it explicitly in SpoonArchitectureEnforcerTest#testSpecPackage", officialPackages, currentPackages); | ||
| } | ||
|
|
||
| private static void assertSetEquals(String msg, Set<?> set1, Set<?> set2) { | ||
| if (set1 == null || set2 == null) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
|
|
||
| if (set1.size() != set2.size()) { | ||
| throw new AssertionError(msg + "\n\nDetails: " + computeDifference(set1, set2)); | ||
| } | ||
|
|
||
| if (!set1.containsAll(set2)) { | ||
| throw new AssertionError(msg + "\n\nDetails: " + computeDifference(set1, set2)); | ||
| } | ||
| } | ||
|
|
||
| private static String computeDifference(Set<?> set1, Set<?> set2) { | ||
| Set<String> results = new HashSet<>(); | ||
|
|
||
| for (Object o : set1) { | ||
| if (!set2.contains(o)) { | ||
| results.add("Missing package " + o + " in computed set"); | ||
| } else { | ||
| set2.remove(o); | ||
| } | ||
| } | ||
|
|
||
| for (Object o : set2) { | ||
| results.add("Package " + o + " presents in computed but not expected set."); | ||
| } | ||
| return StringUtils.join(results, "\n"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.