Skip to content

Commit

Permalink
Use the correct path separator when dealing with filenames. For file …
Browse files Browse the repository at this point in the history
…system files we must use File.separatorChar, but for jar files we must use a forward slash regardless of the system.

	Change on 2015/04/27 by kstanger <kstanger@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=92176574
  • Loading branch information
kstanger authored and tomball committed Apr 30, 2015
1 parent 422726d commit d4e3b76
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 38 deletions.
3 changes: 3 additions & 0 deletions translator/Makefile
Expand Up @@ -160,6 +160,9 @@ JAVA_SOURCES = \
ast/VariableDeclarationFragment.java \ ast/VariableDeclarationFragment.java \
ast/VariableDeclarationStatement.java \ ast/VariableDeclarationStatement.java \
ast/WhileStatement.java \ ast/WhileStatement.java \
file/InputFile.java \
file/JarredInputFile.java \
file/RegularInputFile.java \
gen/AbstractSourceGenerator.java \ gen/AbstractSourceGenerator.java \
gen/GenerationUnit.java \ gen/GenerationUnit.java \
gen/JavadocGenerator.java \ gen/JavadocGenerator.java \
Expand Down
Expand Up @@ -79,10 +79,9 @@ private static InputFile getFileForName(String name) {
return null; return null;
} }


String sourceName = name.replace('.', File.separatorChar) + ".java";
InputFile inputFile = null; InputFile inputFile = null;
try { try {
inputFile = FileUtil.findOnSourcePath(sourceName); inputFile = FileUtil.findOnSourcePath(name);
} catch (IOException e) { } catch (IOException e) {
ErrorUtil.warning(e.getMessage()); ErrorUtil.warning(e.getMessage());
} }
Expand All @@ -92,20 +91,19 @@ private static InputFile getFileForName(String name) {
} }


// Check if the source file is older than the generated header file. // Check if the source file is older than the generated header file.
File headerSource = new File(Options.getOutputDirectory(), sourceName.replace(".java", ".h")); File headerSource =
new File(Options.getOutputDirectory(), name.replace('.', File.separatorChar) + ".h");
if (headerSource.exists() && inputFile.lastModified() < headerSource.lastModified()) { if (headerSource.exists() && inputFile.lastModified() < headerSource.lastModified()) {
return null; return null;
} }


return inputFile; return inputFile;
} }


