当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Field not used warning in Eclipse

问题描述:

I'm implementing a login filter in a JSF application. I have a private field called config. Eclipse displays a warning "The value of the field AuthenticationFilter.config is not used" despite the field being referred to in the init and destroy methods. Am I doing something wrong here or is Eclipse a bit confused?

Here's the AuthenticationFilter class:

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;

}

}

网友答案:

Initialising and destroying config aren't "using" them - the compiler is telling you that you've got a variable that you're wasting memory and time initialising and destroying when it isn't being used anywhere.

网友答案:

I think if you just assign it to null will solve your problem

private FilterConfig config = null;

Sometimes I have this problem too. I think it's the because of the way FilterConfig class is implemented.

Although since you assign the pointer to another one, I don't see why there is a problem. Another quick fix: put some getters/setters, even though you might not use them :D

UPDATE: Also, what davidhxxx said

网友答案:

Using a field is not giving a value to it.
In your code, you give a value to the field but you never use this field.

In your case, if your field has a value but that this valued field is never used as input value in another instruction, it seems to be useless. The field has a value but nobody uses this value.
So, why declaring it ? Whereas, the warning.

In your code config is never used. why declaring it ?
Maybe, your class build is in progress and you will need it.
Maybe, you never need to store theconfig object to use it later. So you can remove the field in your class.
Question yourself about it .

分享给朋友:
您可能感兴趣的文章:
随机阅读: