From b75f2dc0453d19c799f901cda3d22b00a35f82e9 Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Tue, 5 Nov 2024 11:35:54 -0500 Subject: [PATCH] Add test, fix nls warnings Signed-off-by: Rob Stryker Fix test case Signed-off-by: Rob Stryker --- .../jdt/core/tests/model/JavaSearchTests.java | 58 +++++++++++++++++++ .../internal/core/ClassFileWorkingCopy.java | 4 +- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchTests.java index 5d0ccb28fec..ed39fd34cf3 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/JavaSearchTests.java @@ -23,12 +23,17 @@ import junit.framework.Test; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.jdt.core.*; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTParser; +import org.eclipse.jdt.core.dom.ASTRequestor; import org.eclipse.jdt.core.search.IJavaSearchConstants; import org.eclipse.jdt.core.search.IJavaSearchScope; import org.eclipse.jdt.core.search.LocalVariableDeclarationMatch; @@ -42,6 +47,7 @@ import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; import org.eclipse.jdt.internal.core.JarPackageFragmentRoot; import org.eclipse.jdt.internal.core.JavaModelStatus; +import org.eclipse.jdt.internal.core.util.HandleFactory; /** * Tests the Java search engine where results are JavaElements and source positions. @@ -4157,6 +4163,58 @@ public void testStaticImportPackage02() throws CoreException { ); } +public void testCamelCaseTypePattern_ClassFileWorkingCopy_Prereq() throws CoreException { + IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject("JavaSearch"); + IJavaProject jp = JavaCore.create(p); + IClasspathEntry[] entr2 = jp.getResolvedClasspath(true); + String jclMinPath = null; + for( int i = 0; i < entr2.length && jclMinPath == null; i++ ) { + IPath path = entr2[i].getPath(); + if( path.toString().contains("jclMin1.8.jar")) { + jclMinPath = path.toString(); + } + } + String runtimeExceptionPath = jclMinPath + "|java/lang/RuntimeException.class"; + ITypeRoot typeRootRuntimeException = (ITypeRoot)new HandleFactory().createOpenable(runtimeExceptionPath, getJavaSearchScope()); + org.eclipse.jdt.core.ICompilationUnit cuRuntimeException = typeRootRuntimeException.getWorkingCopy(null, new NullProgressMonitor()); + + + String retentionPath = jclMinPath + "|java/lang/annotation/Retention.class"; + ITypeRoot typeRootRetention = (ITypeRoot)new HandleFactory().createOpenable(retentionPath, getJavaSearchScope()); + org.eclipse.jdt.core.ICompilationUnit cuRetention = typeRootRetention.getWorkingCopy(null, new NullProgressMonitor()); + + String retentionPolicyPath = jclMinPath + "|java/lang/annotation/RetentionPolicy.class"; + ITypeRoot typeRootRetentionPolicy = (ITypeRoot)new HandleFactory().createOpenable(retentionPolicyPath, getJavaSearchScope()); + org.eclipse.jdt.core.ICompilationUnit cuRetentionPolicy = typeRootRetentionPolicy.getWorkingCopy(null, new NullProgressMonitor()); + + org.eclipse.jdt.core.ICompilationUnit[] cuArr = new org.eclipse.jdt.core.ICompilationUnit[] { + cuRuntimeException, cuRetention, cuRetentionPolicy + }; + + ASTParser astParser = ASTParser.newParser(AST.getJLSLatest()); + astParser.setCompilerOptions(jp.getOptions(true)); + astParser.setProject(jp); + astParser.setResolveBindings(true); + astParser.setBindingsRecovery(true); + astParser.createASTs(cuArr, new String[0], new ASTRequestor() { + @Override + public void acceptAST(org.eclipse.jdt.core.ICompilationUnit source, org.eclipse.jdt.core.dom.CompilationUnit ast) { + String srcString = source.toString(); + String astString = ast.toString(); + if( srcString.contains("RetentionPolicy.class") && !astString.contains("RetentionPolicy")) { + fail(); + } + if( srcString.contains("Retention.class") && !astString.contains("Retention")) { + fail(); + } + if( srcString.contains("RuntimeException.class") && !astString.contains("RuntimeException")) { + fail(); + } + } + // todo, use a subprogressmonitor or slice it + }, new NullProgressMonitor()); +} + /** * test Bug 110060: [plan][search] Add support for Camel Case search pattern * see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=110060" 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 ff239568d5a..bb727ea58f4 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 @@ -94,8 +94,8 @@ 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 pack = enclosingPackage == null ? "" : enclosingPackage.getElementName(); //$NON-NLS-1$ + String packReplaced = pack.length() > 0 ? pack.replaceAll("\\.", "/") + "/" : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ String goal = parentPath + IDependent.JAR_FILE_ENTRY_SEPARATOR + packReplaced + clazzFileName; ret = goal.toCharArray(); } else {