/*
 * Copyright 2000-2007 JetBrains s.r.o.
 * 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 org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.types;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrInstanceOfExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBuiltinTypeClassExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.GrExpressionImpl;

/**
 * @author ven
 */
public class GrBuiltinTypeClassExpressionImpl extends GrExpressionImpl implements GrBuiltinTypeClassExpression {

  public GrBuiltinTypeClassExpressionImpl(@NotNull ASTNode node) {
    super(node);
  }

  public void accept(GroovyElementVisitor visitor) {
    visitor.visitBuiltinTypeClassExpression(this);
  }

  public String toString() {
    return "builtin type class expression";
  }

  public PsiType getType() {
    return getManager().getElementFactory().createTypeByFQClassName("java.lang.Class", getResolveScope());
  }
}
@@ -732,310 +732,301 @@
 lij.
-psi.PsiType;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;%0Aimport org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;%0Aimport org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrInstanceOfExpression
+openapi.diagnostic.Logger;%0Aimport com.intellij.psi.*;%0Aimport com.intellij.psi.impl.PsiManagerEx;%0Aimport com.intellij.util.Function;%0Aimport com.intellij.util.IncorrectOperationException;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor
 ;%0Aim
@@ -1174,31 +1174,29 @@
 psi.
-api.types.GrTypeElement
+impl.GroovyPsiManager
 ;%0Aim
@@ -1420,16 +1420,300 @@
 ession %7B
+%0A  private static final Logger LOG = Logger.getInstance(%22org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.types.GrBuiltinTypeClassExpressionImpl%22);%0A%0A  private static final Function%3CGrBuiltinTypeClassExpressionImpl, PsiType%3E TYPES_CALCULATOR = new MyTypesCalculator();
 %0A%0A  publ
@@ -2026,64 +2026,359 @@
 urn 
-getManager().getElementFactory().createTypeByFQClassName
+GroovyPsiManager.getInstance(getProject()).getType(this, TYPES_CALCULATOR);%0A  %7D%0A%0A  private static class MyTypesCalculator implements Function%3CGrBuiltinTypeClassExpressionImpl, PsiType%3E %7B%0A    public PsiType fun(GrBuiltinTypeClassExpressionImpl expression) %7B%0A      PsiManagerEx manager = expression.getManager();%0A      PsiClass clazz = manager.findClass
 (%22ja
@@ -2393,16 +2393,27 @@
 Class%22, 
+expression.
 getResol
@@ -2424,13 +2424,632 @@
 ope());%0A
+      if (clazz != null) %7B%0A        PsiElementFactory factory = manager.getElementFactory();%0A        PsiTypeParameter%5B%5D typeParameters = clazz.getTypeParameters();%0A        PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;%0A        if (typeParameters.length == 1) %7B%0A          try %7B%0A            PsiType type = factory.createTypeFromText(expression.getText(), null);%0A            substitutor = substitutor.put(typeParameters%5B0%5D, type);%0A          %7D catch (IncorrectOperationException e) %7B%0A            LOG.error(e);%0A          %7D%0A        %7D%0A        return factory.createType(clazz, substitutor);%0A      %7D%0A      return null;%0A    %7D%0A
   %7D%0A%7D

put correct generic instantiation on Class' type parameter