不抽象,不覆盖抽象方法

时间:2014-07-30 17:45:20

标签: java build

我对这段Java代码有疑问。在尝试构建此代码所属的程序时,我得到了这个错误:

        [javac] C:\eRA\cc284-src\reporting\jsp\test\net\sourceforge\cruisecontrol\mock\MockServletRequest.java:59: error: MockServletRequest is not abstract and does not override abstract method <T>upgrade(Class<T>) in HttpServletRequest

这是我的代码:

  package net.sourceforge.cruisecontrol.mock;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.UnsupportedEncodingException;
  import java.security.Principal;
  import java.util.Enumeration;
  import java.util.Locale;
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Collections;
  import java.util.ArrayList;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletInputStream;
  import javax.servlet.http.Cookie;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;


public class MockServletRequest implements HttpServletRequest {
private String contextPath = "";
private String servletPath;
private Map params = new HashMap();
private String pathInfo;
private Locale locale;


public void httpservletrequest() {
}


public MockServletRequest() {
}

public MockServletRequest(String contextPath, String servletPath) {
    this.contextPath = contextPath;
    this.servletPath = servletPath;
}

public Object getAttribute(String name) {
    return null;
}

public Enumeration getAttributeNames() {
    return null;
}

public String getCharacterEncoding() {
    return null;
}

public int getContentLength() {
    return 0;
}

public String getContentType() {
    return null;
}

public ServletInputStream getInputStream() throws IOException {
    return null;
}

public String getParameter(String paramName) {
    ArrayList values = (ArrayList) params.get(paramName);
    if (values == null || values.isEmpty()) {
        return null;    // param not found
    }
    return (String) values.get(0);
}

public Enumeration getParameterNames() {
    return Collections.enumeration(params.keySet());
}

public String[] getParameterValues(String paramName) {
    ArrayList values = (ArrayList) params.get(paramName);
    if (values == null || values.isEmpty()) {
        return null;    // param not found
    }
    return (String[]) values.toArray(new String[] {});
}

public String getProtocol() {
    return null;
}

public String getScheme() {
    return null;
}

public String getServerName() {
    return null;
}

public int getServerPort() {
    return 0;
}

public BufferedReader getReader() throws IOException {
    return null;
}

public String getRemoteAddr() {
    return null;
}

public String getRemoteHost() {
    return null;
}

public void setAttribute(String s, Object o) {
}

public void removeAttribute(String s) {
}

public Locale getLocale() {
    return locale;
}

public Enumeration getLocales() {
    return null;
}

public boolean isSecure() {
    return false;
}

public RequestDispatcher getRequestDispatcher(String s) {
    return null;
}

public String getRealPath(String s) {
    return null;
}

public String getAuthType() {
    return null;
}

public Cookie[] getCookies() {
    return new Cookie[0];
}

public long getDateHeader(String s) {
    return 0;
}

public String getHeader(String s) {
    return null;
}

public Enumeration getHeaders(String s) {
    return null;
}

public Enumeration getHeaderNames() {
    return null;
}

public int getIntHeader(String s) {
    return 0;
}

public String getMethod() {
    return null;
}

public String getPathInfo() {
    return pathInfo;
}

public String getPathTranslated() {
    return null;
}

public String getContextPath() {
    return (contextPath.length() == 0 ? "" : "/" + contextPath);
}

public String getQueryString() {
    return null;
}

public String getRemoteUser() {
    return null;
}

public boolean isUserInRole(String s) {
    return false;
}

public Principal getUserPrincipal() {
    return null;
}

public String getRequestedSessionId() {
    return null;
}

public String getRequestURI() {
    return null;
}

public String getServletPath() {
    return "/" + servletPath;
}

public HttpSession getSession(boolean b) {
    return null;
}

public HttpSession getSession() {
    return null;
}

public boolean isRequestedSessionIdValid() {
    return false;
}

public boolean isRequestedSessionIdFromCookie() {
    return false;
}

public boolean isRequestedSessionIdFromURL() {
    return false;
}

public boolean isRequestedSessionIdFromUrl() {
    return false;
}

public void addParameter(String paramName, String paramValue) {
    ArrayList values = (ArrayList) params.get(paramName);
    if (values == null) {
        values = new ArrayList();
        params.put(paramName, values);
    }
    values.add(paramValue);
}

public ArrayList removeParameter(String paramName) {
    ArrayList values = (ArrayList) params.remove(paramName);
    return values;
}

public void setPathInfo(String info) {
    pathInfo = info;
}

/**
 * @param locale
 */
public void setLocale(Locale locale) {
    this.locale = locale;
}

public StringBuffer getRequestURL() {
    return null;
}

public String getLocalAddr() {
    return null;
}

public String getLocalName() {
    return null;
}

public int getLocalPort() {
    return 0;
}

public Map getParameterMap() {
    return null;
}

public int getRemotePort() {
    return 0;
}

每当我尝试将其声明为抽象时,我会收到另一个错误,说它无法实现。

3 个答案:

答案 0 :(得分:0)

解构错误消息

MockServletRequest is not abstract and does not override abstract method <T>upgrade(Class<T>) in HttpServletRequest

它会告诉你以下内容:

HttpServletRequest具有抽象方法“upgrade(Class)”。由于您正在实现该类,因此需要实现该方法。

错误消息也告诉您,您还可以调用 MockServletRequest摘要(更新方法)。这会将实现要求传递给更高的对象(但查看代码可能不是您想要的)。

编译器抱怨,因为你没有做过这些事情。

答案 1 :(得分:0)

HttpServletRequest类有一个升级(Class)方法,在实现HttpServletRequest类时必须重写/实现该方法。 你需要一个看起来像这样的方法:

public <T extends HttpUpgradeHandler> T upgrade(Class<T> handlerClass) throws IOException, ServletException {
    // your code here
}

答案 2 :(得分:-1)

如果MockServletRequest将覆盖该抽象方法(正如编译器所要求的那样),请确保您未将MockServletRequest.update()声明为抽象。也就是说,将其声明为public void update(),而不是public abstract void update()