Skip to content
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

review: fix: setVisibility should only be used with private, public or protected #1705

Merged
merged 3 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/spoon/support/reflect/CtModifierHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package spoon.support.reflect;

import spoon.SpoonException;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
Expand Down Expand Up @@ -104,6 +105,9 @@ public boolean removeModifier(ModifierKind modifier) {
}

public CtModifierHandler setVisibility(ModifierKind visibility) {
if (visibility != ModifierKind.PUBLIC && visibility != ModifierKind.PROTECTED && visibility != ModifierKind.PRIVATE) {
throw new SpoonException("setVisibility could only be called with a private, public or protected argument value. Given argument: " + visibility);
}
if (hasModifier(visibility)) {
return this;
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/spoon/test/modifiers/TestModifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import org.junit.Test;
import spoon.Launcher;
import spoon.SpoonException;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.test.modifiers.testclasses.MethodVarArgs;
import spoon.test.modifiers.testclasses.StaticMethod;

import java.util.Collections;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class TestModifiers {

Expand All @@ -33,4 +36,27 @@ public void testMethodWithVarargsDoesNotBecomeTransient() {
spoon.getEnvironment().setShouldCompile(true);
spoon.run();
}

@Test
public void testSetVisibility() {
// contract: setVisibility should only work with public/private/protected modifiers

Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/StaticMethod.java");
spoon.buildModel();

CtType<?> myClass = spoon.getFactory().Type().get(StaticMethod.class);
CtMethod methodPublicStatic = myClass.getMethodsByName("maMethod").get(0);

assertEquals(ModifierKind.PUBLIC, methodPublicStatic.getVisibility());
methodPublicStatic.setVisibility(ModifierKind.PROTECTED);
assertEquals(ModifierKind.PROTECTED, methodPublicStatic.getVisibility());
try {
methodPublicStatic.setVisibility(ModifierKind.FINAL);
fail();
} catch (SpoonException e) {
}

assertEquals(ModifierKind.PROTECTED, methodPublicStatic.getVisibility());
}
}
11 changes: 11 additions & 0 deletions src/test/java/spoon/test/modifiers/testclasses/StaticMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package spoon.test.modifiers.testclasses;

public class StaticMethod {
public static int maMethod() {
return 42;
}

private static int anotherMethod() {
return 42;
}
}