From c43fb2e27fe0f10c9ddf199528a4addb9baed965 Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Wed, 30 Oct 2024 10:58:55 -0400 Subject: [PATCH] Fix testCamelCaseTypePattern01_CamelCase and many others Signed-off-by: Rob Stryker --- .../internal/core/ClassFileWorkingCopy.java | 28 ++++++++++++ .../jdt/internal/core/CompilationUnit.java | 5 +++ .../eclipse/jdt/internal/core/util/Util.java | 44 ++++++++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClassFileWorkingCopy.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClassFileWorkingCopy.java index 9c3cfc4b937..55b5c97c03e 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClassFileWorkingCopy.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClassFileWorkingCopy.java @@ -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; @@ -79,6 +83,30 @@ public IPath getPath() { return this.classFile.getPath(); } +@Override +public char[] getFileName(){ + // BIG PROBLEM HERE!!! + // IJavaElement#getPath() has different specs than + // org.eclipse.jdt.internal.compiler.env.IDependent#getFileName() + //return getPath().toString().toCharArray(); + 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; diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java index 18938da6c40..6fd92e4f8c0 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java @@ -320,6 +320,11 @@ public char[] getContents() { public CompilationUnit originalFromClone() { return CompilationUnit.this; } + @Override + public char[] getFileName(){ + return originalFromClone().getFileName(); + } + }; } diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java index d64ef2a63bd..9718bb3f231 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java @@ -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; @@ -808,7 +810,8 @@ private static IClassFile getClassFile(char[] fileName) { IPackageFragment pkg = getPackageFragment(fileName, pkgEnd, jarSeparator); if (pkg == null) return null; int start; - return pkg.getClassFile(new String(fileName, start = pkgEnd + 1, fileName.length - start)); + IClassFile cz = pkg.getClassFile(new String(fileName, start = pkgEnd + 1, fileName.length - start)); + return cz; } private static ICompilationUnit getCompilationUnit(char[] fileName, WorkingCopyOwner workingCopyOwner) { @@ -1000,7 +1003,11 @@ private static String getLineSeparator(char[] text, char[] buffer) { public static IPackageFragment getPackageFragment(char[] fileName, int pkgEnd, int jarSeparator) { if (jarSeparator != -1) { String jarMemento = new String(fileName, 0, jarSeparator); + // TODO this is where shit is blowing up, 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); @@ -1026,6 +1033,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. */