|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2025 Contributors to the openHAB project |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | +package org.openhab.tools.analysis.pmd; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import net.sourceforge.pmd.lang.java.ast.ASTConstructorCall; |
| 18 | +import net.sourceforge.pmd.lang.java.ast.ASTMethodCall; |
| 19 | +import net.sourceforge.pmd.lang.java.ast.JavaNode; |
| 20 | +import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule; |
| 21 | +import net.sourceforge.pmd.lang.java.types.InvocationMatcher; |
| 22 | + |
| 23 | +/** |
| 24 | + * Checks if one of the JDK provided methods which implicitly use the default locale is used. |
| 25 | + * <p> |
| 26 | + * Not all methods are covered, only public methods that can be called directly. Complex |
| 27 | + * call hierarchies that end up calling a private or protected method won't be caught. Classes |
| 28 | + * belonging to "irrelevant things" like AWT or Swing are excluded. |
| 29 | + * |
| 30 | + * @author Ravi Nadahar - Initial contribution |
| 31 | + */ |
| 32 | +public class ImplicitDefaultLocaleRule extends AbstractJavaRulechainRule { |
| 33 | + |
| 34 | + private static final List<InvocationMatcher> METHODS = List.of( // |
| 35 | + InvocationMatcher.parse("java.io.OutputStreamWriter#new(java.io.OutputStream)"), // |
| 36 | + InvocationMatcher.parse("java.io.PrintStream#format(java.lang.String,java.lang.Object[])"), // |
| 37 | + InvocationMatcher.parse("java.io.PrintWriter#format(java.lang.String,java.lang.Object[])"), // |
| 38 | + InvocationMatcher.parse("java.lang.String#toLowerCase()"), // |
| 39 | + InvocationMatcher.parse("java.lang.String#toUpperCase()"), // |
| 40 | + InvocationMatcher.parse("java.text.BreakIterator#getWordInstance()"), // |
| 41 | + InvocationMatcher.parse("java.text.BreakIterator#getCharacterInstance()"), // |
| 42 | + InvocationMatcher.parse("java.text.BreakIterator#getSentenceInstance()"), // |
| 43 | + InvocationMatcher.parse("java.text.BreakIterator#getLineInstance()"), // |
| 44 | + InvocationMatcher.parse("java.text.Collator#getInstance()"), // |
| 45 | + InvocationMatcher.parse("java.text.DateFormat#getTimeInstance()"), // |
| 46 | + InvocationMatcher.parse("java.text.DateFormat#getTimeInstance(int)"), // |
| 47 | + InvocationMatcher.parse("java.text.DateFormatSymbols#new()"), // |
| 48 | + InvocationMatcher.parse("java.text.DateFormatSymbols#getInstance()"), // |
| 49 | + InvocationMatcher.parse("java.text.DecimalFormat#new()"), // |
| 50 | + InvocationMatcher.parse("java.text.DecimalFormat#new(java.lang.String)"), // |
| 51 | + InvocationMatcher.parse("java.text.DecimalFormatSymbols#new()"), // |
| 52 | + InvocationMatcher.parse("java.text.DecimalFormatSymbols#getInstance()"), // |
| 53 | + InvocationMatcher.parse("java.text.MessageFormat#new(java.lang.String)"), // |
| 54 | + InvocationMatcher.parse("java.text.NumberFormat#getInstance()"), // |
| 55 | + InvocationMatcher.parse("java.text.NumberFormat#getNumberInstance()"), // |
| 56 | + InvocationMatcher.parse("java.text.NumberFormat#getIntegerInstance()"), // |
| 57 | + InvocationMatcher.parse("java.text.NumberFormat#getCurrencyInstance()"), // |
| 58 | + InvocationMatcher.parse("java.text.NumberFormat#getPercentInstance()"), // |
| 59 | + InvocationMatcher.parse("java.text.NumberFormat#getScientificInstance()"), // |
| 60 | + InvocationMatcher.parse("java.text.SimpleDateFormat#new()"), // |
| 61 | + InvocationMatcher.parse("java.text.SimpleDateFormat#new(java.lang.String)"), // |
| 62 | + InvocationMatcher.parse("java.text.SimpleDateFormat#new(java.lang.String,java.text.DateFormatSymbols)"), // |
| 63 | + InvocationMatcher.parse("java.time.format.DateTimeFormatterBuilder#toFormatter()"), // |
| 64 | + InvocationMatcher.parse("java.util.Calendar#new()"), // |
| 65 | + InvocationMatcher.parse("java.util.Calendar#getInstance()"), // |
| 66 | + InvocationMatcher.parse("java.util.Calendar#getInstance(java.util.TimeZone)"), // |
| 67 | + InvocationMatcher.parse("java.util.Currency#getSymbol()"), // |
| 68 | + InvocationMatcher.parse("java.util.Currency#getDisplayName()"), // |
| 69 | + InvocationMatcher.parse("java.util.Formatter#new()"), // |
| 70 | + InvocationMatcher.parse("java.util.Formatter#new(java.lang.Appendable)"), // |
| 71 | + InvocationMatcher.parse("java.util.Formatter#new(java.lang.String)"), // |
| 72 | + InvocationMatcher.parse("java.util.Formatter#new(java.lang.String,java.lang.String)"), // |
| 73 | + InvocationMatcher.parse("java.util.Formatter#new(java.io.File)"), // |
| 74 | + InvocationMatcher.parse("java.util.Formatter#new(java.io.File,java.lang.String)"), // |
| 75 | + InvocationMatcher.parse("java.util.Formatter#new(java.io.PrintStream)"), // |
| 76 | + InvocationMatcher.parse("java.util.Formatter#new(java.io.OutputStream)"), // |
| 77 | + InvocationMatcher.parse("java.util.Formatter#new(java.io.OutputStream,java.lang.String)"), // |
| 78 | + InvocationMatcher.parse("java.util.GregorianCalendar#new()"), // |
| 79 | + InvocationMatcher.parse("java.util.GregorianCalendar#new(java.util.TimeZone)"), // |
| 80 | + InvocationMatcher.parse("java.util.ResourceBundle#getBundle(java.lang.String)"), // |
| 81 | + InvocationMatcher |
| 82 | + .parse("java.util.ResourceBundle#getBundle(java.lang.String,java.util.ResourceBundle.Control)"), // |
| 83 | + InvocationMatcher.parse("java.util.ResourceBundle#getBundle(java.lang.String,java.lang.Module)"), // |
| 84 | + InvocationMatcher.parse("java.util.TimeZone#getDisplayName()"), // |
| 85 | + InvocationMatcher.parse("java.util.TimeZone#getDisplayName(boolean,int)"), // |
| 86 | + InvocationMatcher.parse("javax.accessibility.AccessibleBundle#toDisplayString()"), // |
| 87 | + InvocationMatcher.parse("javax.xml.datatype.XMLGregorianCalendar#toGregorianCalendar()") // |
| 88 | + ); |
| 89 | + |
| 90 | + public ImplicitDefaultLocaleRule() { |
| 91 | + super(ASTConstructorCall.class, ASTMethodCall.class); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String getDescription() { |
| 96 | + return "Methods that implicitly use the default Locale can lead to bugs. Use an overloaded version with an explicit Locale, and specify 'Locale.getDefault()' if the default Locale is desired."; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Object visit(ASTConstructorCall node, Object data) { |
| 101 | + checkInvocation(node, data); |
| 102 | + return data; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Object visit(ASTMethodCall node, Object data) { |
| 107 | + checkInvocation(node, data); |
| 108 | + return data; |
| 109 | + } |
| 110 | + |
| 111 | + private void checkInvocation(JavaNode node, Object data) { |
| 112 | + for (InvocationMatcher matcher : METHODS) { |
| 113 | + if (matcher.matchesCall(node)) { |
| 114 | + asCtx(data).addViolationWithPosition(node, node.getBeginLine(), node.getEndLine(), |
| 115 | + "Avoid method with implicit default Locale: {0}", node.getText()); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments