langtools/src/jdk.jshell/share/classes/jdk/jshell/Unit.java
changeset 34857 14d1224cfed3
parent 33362 65ec6de1d6b4
child 35000 952a7b4652f0
equal deleted inserted replaced
34856:5ca50af1b45c 34857:14d1224cfed3
   223             }
   223             }
   224         }
   224         }
   225         return false;
   225         return false;
   226     }
   226     }
   227 
   227 
   228     void setStatus() {
   228     void setStatus(AnalyzeTask at) {
   229         if (!compilationDiagnostics.hasErrors()) {
   229         if (!compilationDiagnostics.hasErrors()) {
   230             status = VALID;
   230             status = VALID;
   231         } else if (isRecoverable()) {
   231         } else if (isRecoverable()) {
   232             if (isAttemptingCorral && !recompilationDiagnostics.hasErrors()) {
   232             if (isAttemptingCorral && !recompilationDiagnostics.hasErrors()) {
   233                 status = RECOVERABLE_DEFINED;
   233                 status = RECOVERABLE_DEFINED;
   235                 status = RECOVERABLE_NOT_DEFINED;
   235                 status = RECOVERABLE_NOT_DEFINED;
   236             }
   236             }
   237         } else {
   237         } else {
   238             status = REJECTED;
   238             status = REJECTED;
   239         }
   239         }
   240         checkForOverwrite();
   240         checkForOverwrite(at);
   241 
   241 
   242         state.debug(DBG_GEN, "setStatus() %s - status: %s\n",
   242         state.debug(DBG_GEN, "setStatus() %s - status: %s\n",
   243                 si, status);
   243                 si, status);
   244     }
   244     }
   245 
   245 
   359         si.setCompilationStatus(status, unresolved, diags);
   359         si.setCompilationStatus(status, unresolved, diags);
   360         state.debug(DBG_GEN, "recordCompilation: %s -- status %s, unresolved %s\n",
   360         state.debug(DBG_GEN, "recordCompilation: %s -- status %s, unresolved %s\n",
   361                 si, status, unresolved);
   361                 si, status, unresolved);
   362     }
   362     }
   363 
   363 
   364     private void checkForOverwrite() {
   364     private void checkForOverwrite(AnalyzeTask at) {
   365         secondaryEvents = new ArrayList<>();
   365         secondaryEvents = new ArrayList<>();
   366         if (replaceOldEvent != null) secondaryEvents.add(replaceOldEvent);
   366         if (replaceOldEvent != null) secondaryEvents.add(replaceOldEvent);
   367 
   367 
   368         // Defined methods can overwrite methods of other (equivalent) snippets
   368         // Defined methods can overwrite methods of other (equivalent) snippets
   369         if (si.kind() == Kind.METHOD && status.isDefined) {
   369         if (si.kind() == Kind.METHOD && status.isDefined) {
   370             String oqpt = ((MethodSnippet) si).qualifiedParameterTypes();
   370             MethodSnippet msi = (MethodSnippet)si;
   371             String nqpt = computeQualifiedParameterTypes(si);
   371             String oqpt = msi.qualifiedParameterTypes();
       
   372             String nqpt = computeQualifiedParameterTypes(at, msi);
   372             if (!nqpt.equals(oqpt)) {
   373             if (!nqpt.equals(oqpt)) {
   373                 ((MethodSnippet) si).setQualifiedParamaterTypes(nqpt);
   374                 msi.setQualifiedParamaterTypes(nqpt);
   374                 Status overwrittenStatus = overwriteMatchingMethod(si);
   375                 Status overwrittenStatus = overwriteMatchingMethod(msi);
   375                 if (overwrittenStatus != null) {
   376                 if (overwrittenStatus != null) {
   376                     prevStatus = overwrittenStatus;
   377                     prevStatus = overwrittenStatus;
   377                     signatureChanged = true;
   378                     signatureChanged = true;
   378                 }
   379                 }
   379             }
   380             }
   381     }
   382     }
   382 
   383 
   383     // Check if there is a method whose user-declared parameter types are
   384     // Check if there is a method whose user-declared parameter types are
   384     // different (and thus has a different snippet) but whose compiled parameter
   385     // different (and thus has a different snippet) but whose compiled parameter
   385     // types are the same. if so, consider it an overwrite replacement.
   386     // types are the same. if so, consider it an overwrite replacement.
   386     private Status overwriteMatchingMethod(Snippet si) {
   387     private Status overwriteMatchingMethod(MethodSnippet msi) {
   387         String qpt = ((MethodSnippet) si).qualifiedParameterTypes();
   388         String qpt = msi.qualifiedParameterTypes();
   388 
   389 
   389         // Look through all methods for a method of the same name, with the
   390         // Look through all methods for a method of the same name, with the
   390         // same computed qualified parameter types
   391         // same computed qualified parameter types
   391         Status overwrittenStatus = null;
   392         Status overwrittenStatus = null;
   392         for (MethodSnippet sn : state.methods()) {
   393         for (MethodSnippet sn : state.methods()) {
   393             if (sn != null && sn != si && sn.status().isActive && sn.name().equals(si.name())) {
   394             if (sn != null && sn != msi && sn.status().isActive && sn.name().equals(msi.name())) {
   394                 if (qpt.equals(sn.qualifiedParameterTypes())) {
   395                 if (qpt.equals(sn.qualifiedParameterTypes())) {
   395                     overwrittenStatus = sn.status();
   396                     overwrittenStatus = sn.status();
   396                     SnippetEvent se = new SnippetEvent(
   397                     SnippetEvent se = new SnippetEvent(
   397                             sn, overwrittenStatus, OVERWRITTEN,
   398                             sn, overwrittenStatus, OVERWRITTEN,
   398                             false, si, null, null);
   399                             false, msi, null, null);
   399                     sn.setOverwritten();
   400                     sn.setOverwritten();
   400                     secondaryEvents.add(se);
   401                     secondaryEvents.add(se);
   401                     state.debug(DBG_EVNT,
   402                     state.debug(DBG_EVNT,
   402                             "Overwrite event #%d -- key: %s before: %s status: %s sig: %b cause: %s\n",
   403                             "Overwrite event #%d -- key: %s before: %s status: %s sig: %b cause: %s\n",
   403                             secondaryEvents.size(), se.snippet(), se.previousStatus(),
   404                             secondaryEvents.size(), se.snippet(), se.previousStatus(),
   406             }
   407             }
   407         }
   408         }
   408         return overwrittenStatus;
   409         return overwrittenStatus;
   409     }
   410     }
   410 
   411 
   411     private String computeQualifiedParameterTypes(Snippet si) {
   412     private String computeQualifiedParameterTypes(AnalyzeTask at, MethodSnippet msi) {
   412         MethodSnippet msi = (MethodSnippet) si;
   413         String rawSig = TreeDissector.createBySnippet(at, msi).typeOfMethod();
   413         String qpt;
       
   414         AnalyzeTask at = state.taskFactory.new AnalyzeTask(msi.outerWrap());
       
   415         String rawSig = new TreeDissector(at).typeOfMethod();
       
   416         String signature = expunge(rawSig);
   414         String signature = expunge(rawSig);
   417         int paren = signature.lastIndexOf(')');
   415         int paren = signature.lastIndexOf(')');
   418         if (paren < 0) {
   416 
   419             // Uncompilable snippet, punt with user parameter types
   417         // Extract the parameter type string from the method signature,
   420             qpt = msi.parameterTypes();
   418         // if method did not compile use the user-supplied parameter types
   421         } else {
   419         return paren >= 0
   422             qpt = signature.substring(0, paren + 1);
   420                 ? signature.substring(0, paren + 1)
   423         }
   421                 : msi.parameterTypes();
   424         return qpt;
       
   425     }
   422     }
   426 
   423 
   427     SnippetEvent event(String value, Exception exception) {
   424     SnippetEvent event(String value, Exception exception) {
   428         boolean wasSignatureChanged = sigChanged();
   425         boolean wasSignatureChanged = sigChanged();
   429         state.debug(DBG_EVNT, "Snippet: %s id: %s before: %s status: %s sig: %b cause: %s\n",
   426         state.debug(DBG_EVNT, "Snippet: %s id: %s before: %s status: %s sig: %b cause: %s\n",