使用GWT延迟绑定模拟Java反射
以下是我的一些代码: 我的模块包含:使用GWT延迟绑定模拟Java反射,gwt,reflection,panel,dynamic,Gwt,Reflection,Panel,Dynamic,以下是我的一些代码: 我的模块包含: <generate-with class="com.bilancio.ReflectionGenerator"> <when-type-assignable class="com.bilancio.client.Reflection" /> </generate-with> public interface Constructable {} public interface Reflection {
<generate-with class="com.bilancio.ReflectionGenerator">
<when-type-assignable class="com.bilancio.client.Reflection" />
</generate-with>
public interface Constructable {}
public interface Reflection {
public <T> T instantiate( String className );
}
public class ReflectionGenerator extends Generator
{
@Override
public String generate( TreeLogger logger, GeneratorContext
context, String typeName ) throws UnableToCompleteException
{
TypeOracle oracle = context.getTypeOracle( );
JClassType markerInterface =
oracle.findType( Constructable.class.getName( ) );
List<JClassType> clazzes = new ArrayList<JClassType>( );
PropertyOracle propertyOracle = context.getPropertyOracle( );
for ( JClassType classType : oracle.getTypes( ) )
{
if ( !classType.equals( markerInterface ) &&
classType.isAssignableTo( markerInterface ) )
clazzes.add( classType );
}
final String genPackageName = "com.bilancio.client";
final String genClassName = "ReflectionImpl";
ClassSourceFileComposerFactory composer = new
ClassSourceFileComposerFactory( genPackageName, genClassName );
composer.addImplementedInterface( Reflection.class.getCanonicalName( ) );
composer.addImport( "com.bilancio.client.*" );
PrintWriter printWriter = context.tryCreate( logger,
genPackageName, genClassName );
if ( printWriter != null )
{
SourceWriter sourceWriter =
composer.createSourceWriter( context, printWriter );
sourceWriter.println( "ReflectionImpl( ) {" );
sourceWriter.println( "}" );
printFactoryMethod( clazzes, sourceWriter );
sourceWriter.commit( logger );
}
return composer.getCreatedClassName( );
}
private void printFactoryMethod( List<JClassType> clazzes,
SourceWriter sourceWriter )
{
sourceWriter.println( );
sourceWriter.println( "public <T> T instantiate( String
className ) {" );
for ( JClassType classType : clazzes )
{
if ( classType.isAbstract( ) )
continue;
sourceWriter.println( );
sourceWriter.indent( );
sourceWriter.println( "if( className.equals(\"" +
classType.getName( ) + "\")) {" );
sourceWriter.indent( );
sourceWriter.println( "return new " +
classType.getQualifiedSourceName( ) + "();" );
sourceWriter.outdent( );
sourceWriter.println( "}" );
sourceWriter.outdent( );
sourceWriter.println( );
}
sourceWriter.println( );
sourceWriter.outdent( );
sourceWriter.println( "}" );
sourceWriter.outdent( );
sourceWriter.println( );
}
}
这是我的追踪:
[DEBUG] [main] - Rebinding com.bilancio.client.Reflection
[DEBUG] [main] - Adding '1' new generated units
[DEBUG] [main] - Validating newly compiled units
[ERROR] [main] - Errors in 'generated://670356D9DA0D417BB8171499B4C4D57B/com/bilancio/client/ReflectionImpl.java'
[ERROR] [main] - Line 9: This method must return a result of type T
[INFO] [main] - See snapshot: /tmp/com.bilancio.client.ReflectionImpl6488844579312624283.java
解决方法如下:
我缺少默认返回值^ ^在什么时候出现此错误?在编译生成的类时?您是否有异常跟踪?项目生成正确,但在模块加载时出现错误,我将尽快发布跟踪!你觉得塔希尔怎么样?
[DEBUG] [main] - Rebinding com.bilancio.client.Reflection
[DEBUG] [main] - Adding '1' new generated units
[DEBUG] [main] - Validating newly compiled units
[ERROR] [main] - Errors in 'generated://670356D9DA0D417BB8171499B4C4D57B/com/bilancio/client/ReflectionImpl.java'
[ERROR] [main] - Line 9: This method must return a result of type T
[INFO] [main] - See snapshot: /tmp/com.bilancio.client.ReflectionImpl6488844579312624283.java