Skip to content

Commit

Permalink
Fix for #640: resolve star import pack/type for code select/highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 3, 2018
1 parent 213c240 commit 0dba166
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
*/
public class ImportNode extends AnnotatedNode implements Opcodes {

private final ClassNode type;
private ClassNode type;
private final String alias;
private final String fieldName;
// TODO use PackageNode instead here?
private final String packageName;
private final boolean isStar;
private final boolean isStatic;
Expand Down Expand Up @@ -171,6 +170,10 @@ public void setFieldNameExpr(ConstantExpression fieldNameExpr) {
this.fieldNameExpr = fieldNameExpr;
}

public void setType(ClassNode type) {
this.type = type;
}

public String toString() {
return super.toString() + '[' + getText() + ']';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,19 @@ public void visitClass(ClassNode node) {
addError("unable to resolve class " + type.getName(), type);
}
*/
for (ImportNode importNode : module.getStarImports()) {
if (importNode.getEnd() > 0) {
String importName = importNode.getPackageName();
importName = importName.substring(0, importName.length()-1);
ClassNode type = ClassHelper.makeWithoutCaching(importName);
if (resolve(type, false, false, true)) {
importNode.setType(type);
type.setStart(importNode.getStart() + 7);
type.setEnd(type.getStart() + importName.length());
}
}
}
// GRECLIPSE end
for (ImportNode importNode : module.getStaticImports().values()) {
ClassNode type = importNode.getType();
if (resolve(type, true, true, true)) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public class ImportNode extends AnnotatedNode implements Opcodes {

private final ClassNode type;
private ClassNode type;
private final String alias;
private final String fieldName;
private final String packageName;
Expand Down Expand Up @@ -170,6 +170,10 @@ public void setFieldNameExpr(ConstantExpression fieldNameExpr) {
this.fieldNameExpr = fieldNameExpr;
}

public void setType(ClassNode type) {
this.type = type;
}

public String toString() {
return super.toString() + '[' + getText() + ']';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,9 @@ protected Expression transformDeclarationExpression(DeclarationExpression de) {
newDeclExpr.setDeclaringClass(de.getDeclaringClass());
fixDeclaringClass(newDeclExpr);
newDeclExpr.setSourcePosition(de);
// GRECLIPSE add
newDeclExpr.copyNodeMetaData(de);
// GRECLIPSE end
newDeclExpr.addAnnotations(de.getAnnotations());
return newDeclExpr;
}
Expand Down Expand Up @@ -1500,6 +1503,19 @@ public void visitClass(ClassNode node) {
addError("unable to resolve class " + type.getName(), type);
}
*/
for (ImportNode importNode : module.getStarImports()) {
if (importNode.getEnd() > 0) {
String importName = importNode.getPackageName();
importName = importName.substring(0, importName.length()-1);
ClassNode type = ClassHelper.makeWithoutCaching(importName);
if (resolve(type, false, false, true)) {
importNode.setType(type);
type.setStart(importNode.getStart() + 7);
type.setEnd(type.getStart() + importName.length());
}
}
}
// GRECLIPSE end
for (ImportNode importNode : module.getStaticImports().values()) {
ClassNode type = importNode.getType();
if (resolve(type, true, true, true)) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public class ImportNode extends AnnotatedNode implements Opcodes {

private final ClassNode type;
private ClassNode type;
private final String alias;
private final String fieldName;
private final String packageName;
Expand Down Expand Up @@ -170,6 +170,10 @@ public void setFieldNameExpr(ConstantExpression fieldNameExpr) {
this.fieldNameExpr = fieldNameExpr;
}

public void setType(ClassNode type) {
this.type = type;
}

public String toString() {
return super.toString() + '[' + getText() + ']';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,19 @@ public void visitClass(ClassNode node) {
addError("unable to resolve class " + type.getName(), type);
}
*/
for (ImportNode importNode : module.getStarImports()) {
if (importNode.getEnd() > 0) {
String importName = importNode.getPackageName();
importName = importName.substring(0, importName.length()-1);
ClassNode type = ClassHelper.makeWithoutCaching(importName);
if (resolve(type, false, false, true)) {
importNode.setType(type);
type.setStart(importNode.getStart() + 7);
type.setEnd(type.getStart() + importName.length());
}
}
}
// GRECLIPSE end
for (ImportNode importNode : module.getStaticImports().values()) {
ClassNode type = importNode.getType();
if (resolve(type, true, true, true)) continue;
Expand Down Expand Up @@ -1590,7 +1603,6 @@ private void checkCyclicInheritance(ClassNode originalNode, ClassNode parentToCo
for(ClassNode intf : interfacesToCompare) {
checkCyclicInheritance(originalNode, null, intf.getInterfaces());
}
} else {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@ import org.junit.Test
final class CodeSelectImportsTests extends BrowsingTestSuite {

@Test
void testCodeSelectOnImportType() {
void testCodeSelectOnImportType1() {
String source = '''\
import java.util.regex.Pattern
Pattern p = ~/123/
Expand All @@ -32,7 +32,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectOnImportType1a() {
void testCodeSelectOnImportType2() {
String source = '''\
import java.util.regex.Matcher
import java.util.regex.Pattern
Expand All @@ -44,7 +44,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectOnImportType2() {
void testCodeSelectOnImportType3() {
String source = '''\
import java.lang.Thread.State
import java.util.regex.Pattern
Expand All @@ -56,7 +56,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectOnImportType2a() {
void testCodeSelectOnImportType4() {
String source = '''\
import java.lang.Thread.State
def p = ~/123/
Expand All @@ -65,6 +65,25 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
assertCodeSelect([source], 'Thread')
}

@Test
void testCodeSelectOnImportType5() {
String source = '''\
import java.lang.Thread.*
'''.stripIndent()

assertCodeSelect([source], 'Thread')
}

@Test
void testCodeSelectOnImportType6() {
String source = '''\
import java.util.Map.Entry.*
'''.stripIndent()

assertCodeSelect([source], 'Map')
assertCodeSelect([source], 'Entry')
}

@Test
void testCodeSelectOnImportPackage1() {
String source = '''\
Expand All @@ -76,7 +95,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectOnImportPackage1a() {
void testCodeSelectOnImportPackage2() {
String source = '''\
import java.lang.Thread.State
def p = ~/123/
Expand All @@ -86,7 +105,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectOnImportPackage2() {
void testCodeSelectOnImportPackage3() {
String source = '''\
import java.util.regex.*
Pattern p = ~/123/
Expand All @@ -96,7 +115,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectOnImportPackage2a() {
void testCodeSelectOnImportPackage4() {
String source = '''\
import java.lang.Thread.*
def p = ~/123/
Expand All @@ -116,7 +135,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectOnImportWildcard1a() {
void testCodeSelectOnImportWildcard2() {
String source = '''\
import java.util.regex.*;
Pattern p = ~/123/
Expand All @@ -126,7 +145,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectOnImportWildcard2() {
void testCodeSelectOnImportWildcard3() {
String source = '''\
import static java.util.regex.Pattern.*
def p = compile('123')
Expand All @@ -136,7 +155,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
}

@Test
void testCodeSelectOnImportWildcard2a() {
void testCodeSelectOnImportWildcard4() {
String source = '''\
import static java.util.regex.Pattern.*;
def p = compile('123')
Expand Down

0 comments on commit 0dba166

Please sign in to comment.