src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java

Print this page




 906                     @Override
 907                     public void perform(ConsoleReader in) throws IOException {
 908                         performToVar(in, type);
 909                     }
 910                 });
 911                 int idx = type.lastIndexOf(".");
 912                 if (idx > 0) {
 913                     String stype = type.substring(idx + 1);
 914                     QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
 915                     if (res.isUpToDate() && res.getNames().contains(type)
 916                             && !res.isResolvable()) {
 917                         fixes.add(new Fix() {
 918                             @Override
 919                             public String displayName() {
 920                                 return "import: " + type + ". " +
 921                                         repl.messageFormat("jshell.console.create.variable");
 922                             }
 923 
 924                             @Override
 925                             public void perform(ConsoleReader in) throws IOException {
 926                                 repl.processCompleteSource("import " + type + ";");
 927                                 in.println("Imported: " + type);
 928                                 performToVar(in, stype);
 929                             }
 930                         });
 931                     }
 932                 }
 933                 return new FixResult(fixes, null);
 934             }
 935         },
 936         new FixComputer('m', false) { //compute "Introduce method" Fix:
 937             private void performToMethod(ConsoleReader in, String type, String code) throws IOException {
 938                 in.redrawLine();
 939                 if (!code.trim().endsWith(";")) {
 940                     in.putString(";");
 941                 }
 942                 in.putString(" }");
 943                 in.setCursorPosition(0);
 944                 String afterCursor = type.equals("void")
 945                         ? "() { "
 946                         : "() { return ";


1011                     @Override
1012                     public void perform(ConsoleReader in) throws IOException {
1013                         performToMethod(in, type, codeToCursor);
1014                     }
1015                 });
1016                 int idx = type.lastIndexOf(".");
1017                 if (idx > 0) {
1018                     String stype = type.substring(idx + 1);
1019                     QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
1020                     if (res.isUpToDate() && res.getNames().contains(type)
1021                             && !res.isResolvable()) {
1022                         fixes.add(new Fix() {
1023                             @Override
1024                             public String displayName() {
1025                                 return "import: " + type + ". " +
1026                                         repl.messageFormat("jshell.console.create.method");
1027                             }
1028 
1029                             @Override
1030                             public void perform(ConsoleReader in) throws IOException {
1031                                 repl.processCompleteSource("import " + type + ";");
1032                                 in.println("Imported: " + type);
1033                                 performToMethod(in, stype, codeToCursor);
1034                             }
1035                         });
1036                     }
1037                 }
1038                 return new FixResult(fixes, null);
1039             }
1040         },
1041         new FixComputer('i', true) { //compute "Add import" Fixes:
1042             @Override
1043             public FixResult compute(JShellTool repl, String code, int cursor) {
1044                 QualifiedNames res = repl.analysis.listQualifiedNames(code, cursor);
1045                 List<Fix> fixes = new ArrayList<>();
1046                 for (String fqn : res.getNames()) {
1047                     fixes.add(new Fix() {
1048                         @Override
1049                         public String displayName() {
1050                             return "import: " + fqn;
1051                         }
1052 
1053                         @Override
1054                         public void perform(ConsoleReader in) throws IOException {
1055                             repl.processCompleteSource("import " + fqn + ";");
1056                             in.println("Imported: " + fqn);
1057                             in.redrawLine();
1058                         }
1059                     });
1060                 }
1061                 if (res.isResolvable()) {
1062                     return new FixResult(Collections.emptyList(),
1063                             repl.messageFormat("jshell.console.resolvable"));
1064                 } else {
1065                     String error = "";
1066                     if (fixes.isEmpty()) {
1067                         error = repl.messageFormat("jshell.console.no.candidate");
1068                     }
1069                     if (!res.isUpToDate()) {
1070                         error += repl.messageFormat("jshell.console.incomplete");
1071                     }
1072                     return new FixResult(fixes, error);
1073                 }
1074             }
1075         }




 906                     @Override
 907                     public void perform(ConsoleReader in) throws IOException {
 908                         performToVar(in, type);
 909                     }
 910                 });
 911                 int idx = type.lastIndexOf(".");
 912                 if (idx > 0) {
 913                     String stype = type.substring(idx + 1);
 914                     QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
 915                     if (res.isUpToDate() && res.getNames().contains(type)
 916                             && !res.isResolvable()) {
 917                         fixes.add(new Fix() {
 918                             @Override
 919                             public String displayName() {
 920                                 return "import: " + type + ". " +
 921                                         repl.messageFormat("jshell.console.create.variable");
 922                             }
 923 
 924                             @Override
 925                             public void perform(ConsoleReader in) throws IOException {
 926                                 repl.processSource("import " + type + ";");
 927                                 in.println("Imported: " + type);
 928                                 performToVar(in, stype);
 929                             }
 930                         });
 931                     }
 932                 }
 933                 return new FixResult(fixes, null);
 934             }
 935         },
 936         new FixComputer('m', false) { //compute "Introduce method" Fix:
 937             private void performToMethod(ConsoleReader in, String type, String code) throws IOException {
 938                 in.redrawLine();
 939                 if (!code.trim().endsWith(";")) {
 940                     in.putString(";");
 941                 }
 942                 in.putString(" }");
 943                 in.setCursorPosition(0);
 944                 String afterCursor = type.equals("void")
 945                         ? "() { "
 946                         : "() { return ";


1011                     @Override
1012                     public void perform(ConsoleReader in) throws IOException {
1013                         performToMethod(in, type, codeToCursor);
1014                     }
1015                 });
1016                 int idx = type.lastIndexOf(".");
1017                 if (idx > 0) {
1018                     String stype = type.substring(idx + 1);
1019                     QualifiedNames res = repl.analysis.listQualifiedNames(stype, stype.length());
1020                     if (res.isUpToDate() && res.getNames().contains(type)
1021                             && !res.isResolvable()) {
1022                         fixes.add(new Fix() {
1023                             @Override
1024                             public String displayName() {
1025                                 return "import: " + type + ". " +
1026                                         repl.messageFormat("jshell.console.create.method");
1027                             }
1028 
1029                             @Override
1030                             public void perform(ConsoleReader in) throws IOException {
1031                                 repl.processSource("import " + type + ";");
1032                                 in.println("Imported: " + type);
1033                                 performToMethod(in, stype, codeToCursor);
1034                             }
1035                         });
1036                     }
1037                 }
1038                 return new FixResult(fixes, null);
1039             }
1040         },
1041         new FixComputer('i', true) { //compute "Add import" Fixes:
1042             @Override
1043             public FixResult compute(JShellTool repl, String code, int cursor) {
1044                 QualifiedNames res = repl.analysis.listQualifiedNames(code, cursor);
1045                 List<Fix> fixes = new ArrayList<>();
1046                 for (String fqn : res.getNames()) {
1047                     fixes.add(new Fix() {
1048                         @Override
1049                         public String displayName() {
1050                             return "import: " + fqn;
1051                         }
1052 
1053                         @Override
1054                         public void perform(ConsoleReader in) throws IOException {
1055                             repl.processSource("import " + fqn + ";");
1056                             in.println("Imported: " + fqn);
1057                             in.redrawLine();
1058                         }
1059                     });
1060                 }
1061                 if (res.isResolvable()) {
1062                     return new FixResult(Collections.emptyList(),
1063                             repl.messageFormat("jshell.console.resolvable"));
1064                 } else {
1065                     String error = "";
1066                     if (fixes.isEmpty()) {
1067                         error = repl.messageFormat("jshell.console.no.candidate");
1068                     }
1069                     if (!res.isUpToDate()) {
1070                         error += repl.messageFormat("jshell.console.incomplete");
1071                     }
1072                     return new FixResult(fixes, error);
1073                 }
1074             }
1075         }