Skip to content

Commit

Permalink
Moved metamodel util into its own Metamodel class #226
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Apr 17, 2013
1 parent a21f63a commit f4677af
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 132 deletions.
4 changes: 2 additions & 2 deletions runtime/ceylon/language/metamodel/type_.java
@@ -1,10 +1,10 @@
package ceylon.language.metamodel;

import com.redhat.ceylon.compiler.java.Util;
import com.redhat.ceylon.compiler.java.metadata.Ceylon;
import com.redhat.ceylon.compiler.java.metadata.Method;
import com.redhat.ceylon.compiler.java.metadata.Name;
import com.redhat.ceylon.compiler.java.metadata.TypeInfo;
import com.redhat.ceylon.compiler.java.runtime.metamodel.Metamodel;

@Ceylon(major = 4)
@Method
Expand All @@ -16,6 +16,6 @@ private type_() {}
public static ProducedType type(@Name("instance")
@TypeInfo("ceylon.language::Anything")
final Object instance) {
return Util.getMetamodel(Util.getTypeDescriptor(instance));
return Metamodel.getMetamodel(Metamodel.getTypeDescriptor(instance));
}
}
130 changes: 5 additions & 125 deletions runtime/com/redhat/ceylon/compiler/java/Util.java
@@ -1,39 +1,25 @@
package com.redhat.ceylon.compiler.java;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ceylon.language.ArraySequence;
import ceylon.language.Iterable;
import ceylon.language.Iterator;
import ceylon.language.Null;
import ceylon.language.Ranged;
import ceylon.language.Sequential;
import ceylon.language.empty_;
import ceylon.language.finished_;

import com.redhat.ceylon.cmr.api.ArtifactResult;
import com.redhat.ceylon.cmr.api.Logger;
import com.redhat.ceylon.cmr.api.RepositoryManager;
import com.redhat.ceylon.cmr.api.RepositoryManagerBuilder;
import com.redhat.ceylon.compiler.java.metadata.Ceylon;
import com.redhat.ceylon.compiler.java.metadata.Class;
import com.redhat.ceylon.compiler.java.metadata.SatisfiedTypes;
import com.redhat.ceylon.compiler.java.runtime.model.ReifiedType;
import com.redhat.ceylon.compiler.java.runtime.model.RuntimeModuleManager;
import com.redhat.ceylon.compiler.java.runtime.metamodel.Metamodel;
import com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor;
import com.redhat.ceylon.compiler.loader.impl.reflect.mirror.ReflectionClass;
import com.redhat.ceylon.compiler.loader.model.LazyClass;
import com.redhat.ceylon.compiler.loader.model.LazyInterface;
import com.redhat.ceylon.compiler.typechecker.context.Context;
import com.redhat.ceylon.compiler.typechecker.io.VFS;
import com.redhat.ceylon.compiler.typechecker.model.ProducedType;
import com.redhat.ceylon.compiler.typechecker.model.TypeDeclaration;

