1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jshell;
  27 
  28 import jdk.jshell.Wrap.CompoundWrap;
  29 import static jdk.jshell.Util.*;
  30 import java.util.Locale;
  31 import javax.tools.Diagnostic;
  32 import javax.tools.JavaFileObject;
  33 import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject;
  34 
  35 /**
  36  *
  37  * @author Robert Field
  38  */
  39 final class OuterWrap implements GeneralWrap {
  40 
  41     private final String packageName;
  42     private final String className;
  43     private final String userSource;
  44     private final GeneralWrap w;
  45     private final Wrap guts;
  46 
  47     public static OuterWrap wrapInClass(String packageName, String className,
  48              String imports, String userSource, Wrap guts) {
  49         GeneralWrap kw = new CompoundWrap(
  50                 imports
  51                 + "class " + className + " {\n",
  52                 guts,
  53                 "}\n");
  54         return new OuterWrap(packageName, className, userSource, kw, guts);
  55     }
  56 
  57     public static OuterWrap wrapImport(String userSource, Wrap guts) {
  58         return new OuterWrap("", "", userSource, guts, guts);
  59     }
  60 
  61     private OuterWrap(String packageName, String className, String userSource,
  62             GeneralWrap w, Wrap guts) {
  63         this.packageName = packageName;
  64         this.className = className;
  65         this.userSource = userSource;
  66         this.w = w;
  67         this.guts = guts;
  68     }
  69 
  70     @Override
  71     public final String wrapped() {
  72         return w.wrapped();
  73     }
  74 
  75     @Override
  76     public int snippetIndexToWrapIndex(int ui) {
  77         return w.snippetIndexToWrapIndex(ui);
  78     }
  79 
  80     @Override
  81     public int wrapIndexToSnippetIndex(int si) {
  82         return w.wrapIndexToSnippetIndex(si);
  83     }
  84 
  85     @Override
  86     public int firstSnippetIndex() {
  87         return w.firstSnippetIndex();
  88     }
  89 
  90     @Override
  91     public int lastSnippetIndex() {
  92         return w.lastSnippetIndex();
  93     }
  94 
  95     @Override
  96     public int snippetLineToWrapLine(int snline) {
  97         return w.snippetLineToWrapLine(snline);
  98     }
  99 
 100     @Override
 101     public int wrapLineToSnippetLine(int wline) {
 102         return w.wrapLineToSnippetLine(wline);
 103     }
 104 
 105     @Override
 106     public int firstSnippetLine() {
 107         return w.firstSnippetLine();
 108     }
 109 
 110     @Override
 111     public int lastSnippetLine() {
 112         return w.lastSnippetLine();
 113     }
 114 
 115     public String className() {
 116         return className;
 117     }
 118 
 119     public String classFullName() {
 120         return packageName + "." + className;
 121     }
 122 
 123     public String getUserSource() {
 124         return userSource;
 125     }
 126 
 127     Wrap guts() {
 128         return guts;
 129     }
 130 
 131     Diag wrapDiag(Diagnostic<? extends JavaFileObject> d) {
 132         return new WrappedDiagnostic(d);
 133     }
 134 
 135     class WrappedDiagnostic extends Diag {
 136 
 137         private final Diagnostic<? extends JavaFileObject> diag;
 138 
 139         WrappedDiagnostic(Diagnostic<? extends JavaFileObject> diag) {
 140             this.diag = diag;
 141         }
 142 
 143         @Override
 144         public boolean isError() {
 145             return diag.getKind() == Diagnostic.Kind.ERROR;
 146         }
 147 
 148         @Override
 149         public long getPosition() {
 150             return wrapIndexToSnippetIndex(diag.getPosition());
 151         }
 152 
 153         @Override
 154         public long getStartPosition() {
 155             return wrapIndexToSnippetIndex(diag.getStartPosition());
 156         }
 157 
 158         @Override
 159         public long getEndPosition() {
 160             return wrapIndexToSnippetIndex(diag.getEndPosition());
 161         }
 162 
 163         @Override
 164         public String getCode() {
 165             return diag.getCode();
 166         }
 167 
 168         @Override
 169         public String getMessage(Locale locale) {
 170             return expunge(diag.getMessage(locale));
 171         }
 172 
 173         @Override
 174         Unit unitOrNull() {
 175             JavaFileObject fo = diag.getSource();
 176             if (fo instanceof SourceMemoryJavaFileObject) {
 177                 SourceMemoryJavaFileObject sfo = (SourceMemoryJavaFileObject) fo;
 178                 if (sfo.getOrigin() instanceof Unit) {
 179                     return (Unit) sfo.getOrigin();
 180                 }
 181             }
 182             return null;
 183         }
 184 
 185         @Override
 186         public String toString() {
 187             return "WrappedDiagnostic(" + getMessage(null) + ":" + getPosition() + ")";
 188         }
 189     }
 190 }