java - Eclipse 中未使用字段警告

标签 java eclipse

我正在 JSF 应用程序中实现登录过滤器。我有一个名为 config 的私有(private)字段。尽管在 init 和 destroy 方法中引用了该字段,Eclipse 仍显示警告“未使用字段 AuthenticationFilter.config 的值”。我在这里做错了什么,还是 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并不是“使用”它们 - 编译器告诉您,您有一个变量,您在初始化和销毁​​时浪费了内存和时间,但实际上它不是在任何地方都可以使用。

关于java - Eclipse 中未使用字段警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38540122/

相关文章:

android - 尝试播放音频时,我的mediaplayer类出现运行时错误(Android/Eclipse)

Java 应用程序 : Unable to read iso-8859-1 encoded file correctly

java - Android Java JNI 和 C Char 数组无法识别值

eclipse - 用于 Perl 开发的 IDE 最重要的功能是什么?

Android Manifest 实现问题

c++ - 如何使用 Eclipse Luna 在 CDT 中使用构建/运行配置

java - 如何在Java中获取深度嵌套的JSON对象

java - 添加到工作项目后未显示 Google map

java - 将值存储在数组中未指定的索引处。什么更快?

android - Facebook 登录 NullPointerException