Search in sources :

Example 1 with AstLoader

use of kalang.compiler.AstLoader in project kalang by kasonyang.

the class ClassTypeTest method test.

@Test
public void test() throws AstNotFoundException {
    AstLoader astLoader = new AstLoader();
    ClassNode listClass = astLoader.loadAst("java.util.List");
    ClassNode arrayListClass = astLoader.loadAst("java.util.ArrayList");
    Type[] paramTypes = new Type[] { Types.getRootType() };
    ObjectType listType = Types.getClassType(listClass, paramTypes);
    ArrayType listArrayType = Types.getArrayType(listType);
    ObjectType arrayListType = Types.getClassType(arrayListClass, paramTypes);
    ArrayType arrayListArrayType = Types.getArrayType(arrayListType);
    assertTrue(arrayListType.isSubTypeOf(listType));
    assertTrue(listArrayType.isAssignableFrom(arrayListArrayType));
    String mdDescriptors = Arrays.toString(listType.getMethodDescriptors(listClass, false, true));
    assertEquals("[public abstract boolean contains(java.lang.Object arg0), public abstract boolean addAll(int arg0,java.util.Collection<? extends java.lang.Object> arg1), public void sort(java.util.Comparator<? super java.lang.Object> arg0), public abstract java.util.ListIterator<java.lang.Object> listIterator(int arg0), public abstract java.util.ListIterator<java.lang.Object> listIterator(), public abstract boolean retainAll(java.util.Collection<? extends java.lang.Object> arg0), public abstract java.lang.Object[] toArray(), public abstract java.lang.Object remove(int arg0), public abstract boolean addAll(java.util.Collection<? extends java.lang.Object> arg0), public boolean removeIf(java.util.function.Predicate<? super java.lang.Object> arg0), public abstract int hashCode(), public abstract boolean removeAll(java.util.Collection<? extends java.lang.Object> arg0), public java.util.stream.Stream<java.lang.Object> parallelStream(), public abstract int lastIndexOf(java.lang.Object arg0), public void replaceAll(java.util.function.UnaryOperator<java.lang.Object> arg0), public abstract void clear(), public abstract boolean containsAll(java.util.Collection<? extends java.lang.Object> arg0), public abstract int indexOf(java.lang.Object arg0), public abstract boolean add(java.lang.Object arg0), public abstract boolean isEmpty(), public abstract java.util.List<java.lang.Object> subList(int arg0,int arg1), public abstract java.lang.Object get(int arg0), public abstract java.lang.Object set(int arg0,java.lang.Object arg1), public abstract void add(int arg0,java.lang.Object arg1), public abstract java.util.Iterator<java.lang.Object> iterator(), public java.util.Spliterator<java.lang.Object> spliterator(), public void forEach(java.util.function.Consumer<? super java.lang.Object> arg0), public abstract java.lang.Object[] toArray(java.lang.Object[] arg0), public abstract boolean remove(java.lang.Object arg0), public abstract boolean equals(java.lang.Object arg0), public java.util.stream.Stream<java.lang.Object> stream(), public abstract int size()]", mdDescriptors);
// assertTrue(listType.isSubTypeOf(Types.getRootType()));
}
Also used : ArrayType(kalang.core.ArrayType) AstLoader(kalang.compiler.AstLoader) ClassNode(kalang.ast.ClassNode) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) Type(kalang.core.Type) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) Test(org.junit.Test)

Example 2 with AstLoader

use of kalang.compiler.AstLoader in project kalang by kasonyang.

the class ClassTypeTest method testEarsedType.

@Test
public void testEarsedType() throws AstNotFoundException {
    AstLoader astLoader = new AstLoader();
    ClassType listType = Types.getClassType(astLoader.loadAst("java.util.List"), new Type[0]);
    ClassType arrayListType = Types.getClassType(astLoader.loadAst("java.util.ArrayList"), new Type[0]);
    assertTrue(arrayListType.isSubTypeOf(listType));
}
Also used : AstLoader(kalang.compiler.AstLoader) ClassType(kalang.core.ClassType) Test(org.junit.Test)

Example 3 with AstLoader

use of kalang.compiler.AstLoader in project kalang by kasonyang.

the class FileSystemCompiler method compile.

public void compile() throws IOException {
    if (outputDir == null) {
        throw new IllegalStateException("output diretory is null");
    }
    URLClassLoader pathClassLoader = new URLClassLoader(classPaths.toArray(new URL[classPaths.size()]), this.classLoader);
    AstLoader astLoader = new JavaAstLoader(this.parentAstLoader, pathClassLoader);
    KalangCompiler compiler = new KalangCompiler(astLoader) {

        @Override
        public CodeGenerator createCodeGenerator(CompilationUnit compilationUnit) {
            FileSystemOutputManager om = new FileSystemOutputManager(outputDir, extension);
            return new ClassWriter(om);
        }
    };
    compiler.setConfiguration(configuration);
    for (Map.Entry<String, File> e : sourceFiles.entrySet()) {
        String className = e.getKey();
        File file = e.getValue();
        compiler.addSource(className, FileUtils.readFileToString(file), file.getName());
    }
    FileSystemSourceLoader sourceLoader = new FileSystemSourceLoader(sourcePaths.toArray(new File[sourcePaths.size()]), new String[] { "kl", "kalang" });
    compiler.setSourceLoader(sourceLoader);
    if (diagnosisHandler != null) {
        compiler.setDiagnosisHandler(diagnosisHandler);
    }
    compiler.compile();
}
Also used : CompilationUnit(kalang.compiler.CompilationUnit) AstLoader(kalang.compiler.AstLoader) JavaAstLoader(kalang.compiler.JavaAstLoader) KalangCompiler(kalang.compiler.KalangCompiler) JavaAstLoader(kalang.compiler.JavaAstLoader)

Aggregations

AstLoader (kalang.compiler.AstLoader)3 ClassType (kalang.core.ClassType)2 Test (org.junit.Test)2 ClassNode (kalang.ast.ClassNode)1 CompilationUnit (kalang.compiler.CompilationUnit)1 JavaAstLoader (kalang.compiler.JavaAstLoader)1 KalangCompiler (kalang.compiler.KalangCompiler)1 ArrayType (kalang.core.ArrayType)1 ObjectType (kalang.core.ObjectType)1 Type (kalang.core.Type)1