Bug 35987 - unused private method false positive?
Summary: unused private method false positive?
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.0 M1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-04-02 11:57 EST by Rodrigo Peretti CLA
Modified: 2003-06-02 06:12 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rodrigo Peretti CLA 2003-04-02 11:57:33 EST
Eclipse 2.1

This might be an expected/valid result but looks wrong to me:

public interface A {
  public B getB();
}

public interface B {}

class X {
  public A getA() {
    return getA_in_X();
  }

  private A getA_in_X() {
    return new A() {
      public B getB() {
        return getB_in_X();
      }
    };
  }

  private B getB_in_X() {
    return new B();
  }
}

The compiler complains that getB_in_X() is never used.
Comment 1 Philipe Mulet CLA 2003-04-03 04:48:37 EST
I cannot reproduce on your testcase. 

Also note I had to tweak the following code to get it to compile.

  private B getB_in_X() {
    return new B(){}; // added anonymous
  }
Comment 2 Rodrigo Peretti CLA 2003-04-03 11:23:43 EST
OK, I might have missed something (I haven't really tried that on Eclipse - 
sorry). Here is a more concrete example:



import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IStorageEditorInput;

public class X {

	public X() {
		getEditorInput();
	}

	/**
	 * Returns an empty storage.
	 */
	private IStorage getStorage() {
		return new IStorage() {
			public InputStream getContents() throws CoreException {
				return new ByteArrayInputStream("".getBytes());
			}
			public IPath getFullPath() {
				return null;
			}
			public String getName() {
				return null;
			}
			public boolean isReadOnly() {
				return false;
			}
			public Object getAdapter(Class adapter) {
				return null;
			}
		};
	}

	private IEditorInput getEditorInput() {
		return new IStorageEditorInput() {
			public IStorage getStorage() throws CoreException {
				return getStorage();
			}
			public boolean exists() {
				return false;
			}
			public ImageDescriptor getImageDescriptor() {
				return null;
			}
			public String getName() {
				return null;
			}
			public IPersistableElement getPersistable() {
				return null;
			}
			public String getToolTipText() {
				return null;
			}
			public Object getAdapter(Class adapter) {
				return null;
			}
		};
	}

}
Comment 3 Philipe Mulet CLA 2003-04-04 08:07:12 EST
X.getStorage() is indeed unused, since the invocation buried in:

public IStorage getStorage() throws CoreException {
  return getStorage();
}

is recursively calling itself. If you want to target X.getStorage(), then it 
should use 'X.this.getStorage();'.

Is it ok or am I missing something obvious ?
Comment 4 Rodrigo Peretti CLA 2003-04-04 09:39:14 EST
oooppsss... It seems that I was missing something obvious. :-) The funny thing 
is that it works during runtime.
Comment 5 Philipe Mulet CLA 2003-04-04 10:14:49 EST
Or you sure it doesn't perform an infinite recursion ?
Comment 6 Rodrigo Peretti CLA 2003-04-04 10:29:42 EST
Yes, I'm sure. I was using J9 - not sure if that makes a difference. I do not 
have the code in my workspace anymore but I can get that back if you want.
Comment 7 Rodrigo Peretti CLA 2003-04-04 10:31:45 EST
Wait... I might be wrong. I might have never tried running the code like that. 
Now I recall that I did have the X.this.getStorage() in my code to get rid of 
the warning.

You're right. Please close this bug and don't tell anyone about it!!! ;-)
Comment 8 Philipe Mulet CLA 2003-04-04 10:46:49 EST
Closing