langtools/src/jdk.jshell/share/classes/jdk/jshell/OuterWrap.java
changeset 37644 33cf53901cac
parent 35736 bae6ea327651
child 38535 4a25025e0b0d
equal deleted inserted replaced
37643:626e07816dce 37644:33cf53901cac
     1 /*
     1 /*
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015-2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package jdk.jshell;
    26 package jdk.jshell;
    27 
    27 
    28 import jdk.jshell.Wrap.CompoundWrap;
       
    29 import static jdk.jshell.Util.*;
       
    30 import java.util.Locale;
    28 import java.util.Locale;
    31 import javax.tools.Diagnostic;
    29 import javax.tools.Diagnostic;
    32 import javax.tools.JavaFileObject;
    30 import javax.tools.JavaFileObject;
    33 import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject;
    31 import jdk.internal.jshell.remote.RemoteCodes;
    34 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
    32 import static jdk.jshell.Util.*;
       
    33 import static jdk.internal.jshell.remote.RemoteCodes.REPL_PACKAGE;
    35 
    34 
    36 /**
    35 /**
    37  *
    36  *
    38  * @author Robert Field
    37  * @author Robert Field
    39  */
    38  */
    40 final class OuterWrap implements GeneralWrap {
    39 class OuterWrap implements GeneralWrap {
    41 
    40 
    42     private final String packageName;
    41     protected final Wrap w;
    43     private final String className;
       
    44     private final String userSource;
       
    45     private final GeneralWrap w;
       
    46     private final Wrap guts;
       
    47 
    42 
    48     public static OuterWrap wrapInClass(String packageName, String className,
    43     OuterWrap(Wrap wrap) {
    49              String imports, String userSource, Wrap guts) {
    44         this.w = wrap;
    50         GeneralWrap kw = new CompoundWrap(
       
    51                 imports
       
    52                 + "class " + className + " {\n",
       
    53                 guts,
       
    54                 "}\n");
       
    55         return new OuterWrap(packageName, className, userSource, kw, guts);
       
    56     }
       
    57 
       
    58     public static OuterWrap wrapImport(String userSource, Wrap guts) {
       
    59         return new OuterWrap("", "", userSource, guts, guts);
       
    60     }
       
    61 
       
    62     private OuterWrap(String packageName, String className, String userSource,
       
    63             GeneralWrap w, Wrap guts) {
       
    64         this.packageName = packageName;
       
    65         this.className = className;
       
    66         this.userSource = userSource;
       
    67         this.w = w;
       
    68         this.guts = guts;
       
    69     }
    45     }
    70 
    46 
    71     @Override
    47     @Override
    72     public final String wrapped() {
    48     public final String wrapped() {
    73         return w.wrapped();
    49         return w.wrapped();
   112     public int lastSnippetLine() {
    88     public int lastSnippetLine() {
   113         return w.lastSnippetLine();
    89         return w.lastSnippetLine();
   114     }
    90     }
   115 
    91 
   116     public String className() {
    92     public String className() {
   117         return className;
    93         return REPL_DOESNOTMATTER_CLASS_NAME;
   118     }
    94     }
   119 
    95 
   120     public String classFullName() {
    96     public String classFullName() {
   121         return packageName + "." + className;
    97         return REPL_PACKAGE + "." + className();
   122     }
    98     }
   123 
    99 
   124     public String getUserSource() {
   100     @Override
   125         return userSource;
   101     public int hashCode() {
       
   102         return className().hashCode();
   126     }
   103     }
   127 
   104 
   128     Wrap guts() {
   105     @Override
   129         return guts;
   106     public boolean equals(Object o) {
       
   107         return (o instanceof OuterWrap)
       
   108                 ? className().equals(((OuterWrap) o).className())
       
   109                 : false;
       
   110     }
       
   111 
       
   112     @Override
       
   113     public String toString() {
       
   114         return "OW(" + w + ")";
   130     }
   115     }
   131 
   116 
   132     Diag wrapDiag(Diagnostic<? extends JavaFileObject> d) {
   117     Diag wrapDiag(Diagnostic<? extends JavaFileObject> d) {
   133         return new WrappedDiagnostic(d);
   118         return new WrappedDiagnostic(d);
   134     }
   119     }
   135 
   120 
   136     class WrappedDiagnostic extends Diag {
   121     class WrappedDiagnostic extends Diag {
   137 
   122 
   138         private final Diagnostic<? extends JavaFileObject> diag;
   123         final Diagnostic<? extends JavaFileObject> diag;
   139 
   124 
   140         WrappedDiagnostic(Diagnostic<? extends JavaFileObject> diag) {
   125         WrappedDiagnostic(Diagnostic<? extends JavaFileObject> diag) {
   141             this.diag = diag;
   126             this.diag = diag;
   142         }
   127         }
   143 
   128 
   170         public String getMessage(Locale locale) {
   155         public String getMessage(Locale locale) {
   171             return expunge(diag.getMessage(locale));
   156             return expunge(diag.getMessage(locale));
   172         }
   157         }
   173 
   158 
   174         @Override
   159         @Override
   175         Unit unitOrNull() {
       
   176             JavaFileObject fo = diag.getSource();
       
   177             if (fo instanceof SourceMemoryJavaFileObject) {
       
   178                 SourceMemoryJavaFileObject sfo = (SourceMemoryJavaFileObject) fo;
       
   179                 if (sfo.getOrigin() instanceof Unit) {
       
   180                     return (Unit) sfo.getOrigin();
       
   181                 }
       
   182             }
       
   183             return null;
       
   184         }
       
   185 
       
   186         @Override
       
   187         boolean isResolutionError() {
   160         boolean isResolutionError() {
   188             if (!super.isResolutionError()) {
   161             if (!super.isResolutionError()) {
   189                 return false;
   162                 return false;
   190             }
   163             }
   191             for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) {
   164             for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) {
   192                 if (line.trim().startsWith("location:")) {
   165                 if (line.trim().startsWith("location:")) {
   193                     if (!line.contains(REPL_CLASS_PREFIX)) {
   166                     if (!line.contains(RemoteCodes.REPL_CLASS_PREFIX)) {
   194                         // Resolution error must occur within a REPL class or it is not resolvable
   167                         // Resolution error must occur within a REPL class or it is not resolvable
   195                         return false;
   168                         return false;
   196                     }
   169                     }
   197                 }
   170                 }
   198             }
   171             }