Fixes a problem introduced post 1.4. In order to get package level annotations to work, ICompilationUnitAdapter.getMainTypeName() needed to return the simple name of the main type in the compilation unit. The work around of parsing the string returned by CompilationUnitProvider.getLocation() would return the package name instead of the simple type name when a generated class had ".java" as part of its package name. In that scenario, JDT would report a class must be defined in its own file error for the generated type.
The attached patch adds a getMainTypeName() method to CompilationUnitProvider since it does know what the main type name should be. This patch does not do anything with regards to the value returned by CompilationUnitProvider.getLocation () for generated CUPs.
Patch by: mmendez
Review by: scottb
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1502 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/core/ext/typeinfo/CompilationUnitProvider.java b/dev/core/src/com/google/gwt/core/ext/typeinfo/CompilationUnitProvider.java
index 33ec087..d727965 100644
--- a/dev/core/src/com/google/gwt/core/ext/typeinfo/CompilationUnitProvider.java
+++ b/dev/core/src/com/google/gwt/core/ext/typeinfo/CompilationUnitProvider.java
@@ -37,6 +37,8 @@
String getLocation();
+ String getMainTypeName();
+
String getPackageName();
char[] getSource() throws UnableToCompleteException;
diff --git a/dev/core/src/com/google/gwt/dev/jdt/CompilationUnitProviderWithAlternateSource.java b/dev/core/src/com/google/gwt/dev/jdt/CompilationUnitProviderWithAlternateSource.java
index 88c508b..adb8005 100644
--- a/dev/core/src/com/google/gwt/dev/jdt/CompilationUnitProviderWithAlternateSource.java
+++ b/dev/core/src/com/google/gwt/dev/jdt/CompilationUnitProviderWithAlternateSource.java
@@ -42,6 +42,10 @@
return cup.getLocation();
}
+ public String getMainTypeName() {
+ return cup.getMainTypeName();
+ }
+
public String getPackageName() {
return cup.getPackageName();
}
diff --git a/dev/core/src/com/google/gwt/dev/jdt/ICompilationUnitAdapter.java b/dev/core/src/com/google/gwt/dev/jdt/ICompilationUnitAdapter.java
index 0eac5cf..03c078c 100644
--- a/dev/core/src/com/google/gwt/dev/jdt/ICompilationUnitAdapter.java
+++ b/dev/core/src/com/google/gwt/dev/jdt/ICompilationUnitAdapter.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2006 Google Inc.
+ * Copyright 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@@ -21,8 +21,6 @@
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
-import java.io.File;
-
/**
* Implements <code>ICompilationUnit</code> in terms of a
* {@link CompilationUnitProvider}.
@@ -53,41 +51,17 @@
}
/**
- * This method is supposed to return the simple class name for
- * this compilation unit. Examples of simple class names would
- * be "String", or "ArrayList". JDT allows this method to return
- * null in the cases where this compilation unit is not a package-info
- * class.
+ * This method is supposed to return the simple class name for this
+ * compilation unit. Examples of simple class names would be "String", or
+ * "ArrayList". JDT allows this method to return null in the cases where this
+ * compilation unit is not a package-info class.
*/
public char[] getMainTypeName() {
-
- // Note that cup.getLocation() can either return a path, a URL,
- // or "transient source for ..."
- String mainTypeName = cup.getLocation();
-
- // Let's check to see if we are dealing with a path to a java file,
- // or something else
-
- int ext = mainTypeName.lastIndexOf(".java");
- if (ext == -1) {
- // not a path to a java file, return null
- return null;
+ String typeName = cup.getMainTypeName();
+ if (typeName != null) {
+ return typeName.toCharArray();
}
-
- mainTypeName = mainTypeName.substring(0, ext);
-
- int nameStart = mainTypeName.lastIndexOf(File.separatorChar);
-
- // We're not dealing with a path, so check for the URL separator
- if (nameStart == -1) {
- nameStart = mainTypeName.lastIndexOf('/');
- }
-
- // If we do not find a separator, then this is a simple name. The
- // substring call will act as a no-op.
- mainTypeName = mainTypeName.substring(nameStart + 1);
-
- return mainTypeName.toCharArray();
+ return null;
}
public char[][] getPackageName() {
diff --git a/dev/core/src/com/google/gwt/dev/jdt/StaticCompilationUnitProvider.java b/dev/core/src/com/google/gwt/dev/jdt/StaticCompilationUnitProvider.java
index b43e33d..75e4ef3 100644
--- a/dev/core/src/com/google/gwt/dev/jdt/StaticCompilationUnitProvider.java
+++ b/dev/core/src/com/google/gwt/dev/jdt/StaticCompilationUnitProvider.java
@@ -53,6 +53,10 @@
return "transient source for " + packageName + "." + simpleTypeName;
}
+ public String getMainTypeName() {
+ return getTypeName();
+ }
+
public String getPackageName() {
return packageName;
}
diff --git a/dev/core/src/com/google/gwt/dev/jdt/URLCompilationUnitProvider.java b/dev/core/src/com/google/gwt/dev/jdt/URLCompilationUnitProvider.java
index 86455e3..0277dc1 100644
--- a/dev/core/src/com/google/gwt/dev/jdt/URLCompilationUnitProvider.java
+++ b/dev/core/src/com/google/gwt/dev/jdt/URLCompilationUnitProvider.java
@@ -58,6 +58,8 @@
private final URL url;
+ private final String mainTypeName;
+
public URLCompilationUnitProvider(URL url, String packageName) {
assert (url != null);
assert (packageName != null);
@@ -65,11 +67,21 @@
// Files are faster to work with, so use file if available.
this.file = trySimplify(url);
+ String simpleTypeName;
if (file == null) {
this.location = url.toExternalForm();
+ simpleTypeName = new File(url.getPath()).getName();
} else {
this.location = this.file.getAbsolutePath();
+ simpleTypeName = this.file.getName();
}
+
+ int i = simpleTypeName.lastIndexOf(".java");
+ if (i != -1) {
+ simpleTypeName = simpleTypeName.substring(0, i);
+ }
+ mainTypeName = simpleTypeName;
+
this.packageName = packageName;
}
@@ -94,6 +106,10 @@
return location;
}
+ public String getMainTypeName() {
+ return mainTypeName;
+ }
+
public String getPackageName() {
return packageName;
}
@@ -121,6 +137,7 @@
return false;
}
+ @Override
public String toString() {
return location;
}
diff --git a/dev/core/test/com/google/gwt/dev/jdt/test/ByteCodeCompilerTest.java b/dev/core/test/com/google/gwt/dev/jdt/test/ByteCodeCompilerTest.java
index 7af4a7d..d17cb5c 100644
--- a/dev/core/test/com/google/gwt/dev/jdt/test/ByteCodeCompilerTest.java
+++ b/dev/core/test/com/google/gwt/dev/jdt/test/ByteCodeCompilerTest.java
@@ -69,6 +69,10 @@
return "transient source for " + packageName + "." + firstTypeName;
}
+ public String getMainTypeName() {
+ return null;
+ }
+
public String getPackageName() {
return packageName;
}
diff --git a/dev/core/test/com/google/gwt/dev/typeinfo/test/TypeOracleBuilderTest.java b/dev/core/test/com/google/gwt/dev/typeinfo/test/TypeOracleBuilderTest.java
index 323141d..9bf5e19 100644
--- a/dev/core/test/com/google/gwt/dev/typeinfo/test/TypeOracleBuilderTest.java
+++ b/dev/core/test/com/google/gwt/dev/typeinfo/test/TypeOracleBuilderTest.java
@@ -39,6 +39,10 @@
public class TypeOracleBuilderTest extends TestCase {
private static abstract class TestCup implements CompilationUnitProvider {
+ private final String packageName;
+
+ private final String[] typeNames;
+
public TestCup(String packageName, String onlyTypeName) {
this(packageName, new String[] {onlyTypeName});
}
@@ -63,6 +67,10 @@
+ this.typeNames[0];
}
+ public String getMainTypeName() {
+ return null;
+ }
+
public String getPackageName() {
return packageName;
}
@@ -70,13 +78,9 @@
public String[] getTypeNames() {
return typeNames;
}
-
public boolean isTransient() {
return true;
}
-
- private final String packageName;
- private final String[] typeNames;
}
private static Map<String, TestCup> publicTypeNameToTestCupMap = new HashMap<String, TestCup>();
@@ -108,159 +112,6 @@
publicTypeNameToTestCupMap.put(qName, cup);
}
- public void checkTypes(JClassType[] types) throws NotFoundException {
- for (int i = 0; i < types.length; i++) {
- JClassType type = types[i];
- check(type);
-
- JClassType[] nestedTypes = type.getNestedTypes();
- checkTypes(nestedTypes);
- }
- }
-
- public void testAssimilation() throws UnableToCompleteException {
- TypeOracle typeOracle0 = new TypeOracle();
- TreeLogger logger = createTreeLogger();
-
- // Build onto an empty type oracle.
- //
- TypeOracleBuilder builder1 = new TypeOracleBuilder(typeOracle0);
- builder1.addCompilationUnit(CU_Object);
- builder1.addCompilationUnit(CU_BeforeAssimilate);
- TypeOracle typeOracle1 = builder1.build(logger);
- assertSame(typeOracle0, typeOracle1);
- assertEquals(2, typeOracle1.getTypes().length);
- JClassType before = typeOracle1.findType("test.assim.BeforeAssimilate");
-
- // Build onto an existing type oracle.
- //
- TypeOracleBuilder builder2 = new TypeOracleBuilder(typeOracle1);
- builder2.addCompilationUnit(CU_AfterAssimilate);
- TypeOracle typeOracle2 = builder2.build(logger);
- assertSame(typeOracle1, typeOracle2);
- assertEquals(3, typeOracle2.getTypes().length);
-
- // Make sure identities remained intact across the assimilation.
- //
- JClassType after = typeOracle2.findType("test.assim.AfterAssimilate");
-
- assertSame(before, after.getSuperclass());
- }
-
- public void testBindToTypeScope() throws TypeOracleException,
- UnableToCompleteException {
- TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
- tiob.addCompilationUnit(CU_Object);
- tiob.addCompilationUnit(CU_BindToTypeScope);
- TypeOracle tio = tiob.build(createTreeLogger());
- JClassType[] types = tio.getTypes();
- assertEquals(4, types.length);
- checkTypes(types);
- }
-
- public void testDefaultClass() throws TypeOracleException,
- UnableToCompleteException {
- TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
- tiob.addCompilationUnit(CU_Object);
- tiob.addCompilationUnit(CU_DefaultClass);
- TypeOracle tio = tiob.build(createTreeLogger());
- JClassType[] types = tio.getTypes();
- assertEquals(2, types.length);
- checkTypes(types);
- }
-
- public void testFieldsAndTypes() throws TypeOracleException,
- UnableToCompleteException {
- TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
- tiob.addCompilationUnit(CU_Object);
- tiob.addCompilationUnit(CU_FieldsAndTypes);
- TypeOracle tio = tiob.build(createTreeLogger());
- JClassType[] types = tio.getTypes();
- assertEquals(3, types.length);
- checkTypes(types);
- }
-
- public void testMetaData() throws TypeOracleException,
- UnableToCompleteException {
- TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
- tiob.addCompilationUnit(CU_Object);
- tiob.addCompilationUnit(CU_MetaData);
- TypeOracle tio = tiob.build(createTreeLogger());
- JClassType[] types = tio.getTypes();
- assertEquals(2, types.length);
- checkTypes(types);
- }
-
- public void testMethodsAndParams() throws TypeOracleException,
- UnableToCompleteException {
- TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
- tiob.addCompilationUnit(CU_Object);
- tiob.addCompilationUnit(CU_Throwable);
- tiob.addCompilationUnit(CU_MethodsAndParams);
- TypeOracle tio = tiob.build(createTreeLogger());
- JClassType[] types = tio.getTypes();
- assertEquals(3, types.length);
- checkTypes(types);
- }
-
- public void testOuterInner() throws TypeOracleException,
- UnableToCompleteException {
- TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
- tiob.addCompilationUnit(CU_Object);
- tiob.addCompilationUnit(CU_OuterInner);
- TypeOracle tio = tiob.build(createTreeLogger());
- JClassType[] types = tio.getTypes();
- assertEquals(3, types.length);
- checkTypes(types);
- }
-
- public void testSyntaxErrors() throws TypeOracleException,
- UnableToCompleteException {
- TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
- tiob.addCompilationUnit(CU_Object);
- tiob.addCompilationUnit(CU_HasSyntaxErrors);
- TypeOracle tio = tiob.build(createTreeLogger());
- JClassType[] types = tio.getTypes();
- // Only java.lang.Object should remain.
- //
- assertEquals(1, types.length);
- assertEquals("java.lang.Object", types[0].getQualifiedSourceName());
- checkTypes(types);
- }
-
- public void testUnresolvedSymbls() throws TypeOracleException,
- UnableToCompleteException {
- TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
- tiob.addCompilationUnit(CU_Object);
- tiob.addCompilationUnit(CU_HasUnresolvedSymbols);
- tiob.addCompilationUnit(CU_RefsInfectedCompilationUnit);
- TypeOracle tio = tiob.build(createTreeLogger());
- JClassType[] types = tio.getTypes();
- // Only java.lang.Object should remain.
- //
- assertEquals(1, types.length);
- assertEquals("java.lang.Object", types[0].getQualifiedSourceName());
- checkTypes(types);
- }
-
- /**
- * Tweak this if you want to see the log output.
- */
- private TreeLogger createTreeLogger() {
- boolean reallyLog = false;
- if (reallyLog) {
- AbstractTreeLogger logger = new PrintWriterTreeLogger();
- logger.setMaxDetail(TreeLogger.ALL);
- return logger;
- } else {
- return TreeLogger.NULL;
- }
- }
-
- private TypeOracleBuilder createTypeInfoOracleBuilder() {
- return new TypeOracleBuilder();
- }
-
protected TestCup CU_AfterAssimilate = new TestCup("test.assim",
new String[] {"AfterAssimilate"}) {
public void check(JClassType type) {
@@ -765,4 +616,157 @@
return sb.toString().toCharArray();
}
};
+
+ public void checkTypes(JClassType[] types) throws NotFoundException {
+ for (int i = 0; i < types.length; i++) {
+ JClassType type = types[i];
+ check(type);
+
+ JClassType[] nestedTypes = type.getNestedTypes();
+ checkTypes(nestedTypes);
+ }
+ }
+
+ public void testAssimilation() throws UnableToCompleteException {
+ TypeOracle typeOracle0 = new TypeOracle();
+ TreeLogger logger = createTreeLogger();
+
+ // Build onto an empty type oracle.
+ //
+ TypeOracleBuilder builder1 = new TypeOracleBuilder(typeOracle0);
+ builder1.addCompilationUnit(CU_Object);
+ builder1.addCompilationUnit(CU_BeforeAssimilate);
+ TypeOracle typeOracle1 = builder1.build(logger);
+ assertSame(typeOracle0, typeOracle1);
+ assertEquals(2, typeOracle1.getTypes().length);
+ JClassType before = typeOracle1.findType("test.assim.BeforeAssimilate");
+
+ // Build onto an existing type oracle.
+ //
+ TypeOracleBuilder builder2 = new TypeOracleBuilder(typeOracle1);
+ builder2.addCompilationUnit(CU_AfterAssimilate);
+ TypeOracle typeOracle2 = builder2.build(logger);
+ assertSame(typeOracle1, typeOracle2);
+ assertEquals(3, typeOracle2.getTypes().length);
+
+ // Make sure identities remained intact across the assimilation.
+ //
+ JClassType after = typeOracle2.findType("test.assim.AfterAssimilate");
+
+ assertSame(before, after.getSuperclass());
+ }
+
+ public void testBindToTypeScope() throws TypeOracleException,
+ UnableToCompleteException {
+ TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
+ tiob.addCompilationUnit(CU_Object);
+ tiob.addCompilationUnit(CU_BindToTypeScope);
+ TypeOracle tio = tiob.build(createTreeLogger());
+ JClassType[] types = tio.getTypes();
+ assertEquals(4, types.length);
+ checkTypes(types);
+ }
+
+ public void testDefaultClass() throws TypeOracleException,
+ UnableToCompleteException {
+ TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
+ tiob.addCompilationUnit(CU_Object);
+ tiob.addCompilationUnit(CU_DefaultClass);
+ TypeOracle tio = tiob.build(createTreeLogger());
+ JClassType[] types = tio.getTypes();
+ assertEquals(2, types.length);
+ checkTypes(types);
+ }
+
+ public void testFieldsAndTypes() throws TypeOracleException,
+ UnableToCompleteException {
+ TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
+ tiob.addCompilationUnit(CU_Object);
+ tiob.addCompilationUnit(CU_FieldsAndTypes);
+ TypeOracle tio = tiob.build(createTreeLogger());
+ JClassType[] types = tio.getTypes();
+ assertEquals(3, types.length);
+ checkTypes(types);
+ }
+
+ public void testMetaData() throws TypeOracleException,
+ UnableToCompleteException {
+ TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
+ tiob.addCompilationUnit(CU_Object);
+ tiob.addCompilationUnit(CU_MetaData);
+ TypeOracle tio = tiob.build(createTreeLogger());
+ JClassType[] types = tio.getTypes();
+ assertEquals(2, types.length);
+ checkTypes(types);
+ }
+
+ public void testMethodsAndParams() throws TypeOracleException,
+ UnableToCompleteException {
+ TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
+ tiob.addCompilationUnit(CU_Object);
+ tiob.addCompilationUnit(CU_Throwable);
+ tiob.addCompilationUnit(CU_MethodsAndParams);
+ TypeOracle tio = tiob.build(createTreeLogger());
+ JClassType[] types = tio.getTypes();
+ assertEquals(3, types.length);
+ checkTypes(types);
+ }
+
+ public void testOuterInner() throws TypeOracleException,
+ UnableToCompleteException {
+ TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
+ tiob.addCompilationUnit(CU_Object);
+ tiob.addCompilationUnit(CU_OuterInner);
+ TypeOracle tio = tiob.build(createTreeLogger());
+ JClassType[] types = tio.getTypes();
+ assertEquals(3, types.length);
+ checkTypes(types);
+ }
+
+ public void testSyntaxErrors() throws TypeOracleException,
+ UnableToCompleteException {
+ TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
+ tiob.addCompilationUnit(CU_Object);
+ tiob.addCompilationUnit(CU_HasSyntaxErrors);
+ TypeOracle tio = tiob.build(createTreeLogger());
+ JClassType[] types = tio.getTypes();
+ // Only java.lang.Object should remain.
+ //
+ assertEquals(1, types.length);
+ assertEquals("java.lang.Object", types[0].getQualifiedSourceName());
+ checkTypes(types);
+ }
+
+ public void testUnresolvedSymbls() throws TypeOracleException,
+ UnableToCompleteException {
+ TypeOracleBuilder tiob = createTypeInfoOracleBuilder();
+ tiob.addCompilationUnit(CU_Object);
+ tiob.addCompilationUnit(CU_HasUnresolvedSymbols);
+ tiob.addCompilationUnit(CU_RefsInfectedCompilationUnit);
+ TypeOracle tio = tiob.build(createTreeLogger());
+ JClassType[] types = tio.getTypes();
+ // Only java.lang.Object should remain.
+ //
+ assertEquals(1, types.length);
+ assertEquals("java.lang.Object", types[0].getQualifiedSourceName());
+ checkTypes(types);
+ }
+
+ /**
+ * Tweak this if you want to see the log output.
+ */
+ private TreeLogger createTreeLogger() {
+ boolean reallyLog = false;
+ if (reallyLog) {
+ AbstractTreeLogger logger = new PrintWriterTreeLogger();
+ logger.setMaxDetail(TreeLogger.ALL);
+ return logger;
+ } else {
+ return TreeLogger.NULL;
+ }
+ }
+
+ private TypeOracleBuilder createTypeInfoOracleBuilder() {
+ return new TypeOracleBuilder();
+ }
}