Skip to content

Commit 0f6c714

Browse files
committed
GRAILS-6583 added Andrew's code for GSP debugging
1 parent c0e86d5 commit 0f6c714

File tree

4 files changed

+836
-0
lines changed

4 files changed

+836
-0
lines changed

maven/grails-web.mf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Import-Template:
1919
org.codehaus.groovy.ant.*;version="${groovy.version:[=.=.=, =.+1.0)}",
2020
org.codehaus.groovy.tools.*;version="${groovy.version:[=.=.=, =.+1.0)}",
2121
org.codehaus.groovy.syntax.*;version="${groovy.version:[=.=.=, =.+1.0)}",
22+
org.codehaus.groovy.ast.*;version="${groovy.version:[=.=.=, =.+1.0)}",
23+
org.codehaus.groovy.ast.expr.*;version="${groovy.version:[=.=.=, =.+1.0)}",
24+
org.codehaus.groovy.ast.stmt.*;version="${groovy.version:[=.=.=, =.+1.0)}",
2225
org.codehaus.groovy.grails.*;version="${grails.version:[=.=.=, =.+1.0)}",
2326
org.codehaus.groovy.transform.*;version="${groovy.version:[=.=.=, =.+1.0)}",
2427
org.codehaus.groovy.classgen.*;version="${groovy.version:[=.=.=, =.+1.0)}",

src/java/org/codehaus/groovy/grails/web/pages/GroovyPageParser.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public class GroovyPageParser implements Tokens {
7777
private GSPWriter out;
7878
private String className;
7979
private String packageName;
80+
private String sourceName; // last segment of the file name (eg- index.gsp)
8081
private boolean finalPass = false;
8182
private int tagIndex;
8283
private Map tagContext;
@@ -207,6 +208,7 @@ public GroovyPageParser(String name, String uri, String filename, InputStream in
207208
this.pageName = uri;
208209
this.environment = Environment.getCurrent();
209210
makeName(name);
211+
makeSourceName(filename);
210212
} // Parse()
211213

212214
private Map<String, String> parseDirectives(String gspSource) {
@@ -642,6 +644,22 @@ else if (ix == 0 && c >= '0' && c <= '9')
642644
className = buf.toString();
643645
} // makeName()
644646

647+
/**
648+
* find the simple name of this gsp
649+
* @param filename the fully qualified file name
650+
*/
651+
private void makeSourceName(String filename) {
652+
if (filename != null) {
653+
int lastSegmentStart = filename.lastIndexOf('/');
654+
if (lastSegmentStart == -1) {
655+
lastSegmentStart = filename.lastIndexOf('\\');
656+
}
657+
sourceName = filename.substring(lastSegmentStart + 1);
658+
} else {
659+
sourceName = className;
660+
}
661+
}
662+
645663
private static boolean match(CharSequence pat, CharSequence text, int start) {
646664
int ix = start, ixz = text.length(), ixy = start + pat.length();
647665
if (ixz > ixy)
@@ -789,6 +807,10 @@ private void page() {
789807
out.println("null");
790808
}
791809
out.println("}");
810+
811+
if (shouldAddLineNumbers()) {
812+
addLineNumbers();
813+
}
792814
} else {
793815
for (int i = 0; i < DEFAULT_IMPORTS.length; i++) {
794816
out.print("import ");
@@ -798,6 +820,64 @@ private void page() {
798820
}
799821
} // page()
800822

823+
/**
824+
* Determines if the line numbers array should be added to the generated Groovy class.
825+
* @return true if they should
826+
*/
827+
private boolean shouldAddLineNumbers() {
828+
try {
829+
// for now, we support this through a system property.
830+
String prop = System.getenv("GROOVY_PAGE_ADD_LINE_NUMBERS");
831+
return Boolean.valueOf(prop).booleanValue();
832+
} catch (Exception e) {
833+
// something wild happened
834+
return false;
835+
}
836+
}
837+
838+
/**
839+
* Adds the line numbers array to the end of the generated Groovy ModuleNode
840+
* in a way suitable for the LineNumberTransform AST transform to operate on it
841+
*/
842+
private void addLineNumbers() {
843+
out.println();
844+
out.println("@org.codehaus.groovy.grails.web.transform.LineNumber(");
845+
out.print("\tlines = [");
846+
// get the line numbers here. this will mean that the last 2 lines will not be captured in the
847+
// line number information, but that's OK since a user cannot set a breakpoint there anyway.
848+
int[] lineNumbers = filterTrailing0s(out.getLineNumbers());
849+
850+
for (int i = 0; i < lineNumbers.length; i++) {
851+
out.print(lineNumbers[i]);
852+
if (i < lineNumbers.length - 1) {
853+
out.print(", ");
854+
}
855+
}
856+
out.println("],");
857+
out.println("\tsourceName = \"" + sourceName + "\"");
858+
out.println(")");
859+
out.println("class ___LineNumberPlaceholder { }");
860+
}
861+
862+
/**
863+
* Filters trailing 0s from the line number array
864+
* @param lineNumbers the line number array
865+
* @return a new array that removes all 0s from the end of it
866+
*/
867+
private int[] filterTrailing0s(int[] lineNumbers) {
868+
int startLocation = lineNumbers.length - 1;
869+
for (int i = lineNumbers.length -1; i >= 0; i--) {
870+
if (lineNumbers[i] > 0) {
871+
startLocation = i + 1;
872+
break;
873+
}
874+
}
875+
876+
int[] newLineNumbers = new int[startLocation];
877+
System.arraycopy(lineNumbers, 0, newLineNumbers, 0, startLocation);
878+
return newLineNumbers;
879+
}
880+
801881
private void endTag() {
802882
if (!finalPass)
803883
return;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2003-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.groovy.grails.web.transform;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
import org.codehaus.groovy.transform.GroovyASTTransformationClass;
24+
25+
/**
26+
* @author Andrew Eisenberg
27+
* @created Jul 22, 2010
28+
*/
29+
@Retention(RetentionPolicy.SOURCE)
30+
@Target({ ElementType.TYPE })
31+
@GroovyASTTransformationClass({"org.codehaus.groovy.grails.web.transform.LineNumberTransform"})
32+
public @interface LineNumber {
33+
int[] lines();
34+
String sourceName();
35+
}

0 commit comments

Comments
 (0)