Skip to content

Commit

Permalink
Fix resolution of classes in lib directory (close #65)
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Sherman <[email protected]>
  • Loading branch information
bentsherman committed Nov 21, 2024
1 parent c81e242 commit 0743fc4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
22 changes: 14 additions & 8 deletions modules/compiler/src/main/java/script/control/ResolveVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ protected boolean resolve(ClassNode type) {
return true;
if( resolveFromModule(type) )
return true;
if( resolveFromLibImports(type) )
return true;
if( !type.hasPackageName() && resolveFromDefaultImports(type) )
return true;
return resolveFromClassResolver(type.getName()) != null;
Expand Down Expand Up @@ -178,18 +180,22 @@ protected boolean resolveFromModule(ClassNode type) {
return false;
}

protected boolean resolveFromDefaultImports(ClassNode type) {
// resolve from script imports
var typeName = type.getName();
for( var cn : Types.TYPES ) {
if( typeName.equals(cn.getNameWithoutPackage()) || typeName.equals(cn.getName()) ) {
protected boolean resolveFromLibImports(ClassNode type) {
var name = type.getName();
for( var cn : libClasses ) {
if( name.equals(cn.getName()) ) {
type.setRedirect(cn);
return true;
}
}
// resolve from lib directory
for( var cn : libClasses ) {
if( typeName.equals(cn.getNameWithoutPackage()) || typeName.equals(cn.getName()) ) {
return false;
}

protected boolean resolveFromDefaultImports(ClassNode type) {
// resolve from script imports
var typeName = type.getName();
for( var cn : Types.TYPES ) {
if( typeName.equals(cn.getNameWithoutPackage()) ) {
type.setRedirect(cn);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ private List<ClassNode> getLibClasses() {
var moduleNode = sourceUnit.getAST();
if( moduleNode == null )
return;
var packageName = libDir
.relativize(Path.of(uri).getParent())
.toString()
.replaceAll("/", ".");
moduleNode.setPackageName(packageName);
for( var cn : moduleNode.getClasses() ) {
var className = packageName.isEmpty()
? cn.getNameWithoutPackage()
: packageName + "." + cn.getNameWithoutPackage();
cn.setName(className);
}
result.addAll(moduleNode.getClasses());

var entry = libCache.get(uri);
Expand Down

0 comments on commit 0743fc4

Please sign in to comment.