PsiMethod (@NotNull PsiElementFactory elementFactory, @Nullable PsiElement context) { checkRequirements(); String parameters = StreamEx.of(myParameters).map(p -> p.accessibleType + " " + p.name).joining(", ", "(", ")"); String returnExpression = ("void".equals(myReturnType) ? "member." : "return (" + myReturnType + ")member.") + myMemberAccessor.getAccessExpression(); String methodBody = " java.lang.Class<?> klass = " + myMemberAccessor.getClassLookupExpression() + ";\n" + " " + myMemberAccessor.getMemberType() + " member = null;\n" + " int interfaceNumber = -1;\n" + " Class<?>[] interfaces = null;\n" + " while (member == null) {\n" + " try {\n" + " member = klass." + myMemberAccessor.getMemberLookupExpression() + ";\n" + " } catch (java.lang.ReflectiveOperationException e) {\n" + " if (interfaceNumber == -1) {\n" + " interfaces = klass.getInterfaces();\n" + " interfaceNumber = 0;\n" + " }\n" + " if (interfaceNumber < interfaces.length) {\n" + " klass = interfaces[interfaceNumber];\n" + " interfaceNumber += 1;\n" + " } else {\n" + " klass = klass.getSuperclass();\n" + " if (klass == null) throw e;\n" + " interfaceNumber = -1;\n" + " }\n" + " }\n" + " }\n" + " member.setAccessible(true);\n" + " " + returnExpression + ";\n"; List<String> possibleExceptions = myMemberAccessor.getPossibleExceptions(); if (!possibleExceptions.isEmpty()) { methodBody = "try {\n" + methodBody + "}" + createCatchBlocks(possibleExceptions); } String methodText = "public" + (myIsStatic ? " static " : " ") + myReturnType + " " + myName + parameters + " { \n" + methodBody + "}\n"; return elementFactory.createMethodFromText(methodText, context); }