/**
* Helper class for generated Ceylon code that needs to call implementation logic.
Expand All @@ -42,122 +28,16 @@
*/
public class Util {

private static RuntimeModuleManager moduleManager;

static{
resetModuleManager();
}

public static void loadModule(String name, String version, ArtifactResult result, ClassLoader classLoader){
moduleManager.loadModule(name, version, result, classLoader);
}

public static void resetModuleManager() {
RepositoryManagerBuilder builder = new RepositoryManagerBuilder(new Logger(){

@Override
public void error(String str) {
System.err.println("ERROR: "+str);
}

@Override
public void warning(String str) {
System.err.println("WARN: "+str);
}

@Override
public void info(String str) {
System.err.println("INFO: "+str);
}

@Override
public void debug(String str) {
System.err.println("DEBUG: "+str);
}

});
RepositoryManager repoManager = builder.buildRepository();
VFS vfs = new VFS();
Context context = new Context(repoManager, vfs);
moduleManager = new RuntimeModuleManager(context);
moduleManager.initCoreModules();
moduleManager.prepareForTypeChecking();
}

public static String declClassName(String name) {
return name.replace("::", ".");
}

public static TypeDescriptor getTypeDescriptor(Object instance) {
if(instance == null)
return Null.$TypeDescriptor;
else if(instance instanceof ReifiedType)
return((ReifiedType) instance).$getType();
else
return null; // FIXME: interop?
}

public static boolean isReified(java.lang.Object o, TypeDescriptor type){
TypeDescriptor instanceType = getTypeDescriptor(o);
if(instanceType == null)
return false; // FIXME: interop?
return instanceType.toProducedType(moduleManager).isSubtypeOf(type.toProducedType(moduleManager));
}

public static ProducedType getProducedType(Object instance) {
TypeDescriptor instanceType = getTypeDescriptor(instance);
if(instanceType == null)
throw new RuntimeException("Metamodel not yet supported for Java types");
return instanceType.toProducedType(moduleManager);
}

public static ceylon.language.metamodel.ProducedType getMetamodel(TypeDescriptor typeDescriptor) {
if(typeDescriptor == null)
throw new RuntimeException("Metamodel not yet supported for Java types");
ProducedType pt = typeDescriptor.toProducedType(moduleManager);
return getMetamodel(pt);
}

// FIXME: this will need better thinking in terms of memory usage
private static Map<com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface, com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface> typeCheckModelToRuntimeModel
= new HashMap<com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface, com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface>();

public static com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface getOrCreateMetamodel(com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface declaration){
synchronized(typeCheckModelToRuntimeModel){
com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface ret = typeCheckModelToRuntimeModel.get(declaration);
if(ret == null){
if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Class){
ret = new com.redhat.ceylon.compiler.java.runtime.metamodel.Class((com.redhat.ceylon.compiler.typechecker.model.Class)declaration);
}else if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Interface){
ret = new com.redhat.ceylon.compiler.java.runtime.metamodel.Interface((com.redhat.ceylon.compiler.typechecker.model.Interface)declaration);
}
typeCheckModelToRuntimeModel.put(declaration, ret);
}
return ret;
}
public static void loadModule(String name, String version, ArtifactResult result, ClassLoader classLoader){
Metamodel.loadModule(name, version, result, classLoader);
}

public static ceylon.language.metamodel.ProducedType getMetamodel(ProducedType pt) {
TypeDeclaration declaration = pt.getDeclaration();
if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Class){
return new com.redhat.ceylon.compiler.java.runtime.metamodel.ClassType(pt);
}
if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Interface){
return new com.redhat.ceylon.compiler.java.runtime.metamodel.InterfaceType(pt);
}
throw new RuntimeException("Declaration type not supported yet: "+declaration);
}

