Open Type System

202 views
Skip to first unread message

D. Green-Lank

unread,
Nov 9, 2010, 2:23:16 AM11/9/10
to gosu-lang
You've heard some talk about the open type system, and I believe there
is at least one example of using it in the samples available for
download. But if you'd like to get up and running quickly playing with
this feature of the language, just add the Gosu jars and libs to your
classpath, and compile and run the following code (it contains its own
"main" method)... This will create a typeloader that creates a single
type called "foo.MyType" with a single method called "sayHello" that
accepts a string as a parameter, and returns a string... It then
pushes that typeloader onto the global typeloader list, and then
launches the built-in Gosu editor... At this point you can execute a
line of code to use your newly created type such as
"print( foo.MyType.sayHello( 'David' ) )"... Enjoy

import gw.lang.reflect.*;
import gw.lang.reflect.java.IJavaType;
import gw.lang.shell.Gosu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class Main {

public static void main( String... args ) throws Exception {
Gosu.main( "-e", "print( 'foo' )" ); // initialize Gosu - I'm sure
there's a better way
TypeSystem.pushGlobalTypeLoader( new MyTypeLoader() );
Gosu.main( "-g" );
}

private static class MyTypeLoader extends TypeLoaderBase {
@Override
public IType getType( String fullyQualifiedName ) {
return new MyType( this );
}

@Override
public Set<? extends CharSequence> getAllTypeNames() {
return Collections.singleton( "foo.MyType" );
}

@Override
public List<String> getHandledPrefixes() {
return Collections.emptyList();
}

}

private static class MyType extends TypeBase {

private final ITypeLoader _loader;

public MyType( ITypeLoader loader ) {
_loader = loader;
}

@Override
public String getName() {
return "foo.MyType";
}

@Override
public String getRelativeName() {
return "MyType";
}

@Override
public String getNamespace() {
return "foo";
}

@Override
public ITypeLoader getTypeLoader() {
return _loader;
}

@Override
public IType getSupertype() {
return IJavaType.OBJECT;
}

@Override
public List<? extends IType> getInterfaces() {
return Collections.emptyList();
}

@Override
public ITypeInfo getTypeInfo() {
return new MyTypeInfo( this );
}

}

private static class MyTypeInfo extends TypeInfoBase {

private final IType _ownersType;

public MyTypeInfo( IType ownersType ) {
_ownersType = ownersType;
}

@Override
public List<? extends IPropertyInfo> getProperties() {
return Collections.emptyList();
}

@Override
public IPropertyInfo getProperty( CharSequence propName ) {
return null;
}

@Override
public CharSequence getRealPropertyName( CharSequence propName ) {
return null;
}

@Override
public List<? extends IMethodInfo> getMethods() {
List<IMethodInfo> methods = new ArrayList<IMethodInfo>();
methods.add( new MethodInfoBuilder()
.withStatic()
.withName( "sayHello" )
.withParameters( new
ParameterInfoBuilder().withName( "who" ).withType( IJavaType.STRING ) )
.withReturnType( TypeSystem.get( String.class ) ) //
same as IJavaType.STRING - could also use
TypeSystem.getByFullName( "java.lang.String" )
.withCallHandler( new IMethodCallHandler() {
@Override
public Object handleCall( Object ctx, Object... args )
{
return "Hi, " + args[0];
}
} )
.build( this ) );
return methods;
}

@Override
public List<? extends IConstructorInfo> getConstructors() {
return Collections.emptyList();
}

@Override
public List<IAnnotationInfo> getDeclaredAnnotations() {
return Collections.emptyList();
}

@Override
public IType getOwnersType() {
return _ownersType;
}
}

}

D. Green-Lank

unread,
Nov 9, 2010, 3:13:04 AM11/9/10
to gosu-lang
One correction, getType( String fullyQualifiedName ) should probably
check that fullyQualifiedName is equal to "foo.MyType", otherwise
return null.

JP

unread,
Nov 19, 2010, 8:08:23 AM11/19/10
to gosu-lang
Thanks for the sample! I was able to get some TypeLoading done on my
own, including trying out the sites Dynamic example with code
completion.

There doesn't seem to be a way to accomplish TypeLoading in Eclipse?
Or am I just missing something?

D. Green-Lank

unread,
Nov 19, 2010, 4:43:01 PM11/19/10
to gosu-lang
I don't think Eclipse respects typeloaders yet. The built-in Gosu
editor does, however. Just be sure to launch it using the main() from
the example above, rather than using gosu.sh. There's also a special
manifest entry you can add to any Jar file in the classpath in order
to get other typeloaders added automatically, "Gosu-Typeloaders",
which should be set to a space-delimited list of fully qualified class
names of typeloaders to include in the typesystem.

David

Scott McKinney

unread,
Nov 19, 2010, 7:38:14 PM11/19/10
to gosu-lang
Sure, the Eclipse plug-in supports Gosu type loaders. Check out the
UseCustomDynamicType example that employs the DynamicType loader.

Scott
> > > > }- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages