package com.redhat.ceylon.eclipse.core.model;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;

import com.redhat.ceylon.compiler.typechecker.model.Declaration;
import com.redhat.ceylon.compiler.typechecker.model.Package;

public class JavaCompilationUnit extends JavaUnit {
    ICompilationUnit typeRoot;

    public JavaCompilationUnit(ICompilationUnit typeRoot, String fileName, String relativePath, String fullPath, Package pkg) {
        super(fileName, relativePath, fullPath, pkg);
        this.typeRoot = typeRoot;
    }

    @Override
    public ICompilationUnit getTypeRoot() {
        return typeRoot;
    }

    @Override
    public IJavaElement toJavaElement(final Declaration ceylonDeclaration) {
/*        
        try {
            ASTParser parser = ASTParser.newParser(AST.JLS4);
            parser.setBindingsRecovery(true);
            parser.setResolveBindings(true);
            parser.setSource(typeRoot);
            class MatchingASTVisitor extends ASTVisitor {
                IBinding declarationBinding = null;
                private boolean declarationMatched(IBinding currentBinding,
                        IBindingProvider mirror) {
                    if (mirror != null && CharOperation.equals(currentBinding.getKey().toCharArray(), mirror.getBindingKey())) {
                        declarationBinding = currentBinding;
                        return true;
                    }
                    return false;
                }
                @Override
                public boolean visit(MethodDeclaration node) {
                    IBindingProvider mirror = null;
                    if (node.isConstructor() && ceylonDeclaration instanceof LazyClass) {
                        LazyClass lazyClass = (LazyClass) ceylonDeclaration;
                        if (! lazyClass.isAbstraction()) {
                            IBindingProvider constructor = (IBindingProvider) lazyClass.getConstructor();
                            if (constructor != null) {
                                mirror = constructor;
                            }
                        }
                    }
                    if (ceylonDeclaration instanceof LazyMethod) {
                        mirror = (IBindingProvider) ((LazyMethod) ceylonDeclaration).getMethodMirror();
                    }
                    if (ceylonDeclaration instanceof JavaMethod) {
                        mirror = (IBindingProvider) ((JavaMethod) ceylonDeclaration).mirror;
                    }
                    if (declarationMatched(node.resolveBinding(), mirror)) {
                        return false;
                    }

                    return super.visit(node);
                }
                @Override
                public boolean visit(org.eclipse.jdt.core.dom.TypeDeclaration node) {
                    IBindingProvider mirror = null;
                    if (ceylonDeclaration instanceof LazyClass) {
                        LazyClass lazyClass = (LazyClass) ceylonDeclaration;
                        if (! lazyClass.isAbstraction()) {
                            IBindingProvider constructor = (IBindingProvider) lazyClass.getConstructor();
                            if (constructor != null) {
                                return super.visit(node); //this will be managed in the IType element  
                            }
                        }
                        mirror = (IBindingProvider) lazyClass.classMirror;
                    }
                    if (ceylonDeclaration instanceof LazyInterface) {
                        mirror = (IBindingProvider) ((LazyInterface) ceylonDeclaration).classMirror; 
                    }                    
                    if (ceylonDeclaration instanceof LazyValue) {
                        mirror = (IBindingProvider) ((LazyValue) ceylonDeclaration).classMirror; 
                    }
                    if (ceylonDeclaration instanceof JavaBeanValue) {
                        JavaBeanValue javaBeanValue = ((JavaBeanValue) ceylonDeclaration);
                        Scope container = javaBeanValue.getContainer();
                        if (container instanceof LazyClass) {
                            mirror = (IBindingProvider) ((LazyClass) container).classMirror;
                        }
                        if (container instanceof LazyInterface) {
                            mirror = (IBindingProvider) ((LazyInterface) container).classMirror;
                        }
                        if (ceylonDeclaration instanceof LazyValue) {
                            mirror = (IBindingProvider) ((LazyValue) container).classMirror; 
                        }
                        if (declarationMatched(node.resolveBinding(), mirror)) {
                            for (MethodDeclaration methodDecl : node.getMethods()) {
                                if (methodDecl.getName().toString().equals(javaBeanValue.getGetterName())) {
                                    declarationBinding = methodDecl.resolveBinding();
                                    return false;
                                }
                            }
                        }
                        return super.visit(node);
                    }
                    if (declarationMatched(node.resolveBinding(), mirror)) {
                        return false;
                    }
                    return super.visit(node);
                }
                @Override
                public boolean visit(org.eclipse.jdt.core.dom.EnumDeclaration node) {
                    IBindingProvider mirror = null;
                    if (ceylonDeclaration instanceof LazyClass) {
                        LazyClass lazyClass = (LazyClass) ceylonDeclaration;
                        if (! lazyClass.isAbstraction()) {
                            IBindingProvider constructor = (IBindingProvider) lazyClass.getConstructor();
                            if (constructor != null) {
                                return super.visit(node); //this will be managed in the IType element  
                            }
                        }
                        mirror = (IBindingProvider) lazyClass.classMirror;
                    }
                    if (declarationMatched(node.resolveBinding(), mirror)) {
                        return false;
                    }
                    return super.visit(node);
                }
                @Override
                public boolean visit(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration node) {
                    IBindingProvider mirror = null;
                    if (ceylonDeclaration instanceof LazyClass) {
                        LazyClass lazyClass = (LazyClass) ceylonDeclaration;
                        if (! lazyClass.isAbstraction()) {
                            IBindingProvider constructor = (IBindingProvider) lazyClass.getConstructor();
                            if (constructor != null) {
                                return super.visit(node); //this will be managed in the IType element  
                            }
                        }
                        mirror = (IBindingProvider) lazyClass.classMirror;
                    }
                    if (ceylonDeclaration instanceof LazyInterface) {
                        mirror = (IBindingProvider) ((LazyInterface) ceylonDeclaration).classMirror; 
                    }
                    
                    if (declarationMatched(node.resolveBinding(), mirror)) {
                        return false;
                    }
                    return super.visit(node);
                }
            }
            MatchingASTVisitor matchingVisitor = new MatchingASTVisitor();            
            parser.createAST(null).accept(matchingVisitor);
            if (matchingVisitor.declarationBinding != null) {
                return matchingVisitor.declarationBinding.getJavaElement();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
*/
        return new CeylonToJavaMatcher(this).searchInClass(ceylonDeclaration);
    }
}

