PsiLocalVariable (PsiSubstitutor callSubstitutor, PsiCodeBlock block) { PsiClass containingClass = myMethod.getContainingClass(); if (myMethod.hasModifierProperty(PsiModifier.STATIC) || containingClass == null || containingClass instanceof PsiUnnamedClass) return null; PsiType thisType = GenericsUtil.getVariableTypeByExpressionType(myFactory.createType(containingClass, callSubstitutor)); String thisVarName = new VariableNameGenerator(myMethod.getFirstChild(), VariableKind.LOCAL_VARIABLE) .byType(thisType).byName("self").generate(true); PsiExpression initializer = myFactory.createExpressionFromText("null", null); PsiDeclarationStatement declaration = myFactory.createVariableDeclarationStatement(thisVarName, thisType, initializer); declaration = (PsiDeclarationStatement)block.addAfter(declaration, null); return (PsiLocalVariable)declaration.getDeclaredElements()[0]; }