繁体   English   中英

Eclipse中未使用字段警告

[英]Field not used warning in Eclipse

我正在JSF应用程序中实现登录过滤器。 我有一个名为config的私有字段。 Eclipse将显示警告“未使用AuthenticationFilter.config字段的值”,尽管init和destroy方法中已引用该字段。 我在这里做错什么了吗?还是Eclipse有点困惑?

这是AuthenticationFilter类:

package com.mymato.coop;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/restricted/*")
public class AuthenticationFilter implements Filter {

      private FilterConfig config;

      public void doFilter(ServletRequest req, ServletResponse resp,
          FilterChain chain) throws IOException, ServletException {
        if (((HttpServletRequest) req).getSession().getAttribute(
            LoginBean.AUTH_KEY) == null) {
          ((HttpServletResponse) resp).sendRedirect("../login.xhtml");
        } else {
          chain.doFilter(req, resp);
        }
      }

      public void init(FilterConfig config) throws ServletException {
        this.config = config;
      }

      public void destroy() {
        config = null;
      }
    }

初始化和销毁config不是“使用”它们-编译器告诉您,您有一个变量在浪费内存,而在任何地方都没有使用它时会进行初始化和销毁​​。

使用字段并没有赋予它任何价值。
在您的代码中,您为该字段提供了一个值,但是您从未使用过该字段。

在您的情况下,如果您的字段具有值,但是该值字段从未在另一条指令中用作输入值,则它似乎没有用。 该字段具有一个值,但是没有人使用该值。
那么,为什么要宣布呢? 而警告。

在您的代码config中,从未使用过。 为什么要宣布呢?
也许您的班级建设正在进行中,您将需要它。
也许,您永远不需要存储config对象以供以后使用。 因此,您可以在课程中删除该字段。
问问自己。

我认为,只要将其分配为null即可解决您的问题

private FilterConfig config = null;

有时候我也有这个问题。 我认为这是因为FilterConfig类的实现方式所致。

尽管既然您将指针分配给另一个指针,但我不明白为什么会出现问题。 另一个快速解决方案:即使您可能不使用吸气剂/吸气剂,也要放入它们:D

更新 :另外, davidhxxx所说的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM