summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2007-09-18 14:49:58 (EDT)
committerpelder2007-09-18 14:49:58 (EDT)
commita586d0b1f80e720407a5f4c3fbe4f551fc0ae966 (patch)
tree42ab1918f833258a75a24012844624c7aa4e092d
parentf5f4dd1dac9feb263269867e7009d12d6f965bb2 (diff)
downloadorg.eclipse.jet-a586d0b1f80e720407a5f4c3fbe4f551fc0ae966.zip
org.eclipse.jet-a586d0b1f80e720407a5f4c3fbe4f551fc0ae966.tar.gz
org.eclipse.jet-a586d0b1f80e720407a5f4c3fbe4f551fc0ae966.tar.bz2
[197708] Handle Java 5 parameterized types in java:importRoot_R0_8_maintenance
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/ImportTag.java72
1 files changed, 69 insertions, 3 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/ImportTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/ImportTag.java
index 1aae878..11d5b33 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/ImportTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/ImportTag.java
@@ -18,11 +18,21 @@ package org.eclipse.jet.internal.taglib.java;
import org.eclipse.emf.codegen.util.ImportManager;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.jdt.core.dom.ASTVisitor;
+import org.eclipse.jdt.core.dom.Block;
+import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jet.JET2Context;
import org.eclipse.jet.taglib.AbstractFunctionTag;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
import org.eclipse.jet.taglib.java.JavaActionsUtil;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.BadPositionCategoryException;
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.Position;
/**
@@ -45,9 +55,65 @@ public class ImportTag extends AbstractFunctionTag
*/
public String doFunction(TagInfo tc, JET2Context context, String bodyContent) throws JET2TagException
{
- ImportManager importManager = JavaActionsUtil.getImportManager(getOut());
- importManager.addImport(bodyContent);
- return importManager.getImportedName(bodyContent);
+ final ImportManager importManager = JavaActionsUtil.getImportManager(getOut());
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+
+ // create a jface Document to track positions of replaceable text...
+ final IDocument document = new Document();
+ document.set(bodyContent);
+
+ // create a Java statement with the type declaration
+ String varDeclStatement = bodyContent + " x;"; //$NON-NLS-1$
+ // ... and get an AST for it ...
+ parser.setSource(varDeclStatement.toCharArray());
+ parser.setKind(ASTParser.K_STATEMENTS);
+ Block syntheticBlock = (Block)parser.createAST(null);
+
+ // visit the AST, looking for Java 'simple types'
+ syntheticBlock.accept(new ASTVisitor() {
+ public boolean visit(SimpleType node)
+ {
+ // register the fully qualified name with the import manager
+ final String fullyQualifiedName = node.getName().getFullyQualifiedName();
+ importManager.addImport(fullyQualifiedName);
+ try
+ {
+ // register the position so we can replace it later, once we have
+ // recorded the positions of all the qualified names...
+ document.addPosition(new Position(
+ node.getName().getStartPosition(),
+ node.getName().getLength()));
+ }
+ catch (BadLocationException e)
+ {
+ // should not happen, ignore
+ }
+ return super.visit(node);
+ }
+ });
+
+ // Now run through all saved positions, and replace the original text with
+ // the short name from the import manager...
+ try
+ {
+ final Position[] positions = document.getPositions(IDocument.DEFAULT_CATEGORY);
+ for (int i = 0; i < positions.length; i++)
+ {
+ String qualifiedName = document.get(positions[i].offset, positions[i].length);
+ document.replace(positions[i].offset, positions[i].length, importManager.getImportedName(qualifiedName));
+ }
+ }
+ catch (BadPositionCategoryException e)
+ {
+ // should not happen, ignore
+ }
+ catch (BadLocationException e)
+ {
+ // should not happen, ignore
+ }
+
+
+ return document.get();
}
}