(no message)

This commit is contained in:
Dave Griffith
2005-01-28 16:57:01 +03:00
parent 23cda2f023
commit 211eec05b1
7 changed files with 140 additions and 87 deletions
@@ -10,7 +10,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
@@ -21,6 +21,9 @@
<option name="DEFAULT_COMPILER" value="Javac" />
<option name="CLEAR_OUTPUT_DIRECTORY" value="false" />
<option name="DEPLOY_AFTER_MAKE" value="0" />
<excludeFromCompile>
<directory url="file://$PROJECT_DIR$/test" includeSubdirectories="true" />
</excludeFromCompile>
<resourceExtensions>
<entry name=".+\.(properties|xml|html|dtd|tld)" />
<entry name=".+\.(gif|png|jpeg|jpg)" />
@@ -1,44 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.siyeh.ig.bugs.IgnoreResultOfCallInspection.Form">
<grid id="9993d" binding="contentPanel" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="b6695" binding="contentPanel" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="92" y="33" width="211" height="192"/>
<xy x="92" y="39" width="211" height="226"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<scrollpane id="14807">
<grid id="f8a05" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="0" y="0" width="211" height="163"/>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3"/>
<xy x="0" y="0" width="211" height="199"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3"/>
</constraints>
<properties/>
<border type="none"/>
<border type="etched" title="Ignored calls to report"/>
<children>
<component id="b0978" class="javax.swing.JTable" binding="table">
<constraints/>
<properties/>
<component id="f6174" class="javax.swing.JButton" binding="deleteButton">
<constraints>
<xy x="105" y="169" width="100" height="24"/>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="7" anchor="0" fill="1"/>
</constraints>
<properties>
<text value="Delete"/>
</properties>
</component>
<component id="52926" class="javax.swing.JButton" binding="addButton">
<constraints>
<xy x="6" y="169" width="89" height="24"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="7" anchor="0" fill="1"/>
</constraints>
<properties>
<text value="Add"/>
</properties>
</component>
<scrollpane id="14807">
<constraints>
<xy x="6" y="20" width="199" height="144"/>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="b0978" class="javax.swing.JTable" binding="table">
<constraints/>
<properties/>
</component>
</children>
</scrollpane>
</children>
</scrollpane>
<component id="f6174" class="javax.swing.JButton" binding="deleteButton">
</grid>
<component id="2d492" class="javax.swing.JCheckBox" binding="nonLibraryCheckbox">
<constraints>
<xy x="105" y="168" width="106" height="24"/>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="7" anchor="0" fill="1"/>
<xy x="0" y="204" width="188" height="22"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0"/>
</constraints>
<properties>
<text value="Delete"/>
</properties>
</component>
<component id="52926" class="javax.swing.JButton" binding="addButton">
<constraints>
<xy x="0" y="168" width="95" height="24"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="7" anchor="0" fill="1"/>
</constraints>
<properties>
<text value="Add"/>
<text value="Report all ignored non-library calls"/>
</properties>
</component>
</children>
@@ -9,9 +9,12 @@ import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.ExpressionInspection;
import com.siyeh.ig.GroupNames;
import com.siyeh.ig.psiutils.ClassUtils;
import com.siyeh.ig.psiutils.LibraryUtil;
import org.jdom.Element;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.border.EtchedBorder;
import javax.swing.table.AbstractTableModel;
import java.awt.event.ActionEvent;
@@ -23,7 +26,9 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IgnoreResultOfCallInspection extends ExpressionInspection {
public class IgnoreResultOfCallInspection extends ExpressionInspection{
public boolean m_reportAllNonLibraryCalls = false;
public String callCheckString = "java.io.InputStream,read," +
"java.io.InputStream,skip," +
"java.lang.StringBuffer,toString," +
@@ -39,33 +44,34 @@ public class IgnoreResultOfCallInspection extends ExpressionInspection {
parseCallCheckString();
}
public void readSettings(Element element) throws InvalidDataException {
public void readSettings(Element element) throws InvalidDataException{
super.readSettings(element);
parseCallCheckString();
}
private void parseCallCheckString() {
private void parseCallCheckString(){
callsToCheck.clear();
final String[] strings = callCheckString.split(",");
for (int i = 0; i < strings.length; i += 2) {
for(int i = 0; i < strings.length; i += 2){
final String className = strings[i];
final String methodName = strings[i + 1];
callsToCheck.add(new ReturnCheckSpecification(className, methodName));
callsToCheck.add(
new ReturnCheckSpecification(className, methodName));
}
}
public void writeSettings(Element element) throws WriteExternalException {
public void writeSettings(Element element) throws WriteExternalException{
formatCallCheckString();
super.writeSettings(element);
}
private void formatCallCheckString() {
private void formatCallCheckString(){
final StringBuffer buffer = new StringBuffer();
boolean first = true;
for (Iterator iterator = callsToCheck.iterator(); iterator.hasNext();) {
if (first) {
for(Iterator iterator = callsToCheck.iterator(); iterator.hasNext();){
if(first){
first = false;
} else {
} else{
buffer.append(',');
}
final ReturnCheckSpecification returnCheckSpecification =
@@ -79,20 +85,20 @@ public class IgnoreResultOfCallInspection extends ExpressionInspection {
callCheckString = buffer.toString();
}
public String getDisplayName() {
public String getDisplayName(){
return "Result of method call ignored";
}
public String getGroupDisplayName() {
public String getGroupDisplayName(){
return GroupNames.BUGS_GROUP_NAME;
}
public JComponent createOptionsPanel() {
public JComponent createOptionsPanel(){
final Form form = new Form();
return form.getContentPanel();
}
public String buildErrorString(PsiElement location) {
public String buildErrorString(PsiElement location){
final PsiElement parent = location.getParent();
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) parent.getParent();
final PsiMethod method = methodCallExpression.resolveMethod();
@@ -101,48 +107,57 @@ public class IgnoreResultOfCallInspection extends ExpressionInspection {
return "result of " + className + ".#ref() is ignored. #loc ";
}
public BaseInspectionVisitor createVisitor(InspectionManager inspectionManager, boolean onTheFly) {
public BaseInspectionVisitor createVisitor(
InspectionManager inspectionManager, boolean onTheFly){
return new IgnoreResultOfCallVisitor(this, inspectionManager, onTheFly);
}
private class IgnoreResultOfCallVisitor extends BaseInspectionVisitor {
private IgnoreResultOfCallVisitor(BaseInspection inspection, InspectionManager inspectionManager, boolean isOnTheFly) {
private class IgnoreResultOfCallVisitor extends BaseInspectionVisitor{
private IgnoreResultOfCallVisitor(BaseInspection inspection,
InspectionManager inspectionManager,
boolean isOnTheFly){
super(inspection, inspectionManager, isOnTheFly);
}
public void visitExpressionStatement(PsiExpressionStatement statement) {
public void visitExpressionStatement(PsiExpressionStatement statement){
super.visitExpressionStatement(statement);
if (!(statement.getExpression() instanceof PsiMethodCallExpression)) {
if(!(statement.getExpression() instanceof PsiMethodCallExpression)){
return;
}
final PsiMethodCallExpression call = (PsiMethodCallExpression) statement.getExpression();
final PsiReferenceExpression methodExpression = call.getMethodExpression();
final String methodName = methodExpression.getReferenceName();
if (methodName == null) {
final PsiMethod method = call.resolveMethod();
if(method == null){
return;
}
final PsiMethod method = call.resolveMethod();
if (method == null) {
if(method.isConstructor()){
return;
}
final PsiType retType = method.getReturnType();
if(PsiType.VOID.equals(retType)){
return;
}
final PsiClass aClass = method.getContainingClass();
if (aClass == null) {
if(aClass == null){
return;
}
final PsiType retType = method.getReturnType();
if (retType == PsiType.VOID) {
if(m_reportAllNonLibraryCalls &&
!LibraryUtil.classIsInLibrary(aClass)){
registerMethodCallError(call);
return;
}
for (Iterator iterator = callsToCheck.iterator(); iterator.hasNext();) {
final PsiReferenceExpression methodExpression = call.getMethodExpression();
final String methodName = methodExpression.getReferenceName();
if(methodName == null){
return;
}
for(Iterator iterator = callsToCheck.iterator();
iterator.hasNext();){
final ReturnCheckSpecification spec = (ReturnCheckSpecification) iterator.next();
final Pattern methodNamePattern = spec.getMethodNamePattern();
if (methodNamePattern != null &&
methodNamesMatch(methodName, methodNamePattern)) {
if(methodNamePattern != null &&
methodNamesMatch(methodName, methodNamePattern)){
final String classNameToCompare = spec.getClassName();
if (ClassUtils.isSubclass(aClass, classNameToCompare)) {
if(ClassUtils.isSubclass(aClass, classNameToCompare)){
registerMethodCallError(call);
return;
}
@@ -151,93 +166,107 @@ public class IgnoreResultOfCallInspection extends ExpressionInspection {
}
private boolean methodNamesMatch(String methodName, Pattern methodNamePattern) {
private boolean methodNamesMatch(String methodName,
Pattern methodNamePattern){
final Matcher matcher = methodNamePattern.matcher(methodName);
return matcher.matches();
}
}
public class Form {
public class Form{
private JPanel contentPanel;
private JButton addButton;
private JButton deleteButton;
private JTable table;
private JCheckBox nonLibraryCheckbox;
public Form() {
public Form(){
super();
table.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setRowSelectionAllowed(true);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setEnabled(true);
final ReturnCheckSpecificationTableModel model =
new ReturnCheckSpecificationTableModel();
table.setModel(model);
addButton.setEnabled(true);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
callsToCheck.add(new ReturnCheckSpecification());
model.fireTableStructureChanged();
}
});
deleteButton.setEnabled(true);
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
final int[] selectedRows = table.getSelectedRows();
Arrays.sort(selectedRows);
for (int i = selectedRows.length - 1; i >= 0; i--) {
for(int i = selectedRows.length - 1; i >= 0; i--){
callsToCheck.remove(selectedRows[i]);
}
model.fireTableStructureChanged();
}
});
nonLibraryCheckbox.setEnabled(true);
nonLibraryCheckbox.setSelected(m_reportAllNonLibraryCalls);
final ButtonModel buttonModel = nonLibraryCheckbox.getModel();
buttonModel.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
m_reportAllNonLibraryCalls = buttonModel.isSelected();
}
});
}
public JComponent getContentPanel() {
public JComponent getContentPanel(){
return contentPanel;
}
}
private class ReturnCheckSpecificationTableModel extends AbstractTableModel {
private class ReturnCheckSpecificationTableModel
extends AbstractTableModel{
public int getRowCount() {
public int getRowCount(){
return callsToCheck.size();
}
public int getColumnCount() {
public int getColumnCount(){
return 2;
}
public String getColumnName(int columnIndex) {
if (columnIndex == 0) {
public String getColumnName(int columnIndex){
if(columnIndex == 0){
return "Class name";
}
return "Method name";
}
public Class getColumnClass(int columnIndex) {
public Class getColumnClass(int columnIndex){
return String.class;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
public boolean isCellEditable(int rowIndex, int columnIndex){
return true;
}
public Object getValueAt(int rowIndex, int columnIndex) {
final ReturnCheckSpecification spec = (ReturnCheckSpecification) callsToCheck.get(rowIndex);
if (columnIndex == 0) {
public Object getValueAt(int rowIndex, int columnIndex){
final ReturnCheckSpecification spec = (ReturnCheckSpecification) callsToCheck.get(
rowIndex);
if(columnIndex == 0){
return spec.getClassName();
} else {
} else{
return spec.getMethodName();
}
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
final ReturnCheckSpecification spec = (ReturnCheckSpecification) callsToCheck.get(rowIndex);
if (columnIndex == 0) {
public void setValueAt(Object aValue, int rowIndex, int columnIndex){
final ReturnCheckSpecification spec = (ReturnCheckSpecification) callsToCheck.get(
rowIndex);
if(columnIndex == 0){
spec.setClassName((String) aValue);
} else {
} else{
spec.setMethodName((String) aValue);
}
}
@@ -50,7 +50,7 @@ public class SerialVersionUIDNotStaticFinalInspection extends ClassInspection {
registerFieldError(field);
} else {
final PsiType type = field.getType();
if (PsiType.LONG.equals(type)) {
if (!PsiType.LONG.equals(type)) {
registerFieldError(field);
}
}
@@ -52,8 +52,8 @@ public class UnnecessaryFullyQualifiedNameInspection extends ClassInspection {
final boolean oldUseFQNamesInJavadoc = settings.USE_FQ_CLASS_NAMES_IN_JAVADOC;
final boolean oldUseFQNames = settings.USE_FQ_CLASS_NAMES;
try {
settings.USE_FQ_CLASS_NAMES_IN_JAVADOC = true;
settings.USE_FQ_CLASS_NAMES = true;
settings.USE_FQ_CLASS_NAMES_IN_JAVADOC = false;
settings.USE_FQ_CLASS_NAMES = false;
final PsiJavaCodeReferenceElement reference = (PsiJavaCodeReferenceElement) descriptor.getPsiElement();
final PsiManager psiManager = reference.getManager();
final CodeStyleManager styleManager = psiManager.getCodeStyleManager();
@@ -12,6 +12,7 @@ as all of those methods are side-effect free and thus pointless if ignored
Use the panel below to enter the class names and method names of the methods you wish to check for
ignored returns. Class names must be specified as a simple string, while method names may be
specified using a standard Java regular expression. Specifying a class names implicitly specifies
that class and all of it's subclasses.
that class and all of it's subclasses. In addition, you may choose to report any
call to a non-library call whose result is ignored
</font></td> </tr> <tr> <td height="20"> <font face="verdana" size="-2">Powered by InspectionGadgets </font> </td> </tr> </table> </body>
</html>