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 d5c3255 commit d5760cf
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1015,13 +1015,13 @@ private List<String> extractJointOptions(Path classpath) {

for (Map.Entry<String, Object> e : rc.getAttributeMap().entrySet()) {
String key = e.getKey();
if (key.equals("depend")
|| key.equals("encoding")
|| key.equals("extdirs")
|| key.equals("nativeheaderdir")
|| key.equals("release")
|| key.equals("source")
|| key.equals("target")) {
if ("depend".equals(key)
|| "encoding".equals(key)
|| "extdirs".equals(key)
|| "nativeheaderdir".equals(key)
|| "release".equals(key)
|| "source".equals(key)
|| "target".equals(key)) {
switch (key) {
case "nativeheaderdir":
key = "h";
Expand Down Expand Up @@ -1053,7 +1053,7 @@ private List<String> extractJointOptions(Path classpath) {
// map "modulepath" or "modulepathref" to "-J-module-path="
jointOptions.add("-J-module-path=" + javac.getModulepath());
}
} else if (!key.contains("debug") && !key.equals("deprecation") && !key.equals("nowarn") && !key.equals("verbose")) {
} else if (!key.contains("debug") && !"deprecation".equals(key) && !"nowarn".equals(key) && !"verbose".equals(key)) {
log.warn("The option " + key + " cannot be set on the contained <javac> element. The option will be ignored.");
}
// TODO: defaultexcludes, excludes(file)?, includes(file)?, includeDestClasses, tempdir
Expand All @@ -1062,10 +1062,10 @@ private List<String> extractJointOptions(Path classpath) {
// Ant's <javac> supports nested <compilerarg value=""> elements (there
// can be multiple of them) for additional options to be passed to javac.
for (RuntimeConfigurable childrc : Collections.list(rc.getChildren())) {
if (childrc.getElementTag().equals("compilerarg")) {
if ("compilerarg".equals(childrc.getElementTag())) {
for (Map.Entry<String, Object> e : childrc.getAttributeMap().entrySet()) {
String key = e.getKey();
if (key.equals("value")) {
if ("value".equals(key)) {
String value = getProject().replaceProperties(e.getValue().toString());
StringTokenizer st = new StringTokenizer(value, " ");
while (st.hasMoreTokens()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private void parsePackages(List<String> resultantPackages, Path sourcePath) {
File pd = new File(baseDir, dir);
String[] files = pd.list((dir1, name) -> {
if (!includeNoSourcePackages
&& name.equals("package.html")) return true;
&& "package.html".equals(name)) return true;
final StringTokenizer tokenizer = new StringTokenizer(extensions, ":");
while (tokenizer.hasMoreTokens()) {
String ext = tokenizer.nextToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private AnnotationProcessor createAnnotationProcessor(AnnotationNode annotationN

List<AnnotationNode> annotations = annotationNode.getClassNode().getAnnotations();
for (AnnotationNode anno : annotations) {
if (anno.getClassNode().getName().equals("org.apache.groovy.contracts.annotations.meta.AnnotationProcessorImplementation")) {
if ("org.apache.groovy.contracts.annotations.meta.AnnotationProcessorImplementation".equals(anno.getClassNode().getName())) {
annotationProcessor = anno.getMember("value");
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public static void addOldVariableMethodNode(final ClassNode classNode) {
if (fieldType.getName().startsWith("java.lang") || ClassHelper.isPrimitiveType(fieldType) || fieldType.getName().startsWith("java.math") ||
fieldType.getName().startsWith("java.util") ||
fieldType.getName().startsWith("java.sql") ||
fieldType.getName().equals("groovy.lang.GString") ||
fieldType.getName().equals("java.lang.String")) {
"groovy.lang.GString".equals(fieldType.getName()) ||
"java.lang.String".equals(fieldType.getName())) {

MethodNode cloneMethod = fieldType.getMethod("clone", Parameter.EMPTY_ARRAY);
// if a clone classNode is available, the value is cloned
Expand All @@ -94,8 +94,8 @@ public static void addOldVariableMethodNode(final ClassNode classNode) {
} else if (ClassHelper.isPrimitiveType(fieldType)
|| ClassHelper.isNumberType(fieldType)
|| fieldType.getName().startsWith("java.math")
|| fieldType.getName().equals("groovy.lang.GString")
|| fieldType.getName().equals("java.lang.String")) {
|| "groovy.lang.GString".equals(fieldType.getName())
|| "java.lang.String".equals(fieldType.getName())) {

VariableExpression oldVariable = localVarX("$old$" + fieldNode.getName(), fieldNode.getType());
Statement oldVariableAssignment = declS(oldVariable, fieldX(fieldNode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ private void processPropertiesFromGetterSetter(SimpleGroovyMethodDoc currentMeth

//find expected method name
String expectedMethodName;
if ("set".equals(prefix) && (currentMethodDoc.parameters().length >= 1 && !currentMethodDoc.parameters()[0].typeName().equals("boolean"))) {
if ("set".equals(prefix) && (currentMethodDoc.parameters().length >= 1 && !"boolean".equals(currentMethodDoc.parameters()[0].typeName()))) {
expectedMethodName = "get" + propName;
} else if ("get".equals(prefix) && !currentMethodDoc.returnType().typeName().equals("boolean")) {
} else if ("get".equals(prefix) && !"boolean".equals(currentMethodDoc.returnType().typeName())) {
expectedMethodName = "set" + propName;
} else if ("is".equals(prefix)) {
expectedMethodName = "set" + propName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public List<GroovyClassDoc> getParentClasses() {
result.add(0, nextDoc);
prev = nextDoc;
}
if (!result.get(0).qualifiedTypeName().equals("java.lang.Object")) {
if (!"java.lang.Object".equals(result.get(0).qualifiedTypeName())) {
result.add(0, new ExternalGroovyClassDoc(Object.class));
}
return result;
Expand Down Expand Up @@ -461,7 +461,7 @@ public static String getDocUrl(String type, boolean full, List<LinkArgument> lin
return type;
type = type.trim();
if (isPrimitiveType(type) || type.length() == 1) return type;
if (type.equals("def")) type = "java.lang.Object def";
if ("def".equals(type)) type = "java.lang.Object def";
// cater for explicit href in e.g. @see, TODO: push this earlier?
if (type.startsWith("<a href=")) return type;
if (type.startsWith("? extends ")) return "? extends " + getDocUrl(type.substring(10), full, links, relativePath, rootDoc, classDoc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void addOperationCallListeners(Map<String, Map<String, Map<String, Object
String listenerType = (String) listener.get("type");
listener.put("managedObject", this.managedObject);
// register an attribute change notification listener with model mbean
if (listenerType.equals("attributeChangeListener")) {
if ("attributeChangeListener".equals(listenerType)) {
try {
this.addAttributeChangeNotificationListener(
AttributeChangedListener.getListener(), (String) listener.get("attribute"), listener
Expand All @@ -97,7 +97,7 @@ public void addOperationCallListeners(Map<String, Map<String, Map<String, Object
throw new JmxBuilderException(e);
}
}
if (listenerType.equals("operationCallListener")) {
if ("operationCallListener".equals(listenerType)) {
String eventType = "jmx.operation.call." + target;
NotificationFilterSupport filter = new NotificationFilterSupport();
filter.enableType(eventType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
*/
package groovy.json;

import org.apache.groovy.json.internal.CharBuf;
import org.apache.groovy.json.internal.Chr;
import groovy.lang.Closure;
import groovy.lang.MetaBeanProperty;
import groovy.lang.MetaProperty;
import groovy.util.Expando;
import org.apache.groovy.json.internal.CharBuf;
import org.apache.groovy.json.internal.Chr;

import java.io.File;
import java.math.BigDecimal;
Expand Down Expand Up @@ -252,7 +252,7 @@ protected void writeObject(String key, Object object, CharBuf buffer) {
}

String name = mp.getName();
if (name.equals("class") || name.equals("metaClass") || name.equals("declaringClass")) continue;
if ("class".equals(name) || "metaClass".equals(name) || "declaringClass".equals(name)) continue;

namesAndValues.put(name, mp.getProperty(object));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ public Object eval(String script, ScriptContext ctx)
String val = (String) ctx.getAttribute("#jsr223.groovy.engine.keep.globals", ScriptContext.ENGINE_SCOPE);
ReferenceBundle bundle = ReferenceBundle.getHardBundle();
if (val != null && val.length() > 0) {
if (val.equalsIgnoreCase("soft")) {
if ("soft".equalsIgnoreCase(val)) {
bundle = ReferenceBundle.getSoftBundle();
} else if (val.equalsIgnoreCase("weak")) {
} else if ("weak".equalsIgnoreCase(val)) {
bundle = ReferenceBundle.getWeakBundle();
} else if (val.equalsIgnoreCase("phantom")) {
} else if ("phantom".equalsIgnoreCase(val)) {
bundle = ReferenceBundle.getPhantomBundle();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public GroovyResultSetProxy(GroovyResultSetExtension ext) {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
if (method.getDeclaringClass() == GroovyObject.class) {
if (name.equals("getMetaClass")) {
if ("getMetaClass".equals(name)) {
return getMetaClass();
} else if (name.equals("setMetaClass")) {
} else if ("setMetaClass".equals(name)) {
return setMetaClass((MetaClass) args[0]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
Original file line number Diff line number Diff line change
Expand Up @@ -4714,7 +4714,7 @@ public List<Object> getUpdatedParams(List<Object> params, List<Tuple> indexPropL
if (index < 0 || index >= params.size())
throw new IllegalArgumentException("Invalid index " + index + " should be in range 1.." + params.size());
try {
updatedParams.add(prop.equals("<this>") ? params.get(index) : InvokerHelper.getProperty(params.get(index), prop));
updatedParams.add("<this>".equals(prop) ? params.get(index) : InvokerHelper.getProperty(params.get(index), prop));
} catch(MissingPropertyException mpe) {
throw new IllegalArgumentException("Property '" + prop + "' not found for parameter " + index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import groovy.lang.Writable;
import groovy.namespace.QName;
import groovy.util.IndentPrinter;
import groovy.util.Node;
import groovy.xml.XmlNodePrinter;
import groovy.xml.XmlParser;
import groovy.namespace.QName;
import org.apache.groovy.io.StringBuilderWriter;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.runtime.FormatHelper;
Expand Down Expand Up @@ -134,12 +134,12 @@ private static class GspPrinter extends XmlNodePrinter {
}

protected void printGroovyTag(String tag, String text) {
if (tag.equals("scriptlet")) {
if ("scriptlet".equals(tag)) {
out.print(text);
out.print("\n");
return;
}
if (tag.equals("expression")) {
if ("expression".equals(tag)) {
printLineBegin();
out.print("${");
out.print(text);
Expand Down Expand Up @@ -242,7 +242,7 @@ protected boolean printSpecialNode(Node node) {
if (name instanceof QName) {
QName qn = (QName) name;
// check uri and for legacy cases just check prefix name (not recommended)
if (qn.getNamespaceURI().equals("http://groovy.codehaus.org/2005/gsp") || qn.getPrefix().equals("gsp")) {
if ("http://groovy.codehaus.org/2005/gsp".equals(qn.getNamespaceURI()) || "gsp".equals(qn.getPrefix())) {
String s = qn.getLocalPart();
if (s.length() == 0) {
throw new RuntimeException("No local part after 'gsp:' given in node " + node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private boolean isJUnit5AnnotationPresent(Annotation[] annotations, GroovyClassL
return true;
}
// it might directly annotate a class, e.g. Specification in Spock 2
if (name.equals("org.junit.platform.commons.annotation.Testable") && tryLoadClass(name, loader)) {
if ("org.junit.platform.commons.annotation.Testable".equals(name) && tryLoadClass(name, loader)) {
return true;
}
if (isJUnit5TestableMetaAnnotationPresent(type, new HashSet<>()) && tryLoadClass(name, loader)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,13 @@ private void printEscaped(String s, boolean isAttributeValue) {
out.print("&amp;");
break;
case '\'':
if (isAttributeValue && quote.equals("'"))
if (isAttributeValue && "'".equals(quote))
out.print("&apos;");
else
out.print(c);
break;
case '"':
if (isAttributeValue && quote.equals("\""))
if (isAttributeValue && "\"".equals(quote))
out.print("&quot;");
else
out.print(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ protected Map defineNamespaces(Element element, Map namespaces) {

protected void defineNamespace(Map namespaces, String prefix, String uri) {
namespaces.put(prefix, uri);
if (!prefix.equals("xmlns") && !prefix.equals("xml")) {
if (!"xmlns".equals(prefix) && !"xml".equals(prefix)) {
printIndent();
print("mkp.declareNamespace(");
print(prefix);
Expand Down Expand Up @@ -310,7 +310,7 @@ protected boolean printAttributes(Element element) {

protected void printAttributeWithPrefix(Attr attribute, StringBuffer buffer) {
String prefix = attribute.getPrefix();
if (prefix != null && prefix.length() > 0 && !prefix.equals("xmlns")) {
if (prefix != null && prefix.length() > 0 && !"xmlns".equals(prefix)) {
if (buffer.length() > 0) {
buffer.append(", ");
}
Expand Down

0 comments on commit d5760cf

Please sign in to comment.