Permalink
Browse files

GRAILS-6583 added Andrew's code for GSP debugging

  • Loading branch information...
1 parent 804d6ef commit 584dc9b6e7aa6daaa66fb96bed368ea905ac03aa @burtbeckwith burtbeckwith committed Aug 5, 2010
View
@@ -22,6 +22,9 @@ Import-Template:
org.codehaus.groovy.ant.*;version="${groovy.version:[=.=.=, =.+1.0)}",
org.codehaus.groovy.tools.*;version="${groovy.version:[=.=.=, =.+1.0)}",
org.codehaus.groovy.syntax.*;version="${groovy.version:[=.=.=, =.+1.0)}",
+ org.codehaus.groovy.ast.*;version="${groovy.version:[=.=.=, =.+1.0)}",
+ org.codehaus.groovy.ast.expr.*;version="${groovy.version:[=.=.=, =.+1.0)}",
+ org.codehaus.groovy.ast.stmt.*;version="${groovy.version:[=.=.=, =.+1.0)}",
org.codehaus.groovy.grails.*;version="${grails.version:[=.=.=, =.+1.0)}",
org.hibernate.*;version="${hibernate.version:[=.=.=, =.+1.0)}",
com.opensymphony.module.sitemesh.*;version="${sitemesh.version:[=.=.=, =.+1.0)}",
@@ -91,6 +91,7 @@
private GSPWriter out;
private String className;
private String packageName;
+ private String sourceName; // last segment of the file name (eg- index.gsp)
private boolean finalPass = false;
private int tagIndex;
private Map<Object, Object> tagContext;
@@ -227,6 +228,7 @@ public GroovyPageParser(String name, String uri, String filename, InputStream in
pageName = uri;
environment = Environment.getCurrent();
makeName(name);
+ makeSourceName(filename);
}
private Map<String, String> parseDirectives(String gspSource) {
@@ -683,6 +685,22 @@ else if (ix == 0 && c >= '0' && c <= '9') {
className = buf.toString();
}
+ /**
+ * find the simple name of this gsp
+ * @param filename the fully qualified file name
+ */
+ private void makeSourceName(String filename) {
+ if (filename != null) {
+ int lastSegmentStart = filename.lastIndexOf('/');
+ if (lastSegmentStart == -1) {
+ lastSegmentStart = filename.lastIndexOf('\\');
+ }
+ sourceName = filename.substring(lastSegmentStart + 1);
+ } else {
+ sourceName = className;
+ }
+ }
+
private static boolean match(CharSequence pat, CharSequence text, int start) {
int ix = start, ixz = text.length(), ixy = start + pat.length();
if (ixz > ixy) {
@@ -831,6 +849,10 @@ private void page() {
out.println("null");
}
out.println("}");
+
+ if (shouldAddLineNumbers()) {
+ addLineNumbers();
+ }
}
else {
for (int i = 0; i < DEFAULT_IMPORTS.length; i++) {
@@ -839,6 +861,64 @@ private void page() {
}
}
}
+
+ /**
+ * Determines if the line numbers array should be added to the generated Groovy class.
+ * @return true if they should
+ */
+ private boolean shouldAddLineNumbers() {
+ try {
+ // for now, we support this through a system property.
+ String prop = System.getenv("GROOVY_PAGE_ADD_LINE_NUMBERS");
+ return Boolean.valueOf(prop).booleanValue();
+ } catch (Exception e) {
+ // something wild happened
+ return false;
+ }
+ }
+
+ /**
+ * Adds the line numbers array to the end of the generated Groovy ModuleNode
+ * in a way suitable for the LineNumberTransform AST transform to operate on it
+ */
+ private void addLineNumbers() {
+ out.println();
+ out.println("@org.codehaus.groovy.grails.web.transform.LineNumber(");
+ out.print("\tlines = [");
+ // get the line numbers here. this will mean that the last 2 lines will not be captured in the
+ // line number information, but that's OK since a user cannot set a breakpoint there anyway.
+ int[] lineNumbers = filterTrailing0s(out.getLineNumbers());
+
+ for (int i = 0; i < lineNumbers.length; i++) {
+ out.print(lineNumbers[i]);
+ if (i < lineNumbers.length - 1) {
+ out.print(", ");
+ }
+ }
+ out.println("],");
+ out.println("\tsourceName = \"" + sourceName + "\"");
+ out.println(")");
+ out.println("class ___LineNumberPlaceholder { }");
+ }
+
+ /**
+ * Filters trailing 0s from the line number array
+ * @param lineNumbers the line number array
+ * @return a new array that removes all 0s from the end of it
+ */
+ private int[] filterTrailing0s(int[] lineNumbers) {
+ int startLocation = lineNumbers.length - 1;
+ for (int i = lineNumbers.length -1; i >= 0; i--) {
+ if (lineNumbers[i] > 0) {
+ startLocation = i + 1;
+ break;
+ }
+ }
+
+ int[] newLineNumbers = new int[startLocation];
+ System.arraycopy(lineNumbers, 0, newLineNumbers, 0, startLocation);
+ return newLineNumbers;
+ }
private void endTag() {
if (!finalPass) return;
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2003-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.codehaus.groovy.grails.web.transform;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.codehaus.groovy.transform.GroovyASTTransformationClass;
+
+/**
+ * @author Andrew Eisenberg
+ * @created Jul 22, 2010
+ */
+@Retention(RetentionPolicy.SOURCE)
+@Target({ ElementType.TYPE })
+@GroovyASTTransformationClass({"org.codehaus.groovy.grails.web.transform.LineNumberTransform"})
+public @interface LineNumber {
+ int[] lines();
+ String sourceName();
+}
Oops, something went wrong.

0 comments on commit 584dc9b

Please sign in to comment.