private static boolean findClassFile(String typeName) { private static boolean findClassFile(String name) {
// Zip/jar files always use forward slashes.
String path = typeName.replace('.', File.separatorChar) + ".class";
InputFile f = null; InputFile f = null;
try { try {
f = FileUtil.findOnClassPath(path); f = FileUtil.findOnClassPath(name);
} catch (IOException e) { } catch (IOException e) {
ErrorUtil.warning(e.getMessage()); ErrorUtil.warning(e.getMessage());
} }
Expand All @@ -114,7 +112,7 @@ private static boolean findClassFile(String typeName) {
} }
// See if it's a JRE class. // See if it's a JRE class.
try { try {
Class.forName(typeName); Class.forName(name);
return true; return true;
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
// Fall-through. // Fall-through.
Expand Down
Expand Up @@ -128,7 +128,10 @@ private void processJavaFile(String filename) {
inputFile = new RegularInputFile(filename, filename); inputFile = new RegularInputFile(filename, filename);


if (!inputFile.exists()) { if (!inputFile.exists()) {
inputFile = FileUtil.findOnSourcePath(filename); // Convert to a qualified name and search on the sourcepath.
String qualifiedName =
filename.substring(0, filename.length() - 5).replace(File.separatorChar, '.');
inputFile = FileUtil.findOnSourcePath(qualifiedName);


if (inputFile == null) { if (inputFile == null) {
ErrorUtil.error("No such file: " + filename); ErrorUtil.error("No such file: " + filename);
Expand Down
Expand Up @@ -29,8 +29,7 @@ public static CompilationUnit convertCompilationUnit(
org.eclipse.jdt.core.dom.CompilationUnit jdtUnit, InputFile inputFile, String source, org.eclipse.jdt.core.dom.CompilationUnit jdtUnit, InputFile inputFile, String source,
NameTable.Factory nameTableFactory) { NameTable.Factory nameTableFactory) {
return new CompilationUnit( return new CompilationUnit(
jdtUnit, inputFile, FileUtil.getClassNameFromFilePath(inputFile.getUnitName()), source, jdtUnit, inputFile, FileUtil.getMainTypeName(inputFile), source, nameTableFactory);
nameTableFactory);
} }


public static Statement convertStatement(org.eclipse.jdt.core.dom.Statement jdtStatement) { public static Statement convertStatement(org.eclipse.jdt.core.dom.Statement jdtStatement) {
Expand Down
Expand Up @@ -52,5 +52,12 @@ public interface InputFile {
*/ */
String getUnitName(); String getUnitName();


/**
* Gets name of the file stripped of any path components.
* For example, the basename of "package/path/SourceFile.java" is
* "SourceFile.java".
*/
String getBasename();

long lastModified(); long lastModified();
} }
Expand Up @@ -71,6 +71,11 @@ public String getUnitName() {
return internalPath; return internalPath;
} }


@Override
public String getBasename() {
return internalPath.substring(internalPath.lastIndexOf('/') + 1);
}

@Override @Override
public long lastModified() { public long lastModified() {
return new File(jarPath).lastModified(); return new File(jarPath).lastModified();
Expand Down
Expand Up @@ -61,6 +61,11 @@ public String getUnitName() {
return unitPath; return unitPath;
} }


@Override
public String getBasename() {
return unitPath.substring(unitPath.lastIndexOf(File.separatorChar) + 1);
}

@Override @Override
public long lastModified() { public long lastModified() {
return new File(path).lastModified(); return new File(path).lastModified();
Expand Down
Expand Up @@ -40,23 +40,20 @@
*/ */
public class FileUtil { public class FileUtil {


/** public static String getMainTypeName(InputFile file) {
* Gets the name of the file, stripped of any directory or extension. String basename = file.getBasename();
*/ int end = basename.lastIndexOf(".java");
public static String getClassNameFromFilePath(String sourceFileName) { if (end == -1) {
int begin = sourceFileName.lastIndexOf(File.separatorChar) + 1; end = basename.lastIndexOf(".class");
// Also check for /, since this may be a jar'd source when translating on Windows. }
int n = sourceFileName.lastIndexOf('/') + 1; if (end != -1) {
if (n > begin) { basename = basename.substring(0, end);
begin = n;
} }
int end = sourceFileName.lastIndexOf(".java"); return basename;
String className = sourceFileName.substring(begin, end);
return className;
} }


public static String getQualifiedMainTypeName(InputFile file, CompilationUnit unit) { public static String getQualifiedMainTypeName(InputFile file, CompilationUnit unit) {
String qualifiedName = FileUtil.getClassNameFromFilePath(file.getUnitName()); String qualifiedName = getMainTypeName(file);
PackageDeclaration packageDecl = unit.getPackage(); PackageDeclaration packageDecl = unit.getPackage();
if (packageDecl != null) { if (packageDecl != null) {
String packageName = packageDecl.getName().getFullyQualifiedName(); String packageName = packageDecl.getName().getFullyQualifiedName();
Expand All @@ -71,8 +68,8 @@ public static String getQualifiedMainTypeName(InputFile file, CompilationUnit un
* Returns a file guaranteed to exist, or null. * Returns a file guaranteed to exist, or null.
*/ */
@Nullable @Nullable
public static InputFile findOnSourcePath(String filename) throws IOException { public static InputFile findOnSourcePath(String qualifiedName) throws IOException {
return findOnPaths(filename, Options.getSourcePathEntries()); return findOnPaths(qualifiedName, Options.getSourcePathEntries(), ".java");
} }


/** /**
Expand All @@ -81,22 +78,26 @@ public static InputFile findOnSourcePath(String filename) throws IOException {
* Returns a file guaranteed to exist, or null. * Returns a file guaranteed to exist, or null.
*/ */
@Nullable @Nullable
public static InputFile findOnClassPath(String filename) throws IOException { public static InputFile findOnClassPath(String qualifiedName) throws IOException {
return findOnPaths(filename, Options.getClassPathEntries()); return findOnPaths(qualifiedName, Options.getClassPathEntries(), ".class");
} }


private static InputFile findOnPaths(String filename, List<String> paths) throws IOException { private static InputFile findOnPaths(
String qualifiedName, List<String> paths, String extension) throws IOException {
String sourceFileName = qualifiedName.replace('.', File.separatorChar) + extension;
// Zip/jar files always use forward slashes.
String jarEntryName = qualifiedName.replace('.', '/') + extension;
for (String pathEntry : paths) { for (String pathEntry : paths) {
File f = new File(pathEntry); File f = new File(pathEntry);
if (f.isDirectory()) { if (f.isDirectory()) {
RegularInputFile regularFile = new RegularInputFile( RegularInputFile regularFile = new RegularInputFile(
pathEntry + File.separatorChar + filename, filename); pathEntry + File.separatorChar + sourceFileName, sourceFileName);
if (regularFile.exists()) { if (regularFile.exists()) {
return regularFile; return regularFile;
} }
} else { } else {
// Assume it's a jar file // Assume it's a jar file
JarredInputFile jarFile = new JarredInputFile(pathEntry, filename); JarredInputFile jarFile = new JarredInputFile(pathEntry, jarEntryName);
if (jarFile.exists()) { if (jarFile.exists()) {
return jarFile; return jarFile;
} }
Expand Down
Expand Up @@ -888,15 +888,13 @@ public String getPrefix(IPackageBinding packageBinding) {
*/ */
private static String getPrefixFromPackageInfoSource(IPackageBinding packageBinding) { private static String getPrefixFromPackageInfoSource(IPackageBinding packageBinding) {
try { try {
String expectedPackageInfoPath = packageBinding.getName(); String qualifiedName = "package-info";
String packageName = packageBinding.getName();
// Path will be null if this is the empty package. // Path will be null if this is the empty package.
if (expectedPackageInfoPath == null) { if (packageName != null) {
expectedPackageInfoPath = "package-info.java"; qualifiedName = packageName + '.' + qualifiedName;
} else {
expectedPackageInfoPath = expectedPackageInfoPath.replace('.', File.separatorChar)
+ File.separatorChar + "package-info.java";
} }
InputFile file = FileUtil.findOnSourcePath(expectedPackageInfoPath); InputFile file = FileUtil.findOnSourcePath(qualifiedName);
if (file != null) { if (file != null) {
String pkgInfo = FileUtil.readFile(file); String pkgInfo = FileUtil.readFile(file);
int i = pkgInfo.indexOf("@ObjectiveCName"); int i = pkgInfo.indexOf("@ObjectiveCName");
Expand Down

0 comments on commit d4e3b76

Please sign in to comment.