Skip to content

Commit

Permalink
Issue checkstyle#13772: IllegalImport with class pattern fails on the…
Browse files Browse the repository at this point in the history
… static import
  • Loading branch information
AayushSaini101 committed Oct 28, 2023
1 parent a6a339f commit 53b7ea5
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public final void setIllegalPkgs(String... from) {
illegalPkgs = from.clone();
illegalPkgsRegexps.clear();
for (String illegalPkg : illegalPkgs) {
illegalPkgsRegexps.add(CommonUtil.createPattern("^" + illegalPkg + "\\..*"));
illegalPkgsRegexps.add(CommonUtil.createPattern(illegalPkg + "\\..*"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,25 @@ public void testIllegalPackagesAndClassesRegularExpression()
getPath("InputIllegalImportDefault7.java"), expected);
}

@Test
public void testIllegalPackagesAndClassesRegularExpression2()
throws Exception {

final String[] expected = {
"15:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
"16:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
"19:1: " + getCheckMessage(MSG_KEY, "java.util.Enumeration"),
"20:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
"33:1: " + getCheckMessage(MSG_KEY, "java.awt.Component"),
"34:1: " + getCheckMessage(MSG_KEY, "java.awt.Graphics2D"),
"35:1: " + getCheckMessage(MSG_KEY, "java.awt.HeadlessException"),
"36:1: " + getCheckMessage(MSG_KEY, "java.awt.Label"),
"37:1: " + getCheckMessage(MSG_KEY, "java.util.Date"),
"38:1: " + getCheckMessage(MSG_KEY, "java.util.Calendar"),
"39:1: " + getCheckMessage(MSG_KEY, "java.util.BitSet"),
};
verifyWithInlineConfigParser(
getPath("InputIllegalImportDefault7.java"), expected);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
IllegalImport
illegalPkgs = java\\.util
illegalClasses = ^java\\.awt\\..*
regexp = true
*/

package com.puppycrawl.tools.checkstyle.checks.imports.illegalimport;

import java.io.*;
import java.lang.*;
import java.sql.Connection;
import static java.util.List.copyOf; // violation
import java.util.List; // violation
import java.lang.ArithmeticException;
import org.junit.jupiter.api.*;
import java.util.Enumeration; // violation
import java.util.Arrays; // violation

import javax.swing.JToolBar;
import javax.swing.JToggleButton;
import javax.swing.ScrollPaneLayout;
import javax.swing.BorderFactory;
import static java.io.File.listRoots;

import static javax.swing.WindowConstants.*;
import static javax.swing.WindowConstants.*;
import static java.io.File.createTempFile;
import org.junit.jupiter.api.*;

import java.awt.Component; // violation
import static java.awt.Label.ABORT; // violation
import java.awt.HeadlessException; // violation
import java.awt.Label; // violation
import java.util.Date; // violation
import java.util.Calendar; // violation
import java.util.BitSet; // violation

class InputIllegalImportDefault8
{
/** ignore **/
private Class mUse1 = Connection.class;
/** ignore **/
private Class mUse2 = java.io.File.class;
/** ignore **/
private Class mUse3 = null;
/** ignore **/
private Class mUse4 = java.util.Enumeration[].class;
/** usage of illegal import **/
private String ftpClient = null;

/** usage via static method, both normal and fully qualified */
{
int[] x = {};
Arrays.sort(x);
Object obj = javax.swing.BorderFactory.createEmptyBorder();
File[] files = listRoots();
}

/** usage of inner class as type */
private JToolBar.Separator mSep = null;

/** usage of inner class in Constructor */
private Object mUse5 = new Object();

/** usage of inner class in constructor, fully qualified */
private Object mUse6 = new javax.swing.JToggleButton.ToggleButtonModel();

/** we use class name as member's name.
* also an inline JavaDoc-only import {@link Vector linkText} */
private int Component;

/**
* method comment with JavaDoc-only import {@link BitSet#aMethod()}
*/
public void Label() {}

/**
* Renders to a {@linkplain Graphics2D graphics context}.
* @throws HeadlessException if no graphis environment can be found.
* @exception HeadlessException if no graphis environment can be found.
*/
public void render() {}

/**
* First is a class with a method with arguments {@link TestClass1#method1(TestClass2)}.
* Next is a class with typed method {@link TestClass3#method2(TestClass4, TestClass5)}.
*
* @param param1 with a link {@link TestClass6}
* @throws TestClass7 when broken
* @deprecated in 1 for removal in 2. Use {@link TestClass8}
*/
@Deprecated
public void aMethodWithManyLinks() {}
}

0 comments on commit 53b7ea5

Please sign in to comment.