@@ -748,7359 +748,8 @@
 ) %7B%0A
-/*        %0A        try %7B%0A            ASTParser parser = ASTParser.newParser(AST.JLS4);%0A            parser.setBindingsRecovery(true);%0A            parser.setResolveBindings(true);%0A            parser.setSource(typeRoot);%0A            class MatchingASTVisitor extends ASTVisitor %7B%0A                IBinding declarationBinding = null;%0A                private boolean declarationMatched(IBinding currentBinding,%0A                        IBindingProvider mirror) %7B%0A                    if (mirror != null && CharOperation.equals(currentBinding.getKey().toCharArray(), mirror.getBindingKey())) %7B%0A                        declarationBinding = currentBinding;%0A                        return true;%0A                    %7D%0A                    return false;%0A                %7D%0A                @Override%0A                public boolean visit(MethodDeclaration node) %7B%0A                    IBindingProvider mirror = null;%0A                    if (node.isConstructor() && ceylonDeclaration instanceof LazyClass) %7B%0A                        LazyClass lazyClass = (LazyClass) ceylonDeclaration;%0A                        if (! lazyClass.isAbstraction()) %7B%0A                            IBindingProvider constructor = (IBindingProvider) lazyClass.getConstructor();%0A                            if (constructor != null) %7B%0A                                mirror = constructor;%0A                            %7D%0A                        %7D%0A                    %7D%0A                    if (ceylonDeclaration instanceof LazyMethod) %7B%0A                        mirror = (IBindingProvider) ((LazyMethod) ceylonDeclaration).getMethodMirror();%0A                    %7D%0A                    if (ceylonDeclaration instanceof JavaMethod) %7B%0A                        mirror = (IBindingProvider) ((JavaMethod) ceylonDeclaration).mirror;%0A                    %7D%0A                    if (declarationMatched(node.resolveBinding(), mirror)) %7B%0A                        return false;%0A                    %7D%0A%0A                    return super.visit(node);%0A                %7D%0A                @Override%0A                public boolean visit(org.eclipse.jdt.core.dom.TypeDeclaration node) %7B%0A                    IBindingProvider mirror = null;%0A                    if (ceylonDeclaration instanceof LazyClass) %7B%0A                        LazyClass lazyClass = (LazyClass) ceylonDeclaration;%0A                        if (! lazyClass.isAbstraction()) %7B%0A                            IBindingProvider constructor = (IBindingProvider) lazyClass.getConstructor();%0A                            if (constructor != null) %7B%0A                                return super.visit(node); //this will be managed in the IType element  %0A                            %7D%0A                        %7D%0A                        mirror = (IBindingProvider) lazyClass.classMirror;%0A                    %7D%0A                    if (ceylonDeclaration instanceof LazyInterface) %7B%0A                        mirror = (IBindingProvider) ((LazyInterface) ceylonDeclaration).classMirror; %0A                    %7D                    %0A                    if (ceylonDeclaration instanceof LazyValue) %7B%0A                        mirror = (IBindingProvider) ((LazyValue) ceylonDeclaration).classMirror; %0A                    %7D%0A                    if (ceylonDeclaration instanceof JavaBeanValue) %7B%0A                        JavaBeanValue javaBeanValue = ((JavaBeanValue) ceylonDeclaration);%0A                        Scope container = javaBeanValue.getContainer();%0A                        if (container instanceof LazyClass) %7B%0A                            mirror = (IBindingProvider) ((LazyClass) container).classMirror;%0A                        %7D%0A                        if (container instanceof LazyInterface) %7B%0A                            mirror = (IBindingProvider) ((LazyInterface) container).classMirror;%0A                        %7D%0A                        if (ceylonDeclaration instanceof LazyValue) %7B%0A                            mirror = (IBindingProvider) ((LazyValue) container).classMirror; %0A                        %7D%0A                        if (declarationMatched(node.resolveBinding(), mirror)) %7B%0A                            for (MethodDeclaration methodDecl : node.getMethods()) %7B%0A                                if (methodDecl.getName().toString().equals(javaBeanValue.getGetterName())) %7B%0A                                    declarationBinding = methodDecl.resolveBinding();%0A                                    return false;%0A                                %7D%0A                            %7D%0A                        %7D%0A                        return super.visit(node);%0A                    %7D%0A                    if (declarationMatched(node.resolveBinding(), mirror)) %7B%0A                        return false;%0A                    %7D%0A                    return super.visit(node);%0A                %7D%0A                @Override%0A                public boolean visit(org.eclipse.jdt.core.dom.EnumDeclaration node) %7B%0A                    IBindingProvider mirror = null;%0A                    if (ceylonDeclaration instanceof LazyClass) %7B%0A                        LazyClass lazyClass = (LazyClass) ceylonDeclaration;%0A                        if (! lazyClass.isAbstraction()) %7B%0A                            IBindingProvider constructor = (IBindingProvider) lazyClass.getConstructor();%0A                            if (constructor != null) %7B%0A                                return super.visit(node); //this will be managed in the IType element  %0A                            %7D%0A                        %7D%0A                        mirror = (IBindingProvider) lazyClass.classMirror;%0A                    %7D%0A                    if (declarationMatched(node.resolveBinding(), mirror)) %7B%0A                        return false;%0A                    %7D%0A                    return super.visit(node);%0A                %7D%0A                @Override%0A                public boolean visit(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration node) %7B%0A                    IBindingProvider mirror = null;%0A                    if (ceylonDeclaration instanceof LazyClass) %7B%0A                        LazyClass lazyClass = (LazyClass) ceylonDeclaration;%0A                        if (! lazyClass.isAbstraction()) %7B%0A                            IBindingProvider constructor = (IBindingProvider) lazyClass.getConstructor();%0A                            if (constructor != null) %7B%0A                                return super.visit(node); //this will be managed in the IType element  %0A                            %7D%0A                        %7D%0A                        mirror = (IBindingProvider) lazyClass.classMirror;%0A                    %7D%0A                    if (ceylonDeclaration instanceof LazyInterface) %7B%0A                        mirror = (IBindingProvider) ((LazyInterface) ceylonDeclaration).classMirror; %0A                    %7D%0A                    %0A                    if (declarationMatched(node.resolveBinding(), mirror)) %7B%0A                        return false;%0A                    %7D%0A                    return super.visit(node);%0A                %7D%0A            %7D%0A            MatchingASTVisitor matchingVisitor = new MatchingASTVisitor();            %0A            parser.createAST(null).accept(matchingVisitor);%0A            if (matchingVisitor.declarationBinding != null) %7B%0A                return matchingVisitor.declarationBinding.getJavaElement();%0A            %7D%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return null;%0A*/%0A
     

clean unnecessary comment