"unchecked call to generic class?" 该怎么办
What to do about "unchecked call to generic class?"
我有这个class:
package Main;
public abstract class Click123<T extends java.awt.Component> ////////////////
{
boolean debugging = false;
public abstract void singleClick(java.awt.event.MouseEvent e); ////////////////
public abstract void doubleClick(java.awt.event.MouseEvent e); ////////////////
public abstract void tripleClick(java.awt.event.MouseEvent e); ////////////////
public abstract void manyClick(java.awt.event.MouseEvent e); ////////////////
public abstract int getFreq();// how long thread sleeps; i.e., click interval
public Click123(T target) ////////////////
{
target.addMouseListener ////////////////
(
new java.awt.event.MouseAdapter() ////////////////
{
Thread cp = null;
public void mouseClicked(final java.awt.event.MouseEvent e)
{
if (cp != null && cp.isAlive())
cp.interrupt();
if (e.getClickCount() == 1)
{
cp = new Thread(new ClickProcessor(new java.util.concurrent.Callable<Void>() {
@Override public Void call() throws Exception {
singleClick(e); //////////////////////////////////////////
return null;
}
}));
cp.start();
}
else if (e.getClickCount() == 2)
{
cp = new Thread(new ClickProcessor(new java.util.concurrent.Callable<Void>() {
@Override public Void call() throws Exception {
doubleClick(e); //////////////////////////////////////////
return null;
}
}));
cp.start();
}
else if (e.getClickCount() == 3)
{
cp = new Thread(new ClickProcessor(new java.util.concurrent.Callable<Void>()
{
@Override public Void call() throws Exception {
tripleClick(e); //////////////////////////////////////////
return null;
}
})
);
cp.start();
}
else manyClick(e); //////////////////////////////////////////
} // mouseClicked
} // new MouseAdapter
); // add mouseListener
} // Click123
class ClickProcessor implements Runnable
{
java.util.concurrent.Callable<Void> eventProcessor;
ClickProcessor(java.util.concurrent.Callable<Void> eventProcessor)
{
this.eventProcessor = eventProcessor;
}
@Override public void run()
{
try
{
System.out.println("About to sleep " + getFreq());
Thread.sleep(getFreq()); // this value comes from implementation
eventProcessor.call();
} catch (InterruptedException e) { System.out.println(e);}
catch (Exception e) { System.out.println(e);}
} // run
} // class ClickProcessor
} // class Click123
我从 Netbeans 得到的唯一警告是关于 "package visible inner class" ClickProcessor
.
我使用这个命令行编译了我的项目:
javac -Xlint:unchecked -classpath main\*.java gbl\*.java
它在几个地方给出了关于 "unchecked call to Click123<T>
as a member of raw type Click123
" 的警告,包括在此 class 中对它的引用:
public class GridCell extends JTextField {
int row,
col;
char content;
Color foreground,
background;
GridCell(){
content = ' ';
foreground = Color.BLACK;
background = Color.WHITE;
disableKeyCombo(KeyEvent.VK_A, KeyEvent.VK_C, KeyEvent.VK_V, KeyEvent.VK_X,
KeyEvent.VK_H);
new Click123(this) ////////// flagged warning unchecked call to Click123
{
@Override
public void singleClick(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e))
{
if( ! Game.getAvailable().contains("*"))
Game.changeSMBorder(e.getComponent().getX(),
e.getComponent().getY());
else
Game.changeSbBackground(e.getComponent().getX(),
e.getComponent().getY());
}
Game.btnClearBorders.setEnabled(true);
}
@Override public void doubleClick(MouseEvent e){
if(SwingUtilities.isRightMouseButton(e))
{
if(btnUndo.isEnabled())
btnUndo.doClick();
}
}
@Override
public void tripleClick(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e))
{
if(btnRedo.isEnabled())
btnRedo.doClick();
}
}
@Override
public void manyClick(MouseEvent e) {
}
@Override
public int getFreq() {
return CLICK_FREQUENCY;
}
};
}
... class goes on much further
}
什么,如果有的话,can/should我做什么?
您似乎没有完全使用泛型尝试:
new Click123<GridCell>(this)
How to fix unchecked call warning in Java?
尝试更改
new Click123(this)
到
new Click123<GridCell>(this)
Click123
是一个泛型 class 这意味着它有一个类型参数,写在 angular 括号之间。对于 Click123
,类型参数 T
必须是 Component
的子 class,GridCell
是。
应避免使用像 List
或 Set
这样的原始类型。原始类型是在没有类型参数的情况下使用的泛型类型。引入泛型时,只允许原始类型与泛型之前编写的代码兼容。
但是,从您的 class 来看,Click123
似乎根本没有理由应该是通用的。构造函数不能只使用 Component
而不是 T
吗?
我有这个class:
package Main;
public abstract class Click123<T extends java.awt.Component> ////////////////
{
boolean debugging = false;
public abstract void singleClick(java.awt.event.MouseEvent e); ////////////////
public abstract void doubleClick(java.awt.event.MouseEvent e); ////////////////
public abstract void tripleClick(java.awt.event.MouseEvent e); ////////////////
public abstract void manyClick(java.awt.event.MouseEvent e); ////////////////
public abstract int getFreq();// how long thread sleeps; i.e., click interval
public Click123(T target) ////////////////
{
target.addMouseListener ////////////////
(
new java.awt.event.MouseAdapter() ////////////////
{
Thread cp = null;
public void mouseClicked(final java.awt.event.MouseEvent e)
{
if (cp != null && cp.isAlive())
cp.interrupt();
if (e.getClickCount() == 1)
{
cp = new Thread(new ClickProcessor(new java.util.concurrent.Callable<Void>() {
@Override public Void call() throws Exception {
singleClick(e); //////////////////////////////////////////
return null;
}
}));
cp.start();
}
else if (e.getClickCount() == 2)
{
cp = new Thread(new ClickProcessor(new java.util.concurrent.Callable<Void>() {
@Override public Void call() throws Exception {
doubleClick(e); //////////////////////////////////////////
return null;
}
}));
cp.start();
}
else if (e.getClickCount() == 3)
{
cp = new Thread(new ClickProcessor(new java.util.concurrent.Callable<Void>()
{
@Override public Void call() throws Exception {
tripleClick(e); //////////////////////////////////////////
return null;
}
})
);
cp.start();
}
else manyClick(e); //////////////////////////////////////////
} // mouseClicked
} // new MouseAdapter
); // add mouseListener
} // Click123
class ClickProcessor implements Runnable
{
java.util.concurrent.Callable<Void> eventProcessor;
ClickProcessor(java.util.concurrent.Callable<Void> eventProcessor)
{
this.eventProcessor = eventProcessor;
}
@Override public void run()
{
try
{
System.out.println("About to sleep " + getFreq());
Thread.sleep(getFreq()); // this value comes from implementation
eventProcessor.call();
} catch (InterruptedException e) { System.out.println(e);}
catch (Exception e) { System.out.println(e);}
} // run
} // class ClickProcessor
} // class Click123
我从 Netbeans 得到的唯一警告是关于 "package visible inner class" ClickProcessor
.
我使用这个命令行编译了我的项目:
javac -Xlint:unchecked -classpath main\*.java gbl\*.java
它在几个地方给出了关于 "unchecked call to Click123<T>
as a member of raw type Click123
" 的警告,包括在此 class 中对它的引用:
public class GridCell extends JTextField {
int row,
col;
char content;
Color foreground,
background;
GridCell(){
content = ' ';
foreground = Color.BLACK;
background = Color.WHITE;
disableKeyCombo(KeyEvent.VK_A, KeyEvent.VK_C, KeyEvent.VK_V, KeyEvent.VK_X,
KeyEvent.VK_H);
new Click123(this) ////////// flagged warning unchecked call to Click123
{
@Override
public void singleClick(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e))
{
if( ! Game.getAvailable().contains("*"))
Game.changeSMBorder(e.getComponent().getX(),
e.getComponent().getY());
else
Game.changeSbBackground(e.getComponent().getX(),
e.getComponent().getY());
}
Game.btnClearBorders.setEnabled(true);
}
@Override public void doubleClick(MouseEvent e){
if(SwingUtilities.isRightMouseButton(e))
{
if(btnUndo.isEnabled())
btnUndo.doClick();
}
}
@Override
public void tripleClick(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e))
{
if(btnRedo.isEnabled())
btnRedo.doClick();
}
}
@Override
public void manyClick(MouseEvent e) {
}
@Override
public int getFreq() {
return CLICK_FREQUENCY;
}
};
}
... class goes on much further
}
什么,如果有的话,can/should我做什么?
您似乎没有完全使用泛型尝试:
new Click123<GridCell>(this)
How to fix unchecked call warning in Java?
尝试更改
new Click123(this)
到
new Click123<GridCell>(this)
Click123
是一个泛型 class 这意味着它有一个类型参数,写在 angular 括号之间。对于 Click123
,类型参数 T
必须是 Component
的子 class,GridCell
是。
应避免使用像 List
或 Set
这样的原始类型。原始类型是在没有类型参数的情况下使用的泛型类型。引入泛型时,只允许原始类型与泛型之前编写的代码兼容。
但是,从您的 class 来看,Click123
似乎根本没有理由应该是通用的。构造函数不能只使用 Component
而不是 T
吗?