blob: 3581b27ba9069b707e4099811da07e0b3cdcce18 [file] [log] [blame]
/*
* Copyright 2000-2013 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 com.jetbrains.python.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import com.jetbrains.python.PythonFileType;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Constructor;
public class PyElementType extends IElementType {
protected Class<? extends PsiElement> myPsiElementClass;
private static final Class[] PARAMETER_TYPES = new Class[]{ASTNode.class};
private Constructor<? extends PsiElement> myConstructor;
private String mySpecialMethodName;
public PyElementType(@NotNull @NonNls String debugName) {
super(debugName, PythonFileType.INSTANCE.getLanguage());
}
public PyElementType(@NotNull @NonNls String debugName, @NotNull Class<? extends PsiElement> psiElementClass) {
this(debugName);
myPsiElementClass = psiElementClass;
}
public PyElementType(@NotNull @NonNls String debugName, @NotNull @NonNls String specialMethodName) {
this(debugName);
mySpecialMethodName = specialMethodName;
}
@NotNull
public PsiElement createElement(@NotNull ASTNode node) {
if (myPsiElementClass == null) {
throw new IllegalStateException("Cannot create an element for " + node.getElementType() + " without element class");
}
try {
if (myConstructor == null) {
myConstructor = myPsiElementClass.getConstructor(PARAMETER_TYPES);
}
return myConstructor.newInstance(node);
}
catch (Exception e) {
throw new IllegalStateException("No necessary constructor for " + node.getElementType(), e);
}
}
/**
* @return name of special method for operation marked by this token; e.g. "__add__" for "+".
*/
@Nullable
public String getSpecialMethodName() {
return mySpecialMethodName;
}
@Override
public String toString() {
return "Py:" + super.toString();
}
}