Skip to content

Commit

Permalink
refactor: simplify 'if' statement (#2392)
Browse files Browse the repository at this point in the history
* refactor: simplify 'if' in CtExecutableReferenceImpl.java

* refactor: simplify 'if' in CtParameterRemoveRefactoring.java

* refactor: simplify 'if' in CtTypeParameterReferenceImpl.java

* refactor: simplify 'if' in DefaultJavaPrettyPrinter.java

* refactor: simplify 'if' in ForEachNode.java

* refactor: simplify 'if' in ForEachNode.java

* refactor: simplify 'if' in SwitchNode.java

* refactor: simplify 'if' in SubInheritanceHierarchyResolver.java

* refactor: simplify 'if' in PatternPrinter.java

* refactor: simplify 'if' in MinimalImportScanner.java

* refactor: simplify 'if' in MapEntryNode.java

* refactor: simplify 'if' in ImportScannerImpl.java

* refactor: simplify 'if' in Parameters.java

* refactor: simplify 'if' in VisitorPartialEvaluator.java

* refactor: simplify 'if' in TemplateBuilder.java

* refactor: simplify 'if' in CtTypeImpl.java

* refactor: simplify 'if' in MMMethodKind.java

* refactor: fix checkstyle in CtTypeImpl.java

* refactor: remove redundant try-catch

* Remove leftover from 'catch'
  • Loading branch information
zielint0 authored and surli committed Aug 20, 2018
1 parent e4e2a02 commit 88fe903
Show file tree
Hide file tree
Showing 17 changed files with 26 additions and 90 deletions.
12 changes: 3 additions & 9 deletions src/main/java/spoon/metamodel/MMMethodKind.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public enum MMMethodKind {
ADD_FIRST(0, true, 10, m -> {
if (m.getParameters().size() == 1) {
if (m.getSimpleName().startsWith("add") || m.getSimpleName().startsWith("insert")) {
if (m.getSimpleName().endsWith("AtTop") || m.getSimpleName().endsWith("Begin")) {
return true;
}
return m.getSimpleName().endsWith("AtTop") || m.getSimpleName().endsWith("Begin");
}
}
return false;
Expand All @@ -55,9 +53,7 @@ public enum MMMethodKind {
*/
ADD_LAST(0, true, 1, m -> {
if (m.getParameters().size() == 1) {
if (m.getSimpleName().startsWith("add") || m.getSimpleName().startsWith("insert")) {
return true;
}
return m.getSimpleName().startsWith("add") || m.getSimpleName().startsWith("insert");
}
return false;
}),
Expand All @@ -66,9 +62,7 @@ public enum MMMethodKind {
*/
ADD_ON(1, true, 1, m -> {
if (m.getParameters().size() == 2 && "int".equals(m.getParameters().get(0).getType().getSimpleName())) {
if (m.getSimpleName().startsWith("add") || m.getSimpleName().startsWith("insert")) {
return true;
}
return m.getSimpleName().startsWith("add") || m.getSimpleName().startsWith("insert");
}
return false;
}),
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/spoon/pattern/internal/PatternPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ private boolean isCommentVisible(Object obj) {
if (obj instanceof CtElement) {
MetamodelConcept mmType = Metamodel.getInstance().getConcept((Class) obj.getClass());
MetamodelProperty mmCommentField = mmType.getProperty(CtRole.COMMENT);
if (mmCommentField != null && mmCommentField.isDerived() == false) {
return true;
}
return mmCommentField != null && mmCommentField.isDerived() == false;
}
return false;
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/spoon/pattern/internal/node/ForEachNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ public boolean replaceNode(RootNode oldNode, RootNode newNode) {
nestedModel = newNode;
return true;
}
if (nestedModel.replaceNode(oldNode, newNode)) {
return true;
}
return false;
return nestedModel.replaceNode(oldNode, newNode);
}

@Override
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/spoon/pattern/internal/node/MapEntryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ public boolean replaceNode(RootNode oldNode, RootNode newNode) {
if (key.replaceNode(oldNode, newNode)) {
return true;
}
if (value.replaceNode(oldNode, newNode)) {
return true;
}
return false;
return value.replaceNode(oldNode, newNode);
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/spoon/pattern/internal/node/SwitchNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ public boolean replaceNode(RootNode oldNode, RootNode newNode) {
statement = newNode;
return true;
}
if (statement.replaceNode(oldNode, newNode)) {
return true;
}
return statement.replaceNode(oldNode, newNode);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,7 @@ private void checkInvocation(CtInvocation<?> invocation) {
* @return true if it is allowed parameter use
*/
protected boolean isAllowedParameterUsage(CtParameterReference<?> paramRef) {
if (isRemovedParamOfRefactoredInvocation(paramRef)) {
return true;
}
return false;
return isRemovedParamOfRefactoredInvocation(paramRef);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,15 +909,12 @@ private <T> boolean isInitializeStaticFinalField(CtExpression<T> targetExp) {
} catch (ParentNotInitializedException e) {
return false;
}
if (parent instanceof CtFieldWrite
return parent instanceof CtFieldWrite
&& targetExp.equals(((CtFieldWrite) parent).getTarget())
&& anonymousParent != null
&& ((CtFieldWrite) parent).getVariable() != null
&& ((CtFieldWrite) parent).getVariable().getModifiers().contains(ModifierKind.STATIC)
&& ((CtFieldWrite) parent).getVariable().getModifiers().contains(ModifierKind.FINAL)) {
return true;
}
return false;
&& ((CtFieldWrite) parent).getVariable().getModifiers().contains(ModifierKind.FINAL);
}

@Override
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/spoon/reflect/visitor/ImportScannerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,7 @@ protected boolean isImportedInClassImports(CtTypeReference<?> ref) {

if (!(ref.isImplicit()) && classImports.containsKey(ref.getSimpleName())) {
CtTypeReference<?> exist = classImports.get(ref.getSimpleName());
if (exist.getQualifiedName().equals(ref.getQualifiedName())) {
return true;
}
return exist.getQualifiedName().equals(ref.getQualifiedName());
}
return false;
}
Expand Down Expand Up @@ -609,11 +607,7 @@ protected boolean isImportedInMethodImports(CtExecutableReference<?> ref) {

if (!(ref.isImplicit()) && methodImports.containsKey(ref.getSimpleName())) {
CtExecutableReference<?> exist = methodImports.get(ref.getSimpleName());
if (getSignature(exist).equals(
getSignature(ref))
) {
return true;
}
return getSignature(exist).equals(getSignature(ref));
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ protected boolean addMethodImport(CtExecutableReference ref) {
protected boolean isImportedInClassImports(CtTypeReference<?> ref) {
if (!(ref.isImplicit()) && classImports.containsKey(ref.getSimpleName())) {
CtTypeReference<?> exist = classImports.get(ref.getSimpleName());
if (exist.getQualifiedName().equals(ref.getQualifiedName())) {
return true;
}
return exist.getQualifiedName().equals(ref.getQualifiedName());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,7 @@ public JDTBasedSpoonCompiler(Factory factory) {
}

private void initializeCUCOmparator() {
try {
if (System.getenv("SPOON_SEED_CU_COMPARATOR") != null) {
this.sortList = false;
} else {
this.sortList = true;
}
} catch (NumberFormatException | SecurityException e) {
Launcher.LOGGER.error("Error while parsing Spoon seed for CU sorting", e);
this.sortList = true;
}

this.sortList = System.getenv("SPOON_SEED_CU_COMPARATOR") == null;
}

@Override
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -785,14 +785,10 @@ private boolean isSameParameter(CtMethod<?> method, CtTypeReference<?> ctParamet
}
if (expectedType instanceof CtTypeParameterReference && ctParameterType instanceof CtTypeParameterReference) {
// both types are generic
if (!ctParameterType.equals(expectedType)) {
return false;
}
return ctParameterType.equals(expectedType);
} else if (expectedType instanceof CtTypeParameterReference) {
// expectedType type is generic, ctParameterType is real type
if (!expectedType.getTypeErasure().getQualifiedName().equals(ctParameterType.getQualifiedName())) {
return false;
}
return expectedType.getTypeErasure().getQualifiedName().equals(ctParameterType.getQualifiedName());
} else if (ctParameterType instanceof CtTypeParameterReference) {
// ctParameterType is generic, expectedType type is real type
CtTypeParameter declaration = (CtTypeParameter) ctParameterType.getDeclaration();
Expand All @@ -806,10 +802,9 @@ private boolean isSameParameter(CtMethod<?> method, CtTypeReference<?> ctParamet
return declaration.getSuperclass().equals(expectedType);
} else {
return getFactory().Type().objectType().equals(expectedType);
}
} else if (!expectedType.getQualifiedName().equals(ctParameterType.getQualifiedName())) {
// both are real types
return false;
} // both are real types
} else {
return expectedType.getQualifiedName().equals(ctParameterType.getQualifiedName());
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,7 @@ private boolean isLiteralType(Object object) {
if (object instanceof Character) {
return true;
}
if (object instanceof Class) {
return true;
}
return false;
return object instanceof Class;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ public boolean isOverriding(CtExecutableReference<?> executable) {
return false;
}
CtTypeReference<?> declaringType = getDeclaringType();
if (declaringType == null || !declaringType.isSubtypeOf(executable.getDeclaringType())) {
return false;
}
return true;
return declaringType != null && declaringType.isSubtypeOf(executable.getDeclaringType());
}
if (exec instanceof CtMethod<?> && thisExec instanceof CtMethod<?>) {
return new ClassTypingContext(((CtTypeMember) thisExec).getDeclaringType()).isOverriding((CtMethod<?>) thisExec, (CtMethod<?>) exec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,6 @@ public boolean isGenerics() {
if (getDeclaration() instanceof CtTypeParameter) {
return true;
}
if (getBoundingType() != null && getBoundingType().isGenerics()) {
return true;
}
return false;
return getBoundingType() != null && getBoundingType().isGenerics();
}
}
7 changes: 2 additions & 5 deletions src/main/java/spoon/support/template/Parameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,8 @@ public static boolean isParameterSource(CtFieldReference<?> ref) {
//the reference to this is not template parameter
return false;
}
if (ref.getType().isSubtypeOf(getTemplateParameterType(ref.getFactory()))) {
//the type of template field is TemplateParameter.
return true;
}
return false;
//the type of template field is TemplateParameter.
return ref.getType().isSubtypeOf(getTemplateParameterType(ref.getFactory()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ public void accept(CtType<?> type) {
private static final Filter<CtType<?>> typeFilter = new Filter<CtType<?>>() {
@Override
public boolean matches(CtType<?> type) {
if (type instanceof CtTypeParameter) {
return false;
}
return true;
return !(type instanceof CtTypeParameter);
}
};

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/spoon/template/TemplateBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ public static TemplateBuilder createPattern(CtElement templateRoot, CtClass<?> t
return false;
}
//remove all Fields of type TemplateParameter
if (typeMember instanceof CtField<?> && ((CtField<?>) typeMember).getType().isSubtypeOf(templateParamRef)) {
return false;
}
//all other type members have to be part of the pattern model
return true;
return !(typeMember instanceof CtField<?>) || !((CtField<?>) typeMember).getType().isSubtypeOf(templateParamRef);
});
//remove `... extends Template`, which doesn't have to be part of pattern model
tv.removeSuperClass();
Expand Down

0 comments on commit 88fe903

Please sign in to comment.