/* * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * Wrapping of source into Java methods, fields, etc. All but outer layer * wrapping with imports and class.
*/ abstractclass Wrap implements GeneralWrap {
/**Create a stub of a compilable representation of a variable snippet. * The variable is always represented by a field. If the variable * in the snippet has an initializer, the field is initialized by * calling the DOIT_METHOD_NAME method. * * In some cases, the real inferred type of the variable may be non-denotable * (e.g. intersection types). The declared type of the field must always * be denotable (i.e. such that it can be written into the classfile), but * if the real type is potentially non-denotable, the {@code enhanced} parameter * must be true. * * @param source the snippet's masked source code * @param wtype variable's denotable type suitable for field declaration * @param brackets any [] that should be appended to the type * @param wname a wrap of the source that denotes the name of the variable * @param winit Initializer or null * @param enhanced if the real inferred type of the variable is potentially * non-denotable, this must be true * @return a Wrap that declares the given variable, potentially with * an initialization method
*/ publicstatic Wrap varWrap(String source, Wrap wtype, String brackets,
Wrap wname, Wrap winit, boolean enhanced,
Wrap anonDeclareWrap) {
List<Object> components = new ArrayList<>();
components.add(new VarDeclareWrap(wtype, brackets, wname));
Wrap wmeth;
void verify(String s) { if (begin < 0 || end <= begin || end > s.length()) { thrownew InternalError("Bad Range: " + s + "[" + begin + "," + end + "]");
}
}
@Override public String toString() { return"Range[" + begin + "," + end + ")";
}
}
publicstaticclass CompoundWrap extends Wrap {
final Object[] os; final String wrapped; finalint snidxFirst; finalint snidxLast; finalint snlineFirst; finalint snlineLast;
CompoundWrap(Object... os) { this.os = os; int sniFirst = Integer.MAX_VALUE; int sniLast = Integer.MIN_VALUE; int snlnFirst = Integer.MAX_VALUE; int snlnLast = Integer.MIN_VALUE;
StringBuilder sb = new StringBuilder(); for (Object o : os) { if (o instanceof String) {
String s = (String) o;
sb.append(s);
} elseif (o instanceof Wrap) {
Wrap w = (Wrap) o; if (w.firstSnippetIndex() < sniFirst) {
sniFirst = w.firstSnippetIndex();
} if (w.lastSnippetIndex() > sniLast) {
sniLast = w.lastSnippetIndex();
} if (w.firstSnippetLine() < snlnFirst) {
snlnFirst = w.firstSnippetLine();
} if (w.lastSnippetLine() > snlnLast) {
snlnLast = w.lastSnippetLine();
}
sb.append(w.wrapped());
} else { thrownew InternalError("Bad object in CommoundWrap: " + o);
}
} this.wrapped = sb.toString(); this.snidxFirst = sniFirst; this.snidxLast = sniLast; this.snlineFirst = snlnFirst; this.snlineLast = snlnLast;
}
@Override public String wrapped() { return wrapped;
}
@Override publicint snippetIndexToWrapIndex(int sni) { int before = 0; for (Object o : os) { if (o instanceof String) {
String s = (String) o;
before += s.length();
} elseif (o instanceof Wrap) {
Wrap w = (Wrap) o; if (sni >= w.firstSnippetIndex() && sni < w.lastSnippetIndex()) { int wwi = w.snippetIndexToWrapIndex(sni);
debugWrap("\nCommoundWrap.snippetIndexToWrapIndex: SnippetIndex(%d) -> WrapIndex(%d + %d = %d)"
+ "\n === %s",
sni, wwi, before, wwi + before, wrapped()); return wwi + before;
}
before += w.wrapped().length();
}
} return 0;
}
Wrap wrapIndexToWrap(long wi) { int before = 0;
Wrap w = null; for (Object o : os) { if (o instanceof String) {
String s = (String) o;
before += s.length();
} elseif (o instanceof Wrap) {
w = (Wrap) o; int len = w.wrapped().length(); if ((wi - before) <= len) {
debugWrap("CommoundWrap.wrapIndexToWrap: Defer to wrap %s - wi: %d. before; %d >>> %s\n",
w, wi, before, w.wrapped()); return w;
}
before += len;
}
} return w;
}
@Override publicint wrapIndexToSnippetIndex(int wi) { int before = 0; for (Object o : os) { if (o instanceof String) {
String s = (String) o;
before += s.length();
} elseif (o instanceof Wrap) {
Wrap w = (Wrap) o; int len = w.wrapped().length(); if ((wi - before) <= len) { int si = w.wrapIndexToSnippetIndex(wi - before);
debugWrap("\nCommoundWrap.wrapIndexToSnippetIndex: WrapIndex(%d) -> SnippetIndex(%d)\n",
wi, si); return si;
}
before += len;
}
} return lastSnippetIndex();
}
@Override publicint snippetLineToWrapLine(int snline) { int before = 0; for (Object o : os) { if (o instanceof String) {
String s = (String) o;
before += countLines(s);
} elseif (o instanceof Wrap) {
Wrap w = (Wrap) o; if (snline >= w.firstSnippetLine() && snline <= w.lastSnippetLine()) { return w.snippetLineToWrapLine(snline) + before;
}
before += countLines(w.wrapped());
}
} return 0;
}
Wrap wrapLineToWrap(int wline) { int before = 0;
Wrap w = null; for (Object o : os) { if (o instanceof String) {
String s = (String) o;
before += countLines(s);
} elseif (o instanceof Wrap) {
w = (Wrap) o; int lns = countLines(w.wrapped()); if ((wline - before) <= lns) { return w;
}
before += lns;
}
} return w;
}
@Override publicint wrapLineToSnippetLine(int wline) { int before = 0; for (Object o : os) { if (o instanceof String) {
String s = (String) o;
before += countLines(s);
} elseif (o instanceof Wrap) {
Wrap w = (Wrap) o; int lns = countLines(w.wrapped()); if ((wline - before) <= lns) { return w.wrapLineToSnippetLine(wline - before);
}
before += lns;
}
} return 0;
}
@Override public String toString() { return"CompoundWrap(" + Arrays.stream(os)
.map(o -> (o instanceof String)
? "\"" + o + "\""
: o.toString())
.collect(joining(","))
+ ")";
}
}
staticclass RangeWrap extends Wrap {
final Range range; final String wrapped; // The snippet portion of the source finalint firstSnline; // Line count to start of snippet portion finalint lastSnline; // Line count to end of snippet portion
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.