Skip to content

Commit

Permalink
fix: Resolve classes from classpath directories case-sensitively
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Nov 24, 2020
1 parent 4ae9261 commit 762cd08
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/main/java/org/fulib/scenarios/library/DirLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,43 @@ public DirLibrary(File source)

// =============== Methods ===============

private File getFile(String className)
private File getValidFileOrNull(String className)
{
return new File(this.getSource().getPath() + File.separatorChar + className + ".class");
final String classFileName = className.replace('/', File.separatorChar) + ".class";
final File file = new File(this.getSource(), classFileName);
if (!file.exists())
{
return null;
}

final String canonicalFile;
try
{
canonicalFile = file.getCanonicalPath();
}
catch (IOException e)
{
return null;
}

// case sensitive check
if (!canonicalFile.endsWith(classFileName))
{
return null;
}
return file;
}

@Override
public boolean hasClass(String name)
{
return this.getFile(name).exists();
return this.getValidFileOrNull(name) != null;
}

@Override
public InputStream loadClass(String name) throws IOException
{
final File file = this.getFile(name);
return file.exists() ? new FileInputStream(file) : null;
final File file = this.getValidFileOrNull(name);
return file != null ? new FileInputStream(file) : null;
}
}
23 changes: 23 additions & 0 deletions src/test/java/org/fulib/scenarios/library/DirLibraryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.fulib.scenarios.library;

import org.junit.Test;

import java.io.File;

import static org.junit.Assert.*;

public class DirLibraryTest
{

@Test
public void hasClass()
{
final File classesDir = new File("build/classes/java/test");
final DirLibrary library = new DirLibrary(classesDir);

assertTrue(library.hasClass("org/fulib/scenarios/library/DirLibraryTest"));
assertFalse(library.hasClass("org/fulib/scenarios/library/ClassThatDoesNotExist"));
assertFalse(library.hasClass("org/fulib/scenarios/library/dirlibrarytest"));
assertFalse(library.hasClass("Org/Fulib/Scenarios/Library/DirLibraryTest"));
}
}

0 comments on commit 762cd08

Please sign in to comment.