Skip to content

Commit

Permalink
Fix some test cases where a source unit's name is not a file path
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Stryker <[email protected]>

Cleanup

Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
Rob Stryker committed Apr 24, 2024
1 parent 43a949a commit 0ef7ada
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.tools.FileObject;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
Expand All @@ -43,6 +44,7 @@
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.javac.JavacUtils;
import org.eclipse.jdt.internal.javac.VirtualFileObject;
import org.eclipse.jdt.internal.javac.dom.FindNextJavadocableSibling;

import com.sun.tools.javac.file.JavacFileManager;
Expand Down Expand Up @@ -145,15 +147,16 @@ public CompilationUnit toCompilationUnit(org.eclipse.jdt.internal.compiler.env.I
boolean initialNeedsToResolveBinding, IJavaProject project, List<Classpath> classpaths,
NodeSearcher nodeSearcher, int apiLevel, Map<String, String> compilerOptions,
WorkingCopyOwner workingCopyOwner, WorkingCopyOwner typeRootWorkingCopyOwner, int flags, IProgressMonitor monitor) {
// For comparison
//CompilationUnit res2 = CompilationUnitResolver.FACADE.toCompilationUnit(sourceUnit, initialNeedsToResolveBinding, project, classpaths, nodeSearcher, apiLevel, compilerOptions, typeRootWorkingCopyOwner, typeRootWorkingCopyOwner, flags, monitor);

// TODO currently only parse
CompilationUnit res = parse(new org.eclipse.jdt.internal.compiler.env.ICompilationUnit[] { sourceUnit},
apiLevel, compilerOptions, flags, project, monitor).get(sourceUnit);
if (initialNeedsToResolveBinding) {
resolveBindings(res);
}
// For comparison
// CompilationUnit res2 = CompilationUnitResolver.FACADE.toCompilationUnit(sourceUnit, initialNeedsToResolveBinding, project, classpaths, nodeSearcher, apiLevel, compilerOptions, typeRootWorkingCopyOwner, typeRootWorkingCopyOwner, flags, monitor);
// //res.typeAndFlags=res2.typeAndFlags;
//new CompilationUnitComparator(apiLevel).compare(res, res2);
// String res1a = res.toString();
// String res2a = res2.toString();
//
Expand Down Expand Up @@ -181,13 +184,17 @@ private Map<org.eclipse.jdt.internal.compiler.env.ICompilationUnit, CompilationU
JavacUtils.configureJavacContext(context, compilerOptions, javaProject);
var fileManager = (JavacFileManager)context.get(JavaFileManager.class);
for (var sourceUnit : sourceUnits) {
Path sourceUnitPath;
if (sourceUnit.getFileName() == null || sourceUnit.getFileName().length == 0) {
sourceUnitPath = Path.of(new File("whatever.java").toURI());
} else {
sourceUnitPath = Path.of(new File(new String(sourceUnit.getFileName())).toURI());
JavaFileObject fileObject = null;
if( sourceUnit.getFileName() != null || sourceUnit.getFileName().length != 0 ) {
Path p1 = toOSPath(sourceUnit);
if( p1 != null ) {
fileObject = fileManager.getJavaFileObject(p1);
}
}
if( fileObject == null) {
fileObject = new VirtualFileObject(sourceUnit, Kind.OTHER);
}
var fileObject = fileManager.getJavaFileObject(sourceUnitPath);

fileManager.cache(fileObject, CharBuffer.wrap(sourceUnit.getContents()));
AST ast = createAST(compilerOptions, apiLevel, context);
ast.setDefaultNodeFlag(ASTNode.ORIGINAL);
Expand Down Expand Up @@ -242,6 +249,19 @@ private Optional<CompilationUnit> findTargetDOM(Map<JavaFileObject, CompilationU
return Optional.empty();
}

private static Path toOSPath(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit) {
String unitPath = new String(sourceUnit.getFileName());
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(org.eclipse.core.runtime.Path.fromPortableString(new String(sourceUnit.getFileName())));
if (file.isAccessible()) {
return file.getLocation().toPath();
}
File tentativeOSFile = new File(unitPath);
if (tentativeOSFile.isFile()) {
return tentativeOSFile.toPath();
}
return null;
}

private AST createAST(Map<String, String> options, int level, Context context) {
AST ast = AST.newAST(level, JavaCore.ENABLED.equals(options.get(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES)));
String sourceModeSetting = options.get(JavaCore.COMPILER_SOURCE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.eclipse.jdt.internal.javac;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;

import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;

import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;

public class VirtualFileObject implements JavaFileObject {
private ICompilationUnit sourceUnit;
private Kind kind;
public VirtualFileObject(ICompilationUnit sourceUnit, Kind kind) {
this.sourceUnit = sourceUnit;
this.kind = kind;
}

@Override
public URI toUri() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getName() {
return new String(sourceUnit.getFileName());
}

@Override
public InputStream openInputStream() throws IOException {
// TODO Auto-generated method stub
return null;
}

@Override
public OutputStream openOutputStream() throws IOException {
// TODO Auto-generated method stub
return null;
}

@Override
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
// TODO Auto-generated method stub
return null;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append(this.sourceUnit.getContents());
return sb;
}

@Override
public Writer openWriter() throws IOException {
// TODO Auto-generated method stub
return null;
}

@Override
public long getLastModified() {
// TODO Auto-generated method stub
return 0;
}

@Override
public boolean delete() {
return false;
}

@Override
public Kind getKind() {
return this.kind;
}

@Override
public boolean isNameCompatible(String simpleName, Kind kind) {
// TODO Auto-generated method stub
return false;
}

@Override
public NestingKind getNestingKind() {
// TODO Auto-generated method stub
return null;
}

@Override
public Modifier getAccessLevel() {
// TODO Auto-generated method stub
return null;
}

}

0 comments on commit 0ef7ada

Please sign in to comment.