Skip to content

Commit

Permalink
Fixes #3215 - ClassFileWorkingCopy.getFileName() does not follow API doc
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
Rob Stryker committed Nov 5, 2024
1 parent b5a589f commit 647ab15
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModelStatusConstants;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
import org.eclipse.jdt.core.util.IClassFileReader;
import org.eclipse.jdt.internal.compiler.env.IDependent;
import org.eclipse.jdt.internal.compiler.env.IElementInfo;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.core.util.Disassembler;
Expand Down Expand Up @@ -79,6 +83,26 @@ public IPath getPath() {
return this.classFile.getPath();
}

@Override
public char[] getFileName(){
char[] ret = null;
PackageFragmentRoot root = getPackageFragmentRoot();
if (root == null)
// working copy not in workspace
ret = new Path(getElementName()).toString().toCharArray();
else if (root.isArchive()) {
String clazzFileName = this.classFile.getElementName();
String parentPath = this.classFile.getParent().getPath().toString();
IPackageFragment enclosingPackage = (IPackageFragment)getAncestor(IJavaElement.PACKAGE_FRAGMENT);
String pack = enclosingPackage == null ? "" : enclosingPackage.getElementName();
String packReplaced = pack.length() > 0 ? pack.replaceAll("\\.", "/") + "/" : "";
String goal = parentPath + IDependent.JAR_FILE_ENTRY_SEPARATOR + packReplaced + clazzFileName;
ret = goal.toCharArray();
} else {
ret = getParent().getPath().append(getElementName()).toString().toCharArray();
}
return ret;
}
@Override
public JavaElement getPrimaryElement(boolean checkOwner) {
if (checkOwner && isPrimary()) return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ public char[] getContents() {
public CompilationUnit originalFromClone() {
return CompilationUnit.this;
}
@Override
public char[] getFileName(){
return originalFromClone().getFileName();
}

};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@
import org.eclipse.jdt.internal.core.Annotation;
import org.eclipse.jdt.internal.core.ClassFile;
import org.eclipse.jdt.internal.core.JavaElement;
import org.eclipse.jdt.internal.core.JavaModel;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.Member;
import org.eclipse.jdt.internal.core.MemberValuePair;
import org.eclipse.jdt.internal.core.PackageFragment;
Expand Down Expand Up @@ -1001,6 +1003,9 @@ public static IPackageFragment getPackageFragment(char[] fileName, int pkgEnd, i
if (jarSeparator != -1) {
String jarMemento = new String(fileName, 0, jarSeparator);
PackageFragmentRoot root = (PackageFragmentRoot) JavaCore.create(jarMemento);
if( root == null ) {
root = getJarPkgFragmentRoot(new String(fileName), jarSeparator, jarMemento);
}
if (pkgEnd == jarSeparator)
return root.getPackageFragment(CharOperation.NO_STRINGS);
char[] pkgName = CharOperation.subarray(fileName, jarSeparator+1, pkgEnd);
Expand All @@ -1026,6 +1031,41 @@ public static IPackageFragment getPackageFragment(char[] fileName, int pkgEnd, i
}
}

// Copied from org.eclipse.jdt.internal.core.util.HandleFactory
private static PackageFragmentRoot getJarPkgFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPathString) {
IPath jarPath= new Path(jarPathString);
Object target = JavaModel.getTarget(jarPath, false);
if (target instanceof IFile jarFile) {
// internal jar: is it on the classpath of its project?
// e.g. org.eclipse.swt.win32/ws/win32/swt.jar
// is NOT on the classpath of org.eclipse.swt.win32
JavaProject javaProject = (JavaProject) JavaModelManager.getJavaModelManager().getJavaModel().getJavaProject(jarFile);
try {
IClasspathEntry entry = javaProject.getClasspathEntryFor(jarPath);
if (entry != null) {
return (PackageFragmentRoot) javaProject.getPackageFragmentRoot(jarFile);
}
} catch (JavaModelException e) {
// ignore and try to find another project
}
} else if( target instanceof File) {
try {
// Have to search, I guess.
IJavaProject[] javaProjects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
for( int i = 0; i < javaProjects.length; i++ ) {
IClasspathEntry entry = javaProjects[i].getClasspathEntryFor(jarPath);
if (entry != null) {
return (PackageFragmentRoot) javaProjects[i].getPackageFragmentRoot(jarPathString);
}
}
} catch(JavaModelException jme) {
// TODO ignore
}

}
return null;
}

/**
* Returns the number of parameter types in a method signature.
*/
Expand Down

0 comments on commit 647ab15

Please sign in to comment.