Skip to content

Commit

Permalink
Flip operands of equals to avoid potential NPE
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellansun committed Oct 27, 2024
1 parent bfbdb06 commit 1ad5b8f
Show file tree
Hide file tree
Showing 57 changed files with 148 additions and 124 deletions.
6 changes: 3 additions & 3 deletions src/main/java/groovy/beans/BindableASTTransformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ protected boolean needsPropertyChangeSupport(ClassNode declaringClass, SourceUni
while (consideredClass!= null) {
for (MethodNode method : consideredClass.getMethods()) {
// just check length, MOP will match it up
foundAdd = foundAdd || method.getName().equals("addPropertyChangeListener") && method.getParameters().length == 1;
foundRemove = foundRemove || method.getName().equals("removePropertyChangeListener") && method.getParameters().length == 1;
foundFire = foundFire || method.getName().equals("firePropertyChange") && method.getParameters().length == 3;
foundAdd = foundAdd || "addPropertyChangeListener".equals(method.getName()) && method.getParameters().length == 1;
foundRemove = foundRemove || "removePropertyChangeListener".equals(method.getName()) && method.getParameters().length == 1;
foundFire = foundFire || "firePropertyChange".equals(method.getName()) && method.getParameters().length == 3;
if (foundAdd && foundRemove && foundFire) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/groovy/beans/VetoableASTTransformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ protected boolean needsVetoableChangeSupport(ClassNode declaringClass, SourceUni
while (consideredClass!= null) {
for (MethodNode method : consideredClass.getMethods()) {
// just check length, MOP will match it up
foundAdd = foundAdd || method.getName().equals("addVetoableChangeListener") && method.getParameters().length == 1;
foundRemove = foundRemove || method.getName().equals("removeVetoableChangeListener") && method.getParameters().length == 1;
foundFire = foundFire || method.getName().equals("fireVetoableChange") && method.getParameters().length == 3;
foundAdd = foundAdd || "addVetoableChangeListener".equals(method.getName()) && method.getParameters().length == 1;
foundRemove = foundRemove || "removeVetoableChangeListener".equals(method.getName()) && method.getParameters().length == 1;
foundFire = foundFire || "fireVetoableChange".equals(method.getName()) && method.getParameters().length == 3;
if (foundAdd && foundRemove && foundFire) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ private static void checkForConvenienceForm(final AnnotationNode node, final boo
for (Map.Entry<String, Object> entry : parts.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().toString();
if (!key.equals("version") || !value.equals("*") || !exclude) {
if (!"version".equals(key) || !"*".equals(value) || !exclude) {
node.addMember(key, constX(value));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/groovy/lang/GroovyClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ private static String decodeFileName(final String fileName) {
}

private static boolean isFile(final URL ret) {
return ret != null && ret.getProtocol().equals("file");
return ret != null && "file".equals(ret.getProtocol());
}

private static File getFileForUrl(final URL ret, final String filename) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/groovy/lang/GroovyRuntimeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected String getLocationText() {
if (module != null) {
answer += module.getDescription();
}
if (answer.equals(". ")) {
if (". ".equals(answer)) {
return "";
}
return answer;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/groovy/lang/GroovyShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public Object run(final File scriptFile, String[] args) throws CompilationFailed
String scriptName = scriptFile.getName();
int p = scriptName.lastIndexOf('.');
if (p++ >= 0) {
if (scriptName.substring(p).equals("java")) {
if ("java".equals(scriptName.substring(p))) {
throw new CompilationFailedException(0, null);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/groovy/lang/MetaClassImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ public Object invokeMethod(final Class sender, final Object object, final String
}

if (method != null) {
if (arguments.length == 0 && methodName.equals("clone") && method.getDeclaringClass() == ReflectionCache.OBJECT_CLASS) {
if (arguments.length == 0 && "clone".equals(methodName) && method.getDeclaringClass() == ReflectionCache.OBJECT_CLASS) {
throw method.processDoMethodInvokeException(new CloneNotSupportedException(), object, arguments);
}
MetaMethod transformedMetaMethod = VM_PLUGIN.transformMetaMethod(this, method);
Expand Down Expand Up @@ -3331,7 +3331,7 @@ protected static long handleMatches(long matchesDistance, LinkedList matches, Ob
}

private static boolean isGenericGetMethod(MetaMethod method) {
if (method.getName().equals("get")) {
if ("get".equals(method.getName())) {
CachedClass[] parameterTypes = method.getParameterTypes();
return parameterTypes.length == 1 && parameterTypes[0].getTheClass() == String.class;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/groovy/namespace/QName.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ public boolean matches(Object o) {
if (o == null) return false;
if (o instanceof QName) {
final QName qName = (QName) o;
if (!namespaceURI.equals(qName.namespaceURI) && !namespaceURI.equals("*") && !qName.namespaceURI.equals("*")) return false;
return localPart.equals(qName.localPart) || localPart.equals("*") || qName.localPart.equals("*");
if (!namespaceURI.equals(qName.namespaceURI) && !"*".equals(namespaceURI) && !"*".equals(qName.namespaceURI)) return false;
return localPart.equals(qName.localPart) || "*".equals(localPart) || "*".equals(qName.localPart);
} else if (o instanceof String) {
final String string = (String)o;
if (string.length() == 0) return false;
Expand All @@ -226,8 +226,8 @@ public boolean matches(Object o) {
if (lastColonIndex < 0 || lastColonIndex == string.length() - 1) return false;
final String stringPrefix = string.substring(0,lastColonIndex);
final String stringLocalPart = string.substring(lastColonIndex + 1);
if (stringPrefix.equals(prefix) || stringPrefix.equals(namespaceURI) || stringPrefix.equals("*")) {
return localPart.equals(stringLocalPart) || stringLocalPart.equals("*");
if (stringPrefix.equals(prefix) || stringPrefix.equals(namespaceURI) || "*".equals(stringPrefix)) {
return localPart.equals(stringLocalPart) || "*".equals(stringLocalPart);
}
}
return false;
Expand Down Expand Up @@ -287,4 +287,4 @@ public int hashCode() {
result = 29 * result + localPart.hashCode();
return result;
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/groovy/transform/builder/ExternalStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static MethodNode createBuildMethod(BuilderASTTransformation transform,
}

private MethodNode createBuilderMethodForField(ClassNode builderClass, PropertyInfo prop, String prefix) {
String propName = prop.getName().equals("class") ? "clazz" : prop.getName();
String propName = "class".equals(prop.getName()) ? "clazz" : prop.getName();
String setterName = getSetterName(prefix, prop.getName());
return new MethodNode(setterName, ACC_PUBLIC, newClass(builderClass), params(param(newClass(prop.getType()), propName)), NO_EXCEPTIONS, block(
stmt(assignX(propX(varX("this"), constX(propName)), varX(propName))),
Expand All @@ -147,14 +147,14 @@ private MethodNode createBuilderMethodForField(ClassNode builderClass, PropertyI

private static FieldNode createFieldCopy(ClassNode builderClass, PropertyInfo prop) {
String propName = prop.getName();
return new FieldNode(propName.equals("class") ? "clazz" : propName, ACC_PRIVATE, newClass(prop.getType()), builderClass, DEFAULT_INITIAL_VALUE);
return new FieldNode("class".equals(propName) ? "clazz" : propName, ACC_PRIVATE, newClass(prop.getType()), builderClass, DEFAULT_INITIAL_VALUE);
}

private static Expression initializeInstance(ClassNode sourceClass, List<PropertyInfo> props, BlockStatement body) {
Expression instance = localVarX("_the" + sourceClass.getNameWithoutPackage(), sourceClass);
body.addStatement(declS(instance, ctorX(sourceClass)));
for (PropertyInfo prop : props) {
body.addStatement(stmt(assignX(propX(instance, prop.getName()), varX(prop.getName().equals("class") ? "clazz" : prop.getName(), newClass(prop.getType())))));
body.addStatement(stmt(assignX(propX(instance, prop.getName()), varX("class".equals(prop.getName()) ? "clazz" : prop.getName(), newClass(prop.getType())))));
}
return instance;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/groovy/util/GroovyScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public static void main(String[] urls) throws Exception {
String line;
while (true) {
System.out.print("groovy> ");
if ((line = br.readLine()) == null || line.equals("quit")) {
if ((line = br.readLine()) == null || "quit".equals(line)) {
break;
}
try {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ public static boolean isSubtype(final ClassNode maybeExtendedOrImplemented, fina
//--------------------------------------------------------------------------

private static boolean isPackagePrivate(final AnnotatedNode aNode) {
return aNode.getAnnotations().stream().anyMatch(anno -> anno.getClassNode().getName().equals("groovy.transform.PackageScope"))
|| aNode.getDeclaringClass().getAnnotations().stream().anyMatch(anno -> anno.getClassNode().getName().equals("groovy.transform.PackageScope")
return aNode.getAnnotations().stream().anyMatch(anno -> "groovy.transform.PackageScope".equals(anno.getClassNode().getName()))
|| aNode.getDeclaringClass().getAnnotations().stream().anyMatch(anno -> "groovy.transform.PackageScope".equals(anno.getClassNode().getName())
&& Optional.ofNullable(anno.getMember("value")).filter(expr -> expr.getText().contains("FIELDS")).isPresent());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public static String createErrorMessage(final String className, final String fie
}

private static String prettyTypeName(final String name) {
return name.equals("java.lang.Object") ? name + " or def" : name;
return "java.lang.Object".equals(name) ? name + " or def" : name;
}

public static boolean isKnownImmutableType(final ClassNode fieldType, final List<String> knownImmutableClasses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static Visibility getVisForAnnotation(final Class<? extends AnnotatedNod
private static Visibility getVisibility(final Expression e) {
if (e instanceof PropertyExpression) {
PropertyExpression pe = (PropertyExpression) e;
if (pe.getObjectExpression() instanceof ClassExpression && pe.getObjectExpression().getText().equals("groovy.transform.options.Visibility")) {
if (pe.getObjectExpression() instanceof ClassExpression && "groovy.transform.options.Visibility".equals(pe.getObjectExpression().getText())) {
return Visibility.valueOf(pe.getPropertyAsString());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/codehaus/groovy/ast/ClassNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldInitializ
BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression();
if (bExp.getLeftExpression() instanceof FieldExpression) {
FieldExpression fExp = (FieldExpression) bExp.getLeftExpression();
if (fExp.getFieldName().equals("$VALUES")) {
if ("$VALUES".equals(fExp.getFieldName())) {
for (Statement initStmt : staticFieldInitializerStatements) {
it.add(initStmt);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/codehaus/groovy/ast/FieldNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void setInitialValueExpression(Expression initialValueExpression) {

@Override
public boolean equals(Object obj) {
if (obj != null && obj.getClass().getName().equals("org.codehaus.groovy.ast.decompiled.LazyFieldNode")) {
if (obj != null && "org.codehaus.groovy.ast.decompiled.LazyFieldNode".equals(obj.getClass().getName())) {
return obj.equals(this);
}
return super.equals(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void visit(final ASTNode[] nodes, final SourceUnit source) {
init(nodes, source);
AnnotationNode node = (AnnotationNode) nodes[0];
AnnotatedNode target = (AnnotatedNode) nodes[1];
if (!node.getClassNode().getName().equals("groovy.lang.Mixin"))
if (!"groovy.lang.Mixin".equals(node.getClassNode().getName()))
return;

Expression value = node.getMember("value");
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/codehaus/groovy/ast/ModuleNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ protected ClassNode createStatementsClass() {

private MethodNode findRun() {
for (MethodNode node : methods) {
if (node.getName().equals("run") && node.getParameters().length == 0) {
if ("run".equals(node.getName()) && node.getParameters().length == 0) {
return node;
}
}
Expand All @@ -505,14 +505,14 @@ private MethodNode handleMainMethodIfPresent(final List<MethodNode> methods) {
boolean foundStatic = false;
MethodNode result = null;
for (MethodNode node : methods) {
if (node.getName().equals("main") && !node.isPrivate()) {
if ("main".equals(node.getName()) && !node.isPrivate()) {
int numParams = node.getParameters().length;
if (numParams < 2) {
ClassNode argType = numParams > 0 ? node.getParameters()[0].getType() : null;
ClassNode retType = node.getReturnType();

boolean argTypeMatches = argType == null || argType.getNameWithoutPackage().equals("Object") || (argType.isArray() && argType.getComponentType().getNameWithoutPackage().equals("String"));
boolean retTypeMatches = ClassHelper.isPrimitiveVoid(retType) || retType.getNameWithoutPackage().equals("Object");
boolean argTypeMatches = argType == null || "Object".equals(argType.getNameWithoutPackage()) || (argType.isArray() && "String".equals(argType.getComponentType().getNameWithoutPackage()));
boolean retTypeMatches = ClassHelper.isPrimitiveVoid(retType) || "Object".equals(retType.getNameWithoutPackage());
if (retTypeMatches && argTypeMatches) {
if (node.isStatic() ? foundStatic : foundInstance) {
throw new RuntimeException("Repetitive main method found.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public boolean isSealed() {
List<AnnotationStub> annotations = classData.annotations;
if (annotations != null) {
for (AnnotationStub stub : annotations) {
if (stub.className.equals("groovy.transform.Sealed")) {
if ("groovy.transform.Sealed".equals(stub.className)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/codehaus/groovy/ast/tools/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static void addPseudoProperties(ClassNode origType, ClassNode cNode, List
if (!includeStatic && mNode.isStatic()) continue;
if (hasAnnotation(mNode, INTERNAL_TYPE)) continue;
String name = mNode.getName();
if ((name.length() <= 3 && !name.startsWith(IS_PREFIX)) || name.equals("getClass") || name.equals("getMetaClass") || name.equals("getDeclaringClass")) {
if ((name.length() <= 3 && !name.startsWith(IS_PREFIX)) || "getClass".equals(name) || "getMetaClass".equals(name) || "getDeclaringClass".equals(name)) {
// Optimization: skip invalid propertyNames
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/groovy/ast/tools/ClosureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static String convertClosureToSource(final ReaderSource readerSource, fin
public static boolean hasSingleCharacterArg(final Closure c) {
if (c.getMaximumNumberOfParameters() != 1) return false;
String typeName = c.getParameterTypes()[0].getName();
return typeName.equals("char") || typeName.equals("java.lang.Character");
return "char".equals(typeName) || "java.lang.Character".equals(typeName);
}

/**
Expand All @@ -64,7 +64,7 @@ public static boolean hasSingleCharacterArg(final Closure c) {
public static boolean hasSingleStringArg(final Closure c) {
if (c.getMaximumNumberOfParameters() != 1) return false;
String typeName = c.getParameterTypes()[0].getName();
return typeName.equals("java.lang.String");
return "java.lang.String".equals(typeName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ public static void copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNod
continue;
}

if (!includeGenerated && annotation.getClassNode().getName().equals("groovy.transform.Generated")) {
if (!includeGenerated && "groovy.transform.Generated".equals(annotation.getClassNode().getName())) {
continue;
}

Expand Down
31 changes: 28 additions & 3 deletions src/main/java/org/codehaus/groovy/classgen/EnumVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,32 @@

import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
import static org.codehaus.groovy.ast.ClassHelper.int_TYPE;
import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
import static org.codehaus.groovy.ast.tools.GeneralUtils.arrayX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorThisX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.declS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.fieldX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.geX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ifS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.indexX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.localVarX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ltX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.mapX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.minusX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
import static org.codehaus.groovy.ast.tools.GeneralUtils.plusX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.returnS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.spreadX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
import static org.objectweb.asm.Opcodes.ACC_ABSTRACT;
import static org.objectweb.asm.Opcodes.ACC_FINAL;
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
Expand Down Expand Up @@ -116,8 +141,8 @@ private static void addMethods(final ClassNode enumClass, final FieldNode values
boolean hasNext = false;
boolean hasPrevious = false;
for (MethodNode m : enumClass.getMethods()) {
if (m.getName().equals("next") && m.getParameters().length == 0) hasNext = true;
if (m.getName().equals("previous") && m.getParameters().length == 0) hasPrevious = true;
if ("next".equals(m.getName()) && m.getParameters().length == 0) hasNext = true;
if ("previous".equals(m.getName()) && m.getParameters().length == 0) hasPrevious = true;
if (hasNext && hasPrevious) break;
}
boolean empty = true;
Expand Down
Loading

0 comments on commit 1ad5b8f

Please sign in to comment.