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

refactor: code inspection: 'size() == 0' replaceable with 'isEmpty()' #2232

Merged
merged 1 commit into from
Jul 17, 2018
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: 2 additions & 2 deletions src/main/java/spoon/MavenLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,12 @@ class RangeVersion {
includeStart = range.startsWith("[");
includeEnd = range.endsWith("]");
String[] splitRange = range.substring(1, range.length() - 1).split(",");
if (splitRange[0].length() == 0) {
if (splitRange[0].isEmpty()) {
start = new Version("0.0.0.0");
} else {
start = new Version(splitRange[0]);
}
if (splitRange.length == 1 || splitRange[1].length() == 0) {
if (splitRange.length == 1 || splitRange[1].isEmpty()) {
end = new Version("99999999.9999999.999999.99999");
} else {
end = new Version(splitRange[1]);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/compiler/builder/SourceOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public T sources(String... sources) {

/** adds the given {@link spoon.compiler.SpoonFile} as sources */
public T sources(List<SpoonFile> sources) {
if (sources == null || sources.size() == 0) {
if (sources == null || sources.isEmpty()) {
args.add(".");
return myself;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/metamodel/MMMethodKind.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum MMMethodKind {
* Getter.
* T get()
*/
GET(-1, false, 1, m -> m.getParameters().size() == 0 && (m.getSimpleName().startsWith("get") || m.getSimpleName().startsWith("is"))),
GET(-1, false, 1, m -> m.getParameters().isEmpty() && (m.getSimpleName().startsWith("get") || m.getSimpleName().startsWith("is"))),
/**
* Setter
* void set(T)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/spoon/metamodel/MetamodelProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public CtTypeReference<?> getTypeofItems() {

public MMMethod getMethod(MMMethodKind kind) {
List<MMMethod> ms = getMethods(kind);
return ms.size() > 0 ? ms.get(0) : null;
return !ms.isEmpty() ? ms.get(0) : null;
}

public List<MMMethod> getMethods(MMMethodKind kind) {
Expand Down Expand Up @@ -297,7 +297,7 @@ void sortByBestMatch(MMMethodKind key) {
*/
private int getIdxOfBestMatch(List<MMMethod> methods, MMMethodKind key) {
MMMethod mmMethod = methods.get(0);
if (mmMethod.getActualCtMethod().getParameters().size() == 0) {
if (mmMethod.getActualCtMethod().getParameters().isEmpty()) {
return getIdxOfBestMatchByReturnType(methods, key);
} else {
MMMethod mmGetMethod = getMethod(MMMethodKind.GET);
Expand Down Expand Up @@ -540,7 +540,7 @@ public String toString() {
*/
public MetamodelProperty getSuperProperty() {
List<MetamodelProperty> potentialRootSuperFields = new ArrayList<>();
if (roleMethods.size() > 0) {
if (!roleMethods.isEmpty()) {
potentialRootSuperFields.add(this);
}
superProperties.forEach(superField -> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/pattern/PatternBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ protected Factory getFactory() {
if (templateTypeRef != null) {
return templateTypeRef.getFactory();
}
if (patternModel.size() > 0) {
if (!patternModel.isEmpty()) {
return patternModel.get(0).getFactory();
}
throw new SpoonException("PatternBuilder has no CtElement to provide a Factory");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ private void configureByTemplateParameter(CtType<?> templateType, Map<String, Ob
* Use it as Pattern parameter
*/
String fieldName = typeMember.getSimpleName();
String stringMarker = (param.value() != null && param.value().length() > 0) ? param.value() : fieldName;
String stringMarker = (param.value() != null && !param.value().isEmpty()) ? param.value() : fieldName;
//for the compatibility reasons with Parameters.getNamesToValues(), use the proxy name as parameter name
String parameterName = stringMarker;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/pattern/internal/DefaultGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private void addGeneratedByComment(CtElement ele, String generatedBy) {
String EOL = System.getProperty("line.separator");
CtComment comment = getJavaDoc(ele);
String content = comment.getContent();
if (content.trim().length() > 0) {
if (!content.trim().isEmpty()) {
content += EOL + EOL;
}
content += generatedBy;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/pattern/internal/PatternPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void addParameterCommentTo(CtElement ele, ParamOnElement... paramsOnElem
params.add(paramOnElement);
}
}
if (isCommentVisible(ele) && params.size() > 0) {
if (isCommentVisible(ele) && !params.isEmpty()) {
ele.addComment(ele.getFactory().Code().createComment(getSubstitutionRequestsDescription(ele, params), CommentType.BLOCK));
params.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public <T> T getValueAs(Factory factory, String parameterName, Object value, Cla
}
if (value instanceof List) {
List list = (List) value;
if (list.size() == 0) {
if (list.isEmpty()) {
return null;
}
if (list.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void scan(CtRole role, Collection<? extends CtElement> elements) {

private int searchMatchInList(CtRole role, List<? extends CtElement> list, boolean scanChildren) {
int matchCount = 0;
if (list.size() > 0) {
if (!list.isEmpty()) {
TobeMatched tobeMatched = TobeMatched.create(
new ImmutableMapImpl(),
ContainerKind.LIST,
Expand All @@ -77,7 +77,7 @@ private int searchMatchInList(CtRole role, List<? extends CtElement> list, boole
TobeMatched nextTobeMatched = pattern.matchAllWith(tobeMatched);
if (nextTobeMatched != null) {
List<?> matchedTargets = tobeMatched.getMatchedTargets(nextTobeMatched);
if (matchedTargets.size() > 0) {
if (!matchedTargets.isEmpty()) {
matchCount++;
//send information about match to client
matchConsumer.accept(new Match(matchedTargets, nextTobeMatched.getParameters()));
Expand All @@ -99,7 +99,7 @@ private int searchMatchInList(CtRole role, List<? extends CtElement> list, boole
}

private void searchMatchInSet(CtRole role, Set<? extends CtElement> set) {
if (set.size() > 0) {
if (!set.isEmpty()) {
//copy targets, because it might be modified by call of matchConsumer, when refactoring spoon model
//use List, because Spoon uses Sets with predictable order - so keep the order
TobeMatched tobeMatched = TobeMatched.create(
Expand All @@ -110,7 +110,7 @@ private void searchMatchInSet(CtRole role, Set<? extends CtElement> set) {
TobeMatched nextTobeMatched = pattern.matchAllWith(tobeMatched);
if (nextTobeMatched != null) {
List<?> matchedTargets = tobeMatched.getMatchedTargets(nextTobeMatched);
if (matchedTargets.size() > 0) {
if (!matchedTargets.isEmpty()) {
//send information about match to client
matchConsumer.accept(new Match(matchedTargets, nextTobeMatched.getParameters()));
//do not scan children of matched elements. They already matched, so we must not scan them again
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private boolean containsSame(List<?> items, Object object) {
* @return true if there is anything to match.
*/
public boolean hasTargets() {
return targets.size() > 0;
return !targets.isEmpty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected String getWrappedName(String containerName) {
if (name == null) {
return containerName;
}
if (containerName.length() > 0) {
if (!containerName.isEmpty()) {
containerName += ".";
}
return containerName + name;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/refactoring/Refactoring.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static CtMethod<?> copyMethod(final CtMethod<?> method) {
CtMethod<?> clone = method.clone();
String tentativeTypeName = method.getSimpleName() + "Copy";
CtType parent = method.getParent(CtType.class);
while (parent.getMethodsByName(tentativeTypeName).size() > 0) {
while (!parent.getMethodsByName(tentativeTypeName).isEmpty()) {
tentativeTypeName += "X";
}
final String cloneMethodName = tentativeTypeName;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/factory/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ public boolean matches(CtClass<T> element) {
return super.matches(element) && element.getQualifiedName().equals(qualifiedName);
}
});
if (enclosingClasses.size() == 0) {
if (enclosingClasses.isEmpty()) {
return null;
}
return enclosingClasses.get(0);
Expand All @@ -478,7 +478,7 @@ public boolean matches(CtNewClass element) {
return super.matches(element) && element.getAnonymousClass().getQualifiedName().equals(qualifiedName);
}
});
if (anonymousClasses.size() == 0) {
if (anonymousClasses.isEmpty()) {
return null;
}
return anonymousClasses.get(0).getAnonymousClass();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/visitor/CommentHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static void printComment(PrinterHelper printer, CtComment.CommentType commentTyp
printer.writeln();
}
} else {
if (com.length() > 0) {
if (!com.isEmpty()) {
printer.write(COMMENT_STAR + com).writeln();
} else {
printer.write(" *" /* no trailing space */ + com).writeln();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/visitor/CtIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean addAll(Collection c) {
for (Object aC : c) {
this.addFirst((CtElement) aC);
}
return c.size() > 0;
return !c.isEmpty();
}
};

Expand Down Expand Up @@ -74,7 +74,7 @@ public void scan(CtElement element) {

@Override
public boolean hasNext() {
return deque.size() > 0;
return !deque.isEmpty();
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ protected void enterCtStatement(CtStatement s) {
* Exits an expression.
*/
protected void exitCtExpression(CtExpression<?> e) {
while ((context.parenthesedExpression.size() > 0) && e == context.parenthesedExpression.peek()) {
while ((!context.parenthesedExpression.isEmpty()) && e == context.parenthesedExpression.peek()) {
context.parenthesedExpression.pop();
printer.writeSeparator(")");
}
Expand Down Expand Up @@ -387,7 +387,7 @@ private static void addParentPath(StringBuilder sb, CtElement ele) {
}

private boolean shouldSetBracket(CtExpression<?> e) {
if (e.getTypeCasts().size() != 0) {
if (!e.getTypeCasts().isEmpty()) {
return true;
}
try {
Expand Down Expand Up @@ -416,7 +416,7 @@ public <A extends Annotation> void visitCtAnnotation(CtAnnotation<A> annotation)
elementPrinterHelper.writeAnnotations(annotation);
printer.writeSeparator("@");
scan(annotation.getAnnotationType());
if (annotation.getValues().size() > 0) {
if (!annotation.getValues().isEmpty()) {
elementPrinterHelper.printList(annotation.getValues().entrySet(),
null, false, "(", false, false, ",", true, false, ")",
e -> {
Expand Down Expand Up @@ -687,7 +687,7 @@ public <T> void visitCtConstructor(CtConstructor<T> constructor) {
elementPrinterHelper.visitCtNamedElement(constructor, sourceCompilationUnit);
elementPrinterHelper.writeModifiers(constructor);
elementPrinterHelper.writeFormalTypeParameters(constructor);
if (constructor.getFormalCtTypeParameters().size() > 0) {
if (!constructor.getFormalCtTypeParameters().isEmpty()) {
printer.writeSpace();
}
if (constructor.getDeclaringType() != null) {
Expand Down Expand Up @@ -730,7 +730,7 @@ public <T extends Enum<?>> void visitCtEnum(CtEnum<T> ctEnum) {
context.pushCurrentThis(ctEnum);
printer.writeSpace().writeSeparator("{").incTab().writeln();

if (ctEnum.getEnumValues().size() == 0) {
if (ctEnum.getEnumValues().isEmpty()) {
printer.writeSeparator(";").writeln();
} else {
elementPrinterHelper.printList(ctEnum.getEnumValues(),
Expand Down Expand Up @@ -1151,7 +1151,7 @@ public void visitCtFor(CtFor forLoop) {
enterCtStatement(forLoop);
printer.writeKeyword("for").writeSpace().writeSeparator("(");
List<CtStatement> st = forLoop.getForInit();
if (st.size() > 0) {
if (!st.isEmpty()) {
scan(st.get(0));
}
if (st.size() > 1) {
Expand Down Expand Up @@ -1215,7 +1215,7 @@ public <T> void visitCtInterface(CtInterface<T> intrface) {
elementPrinterHelper.writeFormalTypeParameters(intrface);
}

if (intrface.getSuperInterfaces().size() > 0) {
if (!intrface.getSuperInterfaces().isEmpty()) {
elementPrinterHelper.printList(intrface.getSuperInterfaces(),
"extends", false, null, false, true, ",", true, false, null,
ref -> scan(ref));
Expand Down Expand Up @@ -1335,7 +1335,7 @@ public <T> void visitCtMethod(CtMethod<T> m) {
elementPrinterHelper.visitCtNamedElement(m, sourceCompilationUnit);
elementPrinterHelper.writeModifiers(m);
elementPrinterHelper.writeFormalTypeParameters(m);
if (m.getFormalCtTypeParameters().size() > 0) {
if (!m.getFormalCtTypeParameters().isEmpty()) {
printer.writeSpace();
}
try (Writable _context = context.modify().ignoreGenerics(false)) {
Expand Down Expand Up @@ -1413,7 +1413,7 @@ public <T> void visitCtNewArray(CtNewArray<T> newArray) {
ref = ((CtArrayTypeReference) ref).getComponentType();
}
}
if (newArray.getDimensionExpressions().size() == 0) {
if (newArray.getDimensionExpressions().isEmpty()) {
elementPrinterHelper.printList(newArray.getElements(),
null, false, "{", true, false, ",", true, true, "}",
e -> scan(e));
Expand Down Expand Up @@ -1459,7 +1459,7 @@ private <T> void printConstructorCall(CtConstructorCall<T> ctConstructorCall) {

printer.writeKeyword("new").writeSpace();

if (ctConstructorCall.getActualTypeArguments().size() > 0) {
if (!ctConstructorCall.getActualTypeArguments().isEmpty()) {
elementPrinterHelper.writeActualTypeArguments(ctConstructorCall);
}

Expand Down Expand Up @@ -1493,7 +1493,7 @@ private <T> boolean hasDeclaringTypeWithGenerics(CtTypeReference<T> reference) {
return false;
}
// If declaring type have generics, we return true.
if (reference.getDeclaringType().getActualTypeArguments().size() != 0) {
if (!reference.getDeclaringType().getActualTypeArguments().isEmpty()) {
return true;
}
// Checks if the declaring type has generic types.
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/spoon/reflect/visitor/ElementPrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void writeExtendsClause(CtType<?> type) {

/** writes the implemented interfaces with a ListPrinter */
public void writeImplementsClause(CtType<?> type) {
if (type.getSuperInterfaces().size() > 0) {
if (!type.getSuperInterfaces().isEmpty()) {
printList(type.getSuperInterfaces(), "implements",
false, null, false, true, ",", true, false, null,
ref -> prettyPrinter.scan(ref));
Expand All @@ -167,7 +167,7 @@ public void writeExecutableParameters(CtExecutable<?> executable) {

/** writes the thrown exception with a ListPrinter */
public void writeThrowsClause(CtExecutable<?> executable) {
if (executable.getThrownTypes().size() > 0) {
if (!executable.getThrownTypes().isEmpty()) {
printList(executable.getThrownTypes(), "throws",
false, null, false, false, ",", true, false, null,
ref -> prettyPrinter.scan(ref));
Expand Down Expand Up @@ -244,7 +244,7 @@ public void writeFormalTypeParameters(CtFormalTypeDeclarer ctFormalTypeDeclarer)
if (parameters == null) {
return;
}
if (parameters.size() > 0) {
if (!parameters.isEmpty()) {
printList(parameters,
null, false, "<", false, false, ",", true, false, ">",
parameter -> prettyPrinter.scan(parameter));
Expand All @@ -259,7 +259,7 @@ public void writeFormalTypeParameters(CtFormalTypeDeclarer ctFormalTypeDeclarer)
*/
public void writeActualTypeArguments(CtActualTypeContainer ctGenericElementReference) {
final Collection<CtTypeReference<?>> arguments = ctGenericElementReference.getActualTypeArguments();
if (arguments != null && arguments.size() > 0) {
if (arguments != null && !arguments.isEmpty()) {
printList(arguments.stream().filter(a -> !a.isImplicit())::iterator,
null, false, "<", false, false, ",", true, false, ">",
argument -> {
Expand Down Expand Up @@ -333,7 +333,7 @@ public void writeImports(Collection<CtImport> imports) {
printer.writeKeyword("import").writeSpace();
writeQualifiedName(importLine).writeSeparator(";").writeln();
}
if (setStaticImports.size() > 0) {
if (!setStaticImports.isEmpty()) {
if (isFirst) {
printer.writeln();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/visitor/JavaIdentifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public enum JavaIdentifiers {
}

static boolean isJavaIdentifier(String s) {
if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) {
if (s.isEmpty() || !Character.isJavaIdentifierStart(s.charAt(0))) {
return false;
}
for (int i = 1; i < s.length(); i++) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/spoon/reflect/visitor/ListPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ListPrinter(TokenWriter printerHelper, boolean startPrefixSpace, String s
if (startPrefixSpace) {
printerHelper.writeSpace();
}
if (start != null && start.length() > 0) {
if (start != null && !start.isEmpty()) {
printerTokenWriter.writeSeparator(start);
}
if (startSuffixSpace) {
Expand All @@ -71,7 +71,7 @@ public void printSeparatorIfAppropriate() {
if (nextPrefixSpace) {
printerTokenWriter.writeSpace();
}
if (separator != null && separator.length() > 0) {
if (separator != null && !separator.isEmpty()) {
printerTokenWriter.writeSeparator(separator);
}
if (nextSuffixSpace) {
Expand All @@ -85,7 +85,7 @@ public void close() {
if (endPrefixSpace) {
printerTokenWriter.writeSpace();
}
if (end != null && end.length() > 0) {
if (end != null && !end.isEmpty()) {
printerTokenWriter.writeSeparator(end);
}
}
Expand Down
Loading