/*
Copyright 2017 Rice University

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package edu.rice.cs.caper.bayou.core.synthesizer;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Type;

public class Variable {

    final String name;
    final Type type;
    final Class clazz;
    int refCount;

    Variable(String name, Type type) {
        this.name = name;
        this.type = type;
        refCount = 0;

        ITypeBinding binding = type.resolveBinding();
        if (type.isPrimitiveType())
            this.clazz = Visitor.primitiveToClass.get(((PrimitiveType) type).getPrimitiveTypeCode());
        else if (binding != null) {
            try {
                this.clazz = Environment.getClass(binding.getQualifiedName());
            } catch (ClassNotFoundException e) {
                throw new SynthesisException(SynthesisException.ClassNotFoundInLoader);
            }
        }
        else if (type.isSimpleType()) {
            try {
                String t = (((SimpleType) type).getName()).getFullyQualifiedName();
                this.clazz = Environment.getClass(t);
            } catch (ClassNotFoundException e) {
                throw new SynthesisException(SynthesisException.ClassNotFoundInLoader);
            }
        }
        else // TODO: support generic types (isParameterizedType())
            throw new SynthesisException(SynthesisException.InvalidKindOfType);
    }

    public String getName() {
        return name;
    }

    public Type getType() {
        return type;
    }

    public Class getTypeAsClass() {
        return clazz;
    }

    public void addRefCount() {
        refCount += 1;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || ! (o instanceof Variable))
            return false;
        Variable v = (Variable) o;
        return v.name.equals(name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        return name;
    }
}

@@ -642,149 +642,9 @@
 dom.
-ITypeBinding;%0Aimport org.eclipse.jdt.core.dom.PrimitiveType;%0Aimport org.eclipse.jdt.core.dom.SimpleType;%0Aimport org.eclipse.jdt.core.dom.Type
+*
 ;%0A%0Ap
@@ -1075,16 +1075,55 @@
 else if 
+(type.isSimpleType()) %7B%0A            if 
 (binding
@@ -1131,19 +1131,19 @@
 != null)
- %7B%0A
+%0A  
         
@@ -1138,38 +1138,44 @@
 )%0A              
+  
 try %7B%0A
+    
                 
@@ -1241,32 +1241,36 @@
 ));%0A            
+    
 %7D catch (ClassNo
@@ -1282,32 +1282,36 @@
 dException e) %7B%0A
+    
                 
@@ -1398,78 +1398,900 @@
     
-%7D%0A        %7D%0A        else if (type.isSimpleType()) %7B%0A            try %7B%0A
+    %7D%0A            else%0A                try %7B%0A                    String t = (((SimpleType) type).getName()).getFullyQualifiedName();%0A                    this.clazz = Environment.getClass(t);%0A                %7D catch (ClassNotFoundException e) %7B%0A                    throw new SynthesisException(SynthesisException.ClassNotFoundInLoader);%0A                %7D%0A        %7D%0A        else if (type.isParameterizedType()) %7B%0A            if (binding != null)%0A                try %7B%0A                    ITypeBinding erased = binding.getErasure();%0A                    this.clazz = Environment.getClass(erased.getQualifiedName());%0A                %7D catch (ClassNotFoundException e) %7B%0A                    throw new SynthesisException(SynthesisException.ClassNotFoundInLoader);%0A                %7D%0A            else%0A                try %7B%0A                    Type baseType = ((ParameterizedType) type).getType();%0A    
     
@@ -2320,33 +2320,37 @@
  (((SimpleType) 
-t
+baseT
 ype).getName()).
@@ -2382,32 +2382,36 @@
                 
+  
+  
 this.clazz = Env
@@ -2436,32 +2436,36 @@
 t);%0A            
+    
 %7D catch (ClassNo
@@ -2493,32 +2493,36 @@
                 
+ 
+   
 throw new Synthe
@@ -2569,32 +2569,36 @@
 FoundInLoader);%0A
+    
             %7D%0A  

Make synthesizer accept generic variables in scope