diff options
author | jay | 2018-07-29 12:22:21 +0000 |
---|---|---|
committer | jay | 2018-07-30 09:23:05 +0000 |
commit | c038db2ca25d496e8aae7de4cd959c51ab3c9149 (patch) | |
tree | 4c4a4ba82ea35845c7250f51b3f040b962a1aca4 | |
parent | f0d6c3e98ff6539a356e47a9b168d816f1f67b6a (diff) | |
download | eclipse.jdt.core-c038db2ca25d496e8aae7de4cd959c51ab3c9149.tar.gz eclipse.jdt.core-c038db2ca25d496e8aae7de4cd959c51ab3c9149.tar.xz eclipse.jdt.core-c038db2ca25d496e8aae7de4cd959c51ab3c9149.zip |
Bug 534501: Java annotation processor support is incompleteI20180730-0800
Change-Id: I061c806094fa71223fbc9f89f3e3c23707a8858d
Signed-off-by: jay <jarthana@in.ibm.com>
6 files changed, 132 insertions, 15 deletions
diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeClassOutputStream.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeClassOutputStream.java index 17295a1ac9..20ecb1bbb0 100644 --- a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeClassOutputStream.java +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeClassOutputStream.java @@ -51,15 +51,19 @@ public class IdeClassOutputStream extends ByteArrayOutputStream IBinaryType binaryType = null; try { - binaryType = ClassFileReader.read(this._file.getLocation().toString()); + try { + binaryType = ClassFileReader.read(this._file.getLocation().toString()); + } catch(IOException ioe) { + // Files doesn't yet exist + } if (binaryType == null) { saveToDisk(contents, true); } else { saveToDisk(contents, false); } binaryType = ClassFileReader.read(this._file.getLocation().toString()); - char[] name = binaryType.getName(); - ReferenceBinding type = compiler.lookupEnvironment.getType(CharOperation.splitOn('/', name)); + char[][] splitOn = CharOperation.splitOn('/', binaryType.getName()); + ReferenceBinding type = compiler.lookupEnvironment.getType(splitOn); if (type != null && type.isValidBinding()) { if (type.isBinaryBinding()) { _env.addNewClassFile(type); @@ -70,7 +74,7 @@ public class IdeClassOutputStream extends ByteArrayOutputStream } } } catch(Exception ex) { - // move on + Apt6Plugin.log(ex, "Could not create generated class file " + _file.getName()); //$NON-NLS-1$ } finally { closeInputStream(contents); @@ -81,8 +85,9 @@ public class IdeClassOutputStream extends ByteArrayOutputStream if (stream != null) { try { stream.close(); + } catch (IOException ioe) { + // Nothing to do } - catch (IOException ioe) {} } } private void saveToDisk(InputStream toSave, boolean create) throws IOException{ @@ -90,8 +95,7 @@ public class IdeClassOutputStream extends ByteArrayOutputStream FileSystemUtil.makeDerivedParentFolders(_file.getParent()); if (create) { _file.create(toSave, IResource.FORCE | IResource.DERIVED, null); - } - else { + } else { _file.setContents(toSave, true, false, null); } } @@ -99,9 +103,8 @@ public class IdeClassOutputStream extends ByteArrayOutputStream if (_file.exists()) { // Do nothing. This is a case-insensitive file system mismatch, // and the underlying platform has saved the contents already. - } - else { - Apt6Plugin.log(ce, "Could not create generated non-Java file " + _file.getName()); //$NON-NLS-1$ + } else { + Apt6Plugin.log(ce, "Could not create generated class file " + _file.getName()); //$NON-NLS-1$ throw new IOException(ce); } } diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeFilerImpl.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeFilerImpl.java index c1ff14d9f6..3a9eefc3f6 100644 --- a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeFilerImpl.java +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeFilerImpl.java @@ -68,7 +68,8 @@ public class IdeFilerImpl implements Filer { throw new IllegalArgumentException("Name is null"); } - IFile file = _env.getAptProject().getGeneratedFileManager(_env.isTestCode()).getIFileForTypeName(name.toString()); + String nameAsString = name.toString(); + IFile file = _env.getAptProject().getGeneratedFileManager(_env.isTestCode()).getIFileForTypeName(nameAsString); GeneratedSourceFolderManager gsfm = _env.getAptProject().getGeneratedSourceFolderManager(_env.isTestCode()); IPath path = null; @@ -78,11 +79,19 @@ public class IdeFilerImpl implements Filer { Apt6Plugin.log(e, "Failure getting the binary output location"); //$NON-NLS-1$ throw new IOException(e); } - file = getFileFromOutputLocation(StandardLocation.CLASS_OUTPUT, "", name + ".class"); - path = path.append(name.toString()); + int index = nameAsString.lastIndexOf('.'); + String pkg = null; + if (index != -1) { + name = nameAsString.substring(index + 1); + pkg = nameAsString.substring(0, index); + } else { + pkg = ""; + } + file = getFileFromOutputLocation(StandardLocation.CLASS_OUTPUT, pkg, name + ".class"); + path = path.append(nameAsString); path = new Path(path.toString() + ".class"); - return new IdeOutputClassFileObject(_env, file, name.toString()); + return new IdeOutputClassFileObject(_env, file, nameAsString); } /* (non-Javadoc) diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeOutputClassFileObject.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeOutputClassFileObject.java index ba48f1e62e..d9ab44abb1 100644 --- a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeOutputClassFileObject.java +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeOutputClassFileObject.java @@ -56,7 +56,7 @@ public class IdeOutputClassFileObject extends IdeOutputFileObject implements Jav @Override public OutputStream openOutputStream() throws IOException { - return new IdeClassOutputStream(_env, _file); + return new IdeClassOutputStream(_env,_file); } /* (non-Javadoc) diff --git a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/FilerTests.java b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/FilerTests.java index a1efdd4bf8..a863ef07a8 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/FilerTests.java +++ b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/FilerTests.java @@ -359,6 +359,7 @@ public class FilerTests extends TestBase } public void testCreateClass1() throws Exception { + FilerTesterProc.roundNo = 0; ProcessorTestStatus.reset(); IJavaProject jproj = createJavaProject(_projectName); disableJava5Factories(jproj); @@ -397,4 +398,44 @@ public class FilerTests extends TestBase long lastModified2 = file.lastModified(); assertTrue("file should have been overwritten", (lastModified2 > lastModified)); } + public void testCreateClass2() throws Exception { + FilerTesterProc.roundNo = 0; + ProcessorTestStatus.reset(); + IJavaProject jproj = createJavaProject(_projectName); + disableJava5Factories(jproj); + IProject proj = jproj.getProject(); + IPath projPath = proj.getFullPath(); + + env.addClass(projPath.append("src"), "p", "Trigger", + "package p;\n" + + "import org.eclipse.jdt.apt.pluggable.tests.annotations.FilerTestTrigger;\n" + + "@FilerTestTrigger(test = \"testCreateClass2\", arg0 = \"p\", arg1 = \"Test.java\")" + + "public class Trigger {\n" + + "}" + ); + AptConfig.setEnabled(jproj, true); + + fullBuild(); + final String[] expectedClasses = {"p.Trigger" }; + expectingUniqueCompiledClasses(expectedClasses); + IPath path = proj.getLocation().append("bin/p/Trigger.class"); + File file = new File(path.toOSString()); + assertTrue("File should exist", file.exists()); + long lastModified = file.lastModified(); + Thread.sleep(1000); + ClassFile[] classFiles = this.debugRequestor.getClassFiles(); + FilerTesterProc.classContent = classFiles[0].getBytes(); + env.addClass(projPath.append(".apt_generated"), "g", "Test", + "package g;\n" + + "import org.eclipse.jdt.apt.pluggable.tests.annotations.FilerTestTrigger;\n" + + "@FilerTestTrigger(test = \"testCreateClass2\",arg0 = \"g\",arg1 = \"Test.java\") " + + "public class Test { }" + ); + + incrementalBuild(); + assertEquals("should have triggered 5 rounds", 5, FilerTesterProc.roundNo); + assertTrue("File should exist", file.exists()); + long lastModified2 = file.lastModified(); + assertTrue("file should have been overwritten", (lastModified2 > lastModified)); + } } diff --git a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/processors/filertester/FilerTesterProc.java b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/processors/filertester/FilerTesterProc.java index 31cd126bf6..6da2297551 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/processors/filertester/FilerTesterProc.java +++ b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/processors/filertester/FilerTesterProc.java @@ -282,6 +282,40 @@ public class FilerTesterProc extends AbstractProcessor { } finally { } } + public void testCreateClass2(Element e, String pkg, String relName) throws Exception { + Filer filer = processingEnv.getFiler(); + try { + if (++roundNo == 1) + return; + if (roundNo == 2) { + JavaFileObject jfo = filer.createSourceFile("p/Test", e.getEnclosingElement()); + PrintWriter pw = null; + try { + pw = new PrintWriter(jfo.openWriter()); + pw.write("package p;\n " + + "import org.eclipse.jdt.apt.pluggable.tests.annotations.FilerTestTrigger;\n" + + "@FilerTestTrigger(test = \"testCreateClass1\", arg0 = \"p\", arg1 = \"Test.java\")" + + "public class Test {}"); + } finally { + pw.close(); + } + } else if(roundNo == 3) { + if (classContent == null) { + throw new IOException("Class file should have been present"); + } + IdeOutputClassFileObject jfo = (IdeOutputClassFileObject) filer.createClassFile("p.Trigger"); + OutputStream out = null; + try { + out = jfo.openOutputStream(); + out.write(classContent); + } catch (Exception ex) { + } finally { + out.close(); + } + } + } finally { + } + } private void checkGenUri(FileObject fo, String name, String content, String category) throws Exception { PrintWriter pw = null; diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathMultiDirectory.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathMultiDirectory.java index 42e06ad5e9..5de9cf4669 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathMultiDirectory.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathMultiDirectory.java @@ -11,6 +11,7 @@ package org.eclipse.jdt.internal.core.builder; import org.eclipse.core.resources.*; +import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.CompilationResult; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; @@ -67,6 +68,35 @@ protected boolean isExcluded(IResource resource) { return Util.isExcluded(resource, this.inclusionPatterns, this.exclusionPatterns); return false; } +@Override +String[] directoryList(String qualifiedPackageName) { + String[] dirList = (String[]) this.directoryCache.get(qualifiedPackageName); + if (dirList != null) return dirList; + + try { + IResource container = this.binaryFolder.findMember(qualifiedPackageName); // this is a case-sensitive check + if (container instanceof IContainer) { + IResource[] members = ((IContainer) container).members(); + dirList = new String[members.length]; + int index = 0; + for (int i = 0, l = members.length; i < l; i++) { + IResource m = members[i]; + String name = m.getName(); + if (m.getType() == IResource.FILE && org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(name)) { + // add exclusion pattern check here if we want to hide .class files + dirList[index++] = name; + } + } + if (index < dirList.length) + System.arraycopy(dirList, 0, dirList = new String[index], 0, index); + this.directoryCache.put(qualifiedPackageName, dirList); + return dirList; + } + } catch(CoreException ignored) { + // ignore + } + return null; +} @Override public String toString() { |