public static TypeDescriptor getTypeDescriptorForDeclaration(com.redhat.ceylon.compiler.typechecker.model.Declaration declaration) {
if(declaration instanceof LazyClass){
ReflectionClass classMirror = (ReflectionClass) ((LazyClass) declaration).classMirror;
return TypeDescriptor.klass(classMirror.klass);
}
if(declaration instanceof LazyInterface){
ReflectionClass classMirror = (ReflectionClass) ((LazyInterface) declaration).classMirror;
return TypeDescriptor.klass(classMirror.klass);
}
throw new RuntimeException("Unsupported declaration type: " + declaration);
public static boolean isReified(java.lang.Object o, TypeDescriptor type){
return Metamodel.isReified(o, type);
}

/**
Expand Down
Expand Up @@ -47,18 +47,18 @@ public ClassOrInterface(com.redhat.ceylon.compiler.typechecker.model.ClassOrInte
}

protected void init(){
this.$reifiedType = Util.getTypeDescriptorForDeclaration(declaration);
this.$reifiedType = Metamodel.getTypeDescriptorForDeclaration(declaration);
com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface declaration = (com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface) this.declaration;

ProducedType superType = declaration.getExtendedType();
if(superType != null)
this.superclass = (ceylon.language.metamodel.ClassType) Util.getMetamodel(superType);
this.superclass = (ceylon.language.metamodel.ClassType) Metamodel.getMetamodel(superType);

List<ProducedType> satisfiedTypes = declaration.getSatisfiedTypes();
ceylon.language.metamodel.InterfaceType[] interfaces = new ceylon.language.metamodel.InterfaceType[satisfiedTypes.size()];
int i=0;
for(ProducedType pt : satisfiedTypes){
interfaces[i++] = (ceylon.language.metamodel.InterfaceType) Util.getMetamodel(pt);
interfaces[i++] = (ceylon.language.metamodel.InterfaceType) Metamodel.getMetamodel(pt);
}
this.interfaces = (Sequential)Util.sequentialInstance($InterfacesTypeDescriptor, interfaces);

Expand Down
Expand Up @@ -65,7 +65,7 @@ protected void checkInit(){

protected void init() {
com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface decl = (com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface) producedType.getDeclaration();
this.declaration = Util.getOrCreateMetamodel(decl);
this.declaration = Metamodel.getOrCreateMetamodel(decl);
java.util.Map<ceylon.language.metamodel.TypeParameter, ceylon.language.metamodel.ProducedType> typeArguments
= new LinkedHashMap<ceylon.language.metamodel.TypeParameter, ceylon.language.metamodel.ProducedType>();
Iterator<? extends ceylon.language.metamodel.TypeParameter> typeParameters = declaration.getTypeParameters().iterator();
Expand All @@ -76,7 +76,7 @@ protected void init() {
com.redhat.ceylon.compiler.java.runtime.metamodel.TypeParameter tp = (com.redhat.ceylon.compiler.java.runtime.metamodel.TypeParameter) it;
com.redhat.ceylon.compiler.typechecker.model.TypeParameter tpDecl = (com.redhat.ceylon.compiler.typechecker.model.TypeParameter) tp.declaration;
com.redhat.ceylon.compiler.typechecker.model.ProducedType ptArg = ptArguments.get(tpDecl);
ProducedType ptArgWrapped = Util.getMetamodel(ptArg);
ProducedType ptArgWrapped = Metamodel.getMetamodel(ptArg);
typeArguments.put(tp, ptArgWrapped);
}
this.typeArguments = new InternalMap<ceylon.language.metamodel.TypeParameter,
Expand Down
@@ -0,0 +1,138 @@
package com.redhat.ceylon.compiler.java.runtime.metamodel;

import java.util.HashMap;
import java.util.Map;

import ceylon.language.Null;

import com.redhat.ceylon.cmr.api.ArtifactResult;
import com.redhat.ceylon.cmr.api.Logger;
import com.redhat.ceylon.cmr.api.RepositoryManager;
import com.redhat.ceylon.cmr.api.RepositoryManagerBuilder;
import com.redhat.ceylon.compiler.java.runtime.model.ReifiedType;
import com.redhat.ceylon.compiler.java.runtime.model.RuntimeModuleManager;
import com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor;
import com.redhat.ceylon.compiler.loader.impl.reflect.mirror.ReflectionClass;
import com.redhat.ceylon.compiler.loader.model.LazyClass;
import com.redhat.ceylon.compiler.loader.model.LazyInterface;
import com.redhat.ceylon.compiler.typechecker.context.Context;
import com.redhat.ceylon.compiler.typechecker.io.VFS;
import com.redhat.ceylon.compiler.typechecker.model.ProducedType;
import com.redhat.ceylon.compiler.typechecker.model.TypeDeclaration;

public class Metamodel {

private static RuntimeModuleManager moduleManager;

static{
resetModuleManager();
}

// FIXME: this will need better thinking in terms of memory usage
private static Map<com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface, com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface> typeCheckModelToRuntimeModel
= new HashMap<com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface, com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface>();

public static void loadModule(String name, String version, ArtifactResult result, ClassLoader classLoader){
moduleManager.loadModule(name, version, result, classLoader);
}

public static void resetModuleManager() {
RepositoryManagerBuilder builder = new RepositoryManagerBuilder(new Logger(){

@Override
public void error(String str) {
System.err.println("ERROR: "+str);
}

@Override
public void warning(String str) {
System.err.println("WARN: "+str);
}

@Override
public void info(String str) {
System.err.println("INFO: "+str);
}

@Override
public void debug(String str) {
System.err.println("DEBUG: "+str);
}

});
RepositoryManager repoManager = builder.buildRepository();
VFS vfs = new VFS();
Context context = new Context(repoManager, vfs);
moduleManager = new RuntimeModuleManager(context);
moduleManager.initCoreModules();
moduleManager.prepareForTypeChecking();
}

public static TypeDescriptor getTypeDescriptor(Object instance) {
if(instance == null)
return Null.$TypeDescriptor;
else if(instance instanceof ReifiedType)
return((ReifiedType) instance).$getType();
else
return null; // FIXME: interop?
}
public static boolean isReified(java.lang.Object o, TypeDescriptor type){
TypeDescriptor instanceType = getTypeDescriptor(o);
if(instanceType == null)
return false; // FIXME: interop?
return instanceType.toProducedType(moduleManager).isSubtypeOf(type.toProducedType(moduleManager));
}

public static ProducedType getProducedType(Object instance) {
TypeDescriptor instanceType = getTypeDescriptor(instance);
if(instanceType == null)
throw new RuntimeException("Metamodel not yet supported for Java types");
return instanceType.toProducedType(moduleManager);
}

public static ceylon.language.metamodel.ProducedType getMetamodel(TypeDescriptor typeDescriptor) {
if(typeDescriptor == null)
throw new RuntimeException("Metamodel not yet supported for Java types");
ProducedType pt = typeDescriptor.toProducedType(moduleManager);
return getMetamodel(pt);
}

public static com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface getOrCreateMetamodel(com.redhat.ceylon.compiler.typechecker.model.ClassOrInterface declaration){
synchronized(typeCheckModelToRuntimeModel){
com.redhat.ceylon.compiler.java.runtime.metamodel.ClassOrInterface ret = typeCheckModelToRuntimeModel.get(declaration);
if(ret == null){
if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Class){
ret = new com.redhat.ceylon.compiler.java.runtime.metamodel.Class((com.redhat.ceylon.compiler.typechecker.model.Class)declaration);
}else if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Interface){
ret = new com.redhat.ceylon.compiler.java.runtime.metamodel.Interface((com.redhat.ceylon.compiler.typechecker.model.Interface)declaration);
}
typeCheckModelToRuntimeModel.put(declaration, ret);
}
return ret;
}
}

public static ceylon.language.metamodel.ProducedType getMetamodel(ProducedType pt) {
TypeDeclaration declaration = pt.getDeclaration();
if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Class){
return new com.redhat.ceylon.compiler.java.runtime.metamodel.ClassType(pt);
}
if(declaration instanceof com.redhat.ceylon.compiler.typechecker.model.Interface){
return new com.redhat.ceylon.compiler.java.runtime.metamodel.InterfaceType(pt);
}
throw new RuntimeException("Declaration type not supported yet: "+declaration);
}

public static TypeDescriptor getTypeDescriptorForDeclaration(com.redhat.ceylon.compiler.typechecker.model.Declaration declaration) {
if(declaration instanceof LazyClass){
ReflectionClass classMirror = (ReflectionClass) ((LazyClass) declaration).classMirror;
return TypeDescriptor.klass(classMirror.klass);
}
if(declaration instanceof LazyInterface){
ReflectionClass classMirror = (ReflectionClass) ((LazyInterface) declaration).classMirror;
return TypeDescriptor.klass(classMirror.klass);
}
throw new RuntimeException("Unsupported declaration type: " + declaration);
}

}

0 comments on commit f4677af

Please sign in to comment.