Skip to content

Commit 782e41a

Browse files
authored
Painless: Remove extraneous INLINE constant. (#29340)
1 parent 1df43a0 commit 782e41a

File tree

5 files changed

+27
-51
lines changed

5 files changed

+27
-51
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/Location.java

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
public final class Location {
2828
private final String sourceName;
2929
private final int offset;
30-
30+
3131
/**
32-
* Create a new Location
32+
* Create a new Location
3333
* @param sourceName script's name
3434
* @param offset character offset of script element
3535
*/
3636
public Location(String sourceName, int offset) {
3737
this.sourceName = Objects.requireNonNull(sourceName);
3838
this.offset = offset;
3939
}
40-
40+
4141
/**
4242
* Return the script's name
4343
*/
@@ -68,43 +68,31 @@ public RuntimeException createError(RuntimeException exception) {
6868

6969
// This maximum length is theoretically 65535 bytes, but as it's CESU-8 encoded we don't know how large it is in bytes, so be safe
7070
private static final int MAX_NAME_LENGTH = 256;
71-
71+
7272
/** Computes the file name (mostly important for stacktraces) */
73-
public static String computeSourceName(String scriptName, String source) {
73+
public static String computeSourceName(String scriptName) {
7474
StringBuilder fileName = new StringBuilder();
75-
if (scriptName.equals(PainlessScriptEngine.INLINE_NAME)) {
76-
// its an anonymous script, include at least a portion of the source to help identify which one it is
77-
// but don't create stacktraces with filenames that contain newlines or huge names.
75+
// its an anonymous script, include at least a portion of the source to help identify which one it is
76+
// but don't create stacktraces with filenames that contain newlines or huge names.
7877

79-
// truncate to the first newline
80-
int limit = source.indexOf('\n');
81-
if (limit >= 0) {
82-
int limit2 = source.indexOf('\r');
83-
if (limit2 >= 0) {
84-
limit = Math.min(limit, limit2);
85-
}
86-
} else {
87-
limit = source.length();
78+
// truncate to the first newline
79+
int limit = scriptName.indexOf('\n');
80+
if (limit >= 0) {
81+
int limit2 = scriptName.indexOf('\r');
82+
if (limit2 >= 0) {
83+
limit = Math.min(limit, limit2);
8884
}
85+
} else {
86+
limit = scriptName.length();
87+
}
8988

90-
// truncate to our limit
91-
limit = Math.min(limit, MAX_NAME_LENGTH);
92-
fileName.append(source, 0, limit);
89+
// truncate to our limit
90+
limit = Math.min(limit, MAX_NAME_LENGTH);
91+
fileName.append(scriptName, 0, limit);
9392

94-
// if we truncated, make it obvious
95-
if (limit != source.length()) {
96-
fileName.append(" ...");
97-
}
98-
fileName.append(" @ <inline script>");
99-
} else {
100-
// its a named script, just use the name
101-
// but don't trust this has a reasonable length!
102-
if (scriptName.length() > MAX_NAME_LENGTH) {
103-
fileName.append(scriptName, 0, MAX_NAME_LENGTH);
104-
fileName.append(" ...");
105-
} else {
106-
fileName.append(scriptName);
107-
}
93+
// if we truncated, make it obvious
94+
if (limit != scriptName.length()) {
95+
fileName.append(" ...");
10896
}
10997
return fileName.toString();
11098
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScript.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,7 @@ default ScriptException convertToScriptException(Throwable t, Map<String, List<S
9191
scriptStack.add(element.toString());
9292
}
9393
}
94-
// build a name for the script:
95-
final String name;
96-
if (PainlessScriptEngine.INLINE_NAME.equals(getName())) {
97-
name = getSource();
98-
} else {
99-
name = getName();
100-
}
101-
ScriptException scriptException = new ScriptException("runtime error", t, scriptStack, name, PainlessScriptEngine.NAME);
94+
ScriptException scriptException = new ScriptException("runtime error", t, scriptStack, getName(), PainlessScriptEngine.NAME);
10295
for (Map.Entry<String, List<String>> entry : extraMetadata.entrySet()) {
10396
scriptException.addMetadata(entry.getKey(), entry.getValue());
10497
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScriptEngine.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,6 @@ public String getType() {
119119
return NAME;
120120
}
121121

122-
/**
123-
* When a script is anonymous (inline), we give it this name.
124-
*/
125-
static final String INLINE_NAME = "<inline>";
126-
127122
@Override
128123
public <T> T compile(String scriptName, String scriptSource, ScriptContext<T> context, Map<String, String> params) {
129124
Compiler compiler = contextsToCompilers.get(context);
@@ -425,7 +420,7 @@ public Loader run() {
425420
return AccessController.doPrivileged(new PrivilegedAction<Object>() {
426421
@Override
427422
public Object run() {
428-
String name = scriptName == null ? INLINE_NAME : scriptName;
423+
String name = scriptName == null ? source : scriptName;
429424
Constructor<?> constructor = compiler.compile(loader, new MainMethodReserved(), name, source, compilerSettings);
430425

431426
try {
@@ -488,7 +483,7 @@ void compile(Compiler compiler, Loader loader, MainMethodReserved reserved,
488483
AccessController.doPrivileged(new PrivilegedAction<Void>() {
489484
@Override
490485
public Void run() {
491-
String name = scriptName == null ? INLINE_NAME : scriptName;
486+
String name = scriptName == null ? source : scriptName;
492487
compiler.compile(loader, reserved, name, source, compilerSettings);
493488

494489
return null;

modules/lang-painless/src/main/java/org/elasticsearch/painless/antlr/Walker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private Walker(ScriptClassInfo scriptClassInfo, MainMethodReserved reserved, Str
198198
this.reserved.push(reserved);
199199
this.debugStream = debugStream;
200200
this.settings = settings;
201-
this.sourceName = Location.computeSourceName(sourceName, sourceText);
201+
this.sourceName = Location.computeSourceName(sourceName);
202202
this.sourceText = sourceText;
203203
this.globals = new Globals(new BitSet(sourceText.length()));
204204
this.definition = definition;

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/SSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public void write() {
249249
}
250250
visitor.visit(WriterConstants.CLASS_VERSION, classAccess, className, null,
251251
Type.getType(scriptClassInfo.getBaseClass()).getInternalName(), classInterfaces);
252-
visitor.visitSource(Location.computeSourceName(name, source), null);
252+
visitor.visitSource(Location.computeSourceName(name), null);
253253

254254
// Write the a method to bootstrap def calls
255255
MethodWriter bootstrapDef = new MethodWriter(Opcodes.ACC_STATIC | Opcodes.ACC_VARARGS, DEF_BOOTSTRAP_METHOD, visitor,

0 commit comments

Comments
 (0)