org.codehaus.groovy.ast.ImportNode Java Examples
The following examples show how to use
org.codehaus.groovy.ast.ImportNode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ASTTransformer.java From pom-manipulation-ext with Apache License 2.0 | 6 votes |
public void visit(ASTNode[] nodes, SourceUnit source) { init(nodes, source); AnnotatedNode parent = (AnnotatedNode) nodes[1]; AnnotationNode node = (AnnotationNode) nodes[0]; if (MAVEN_TYPE.equals( node.getClassNode()) || GRADLE_TYPE.equals( node.getClassNode() )) { type = ( MAVEN_TYPE.equals( node.getClassNode()) ) ? Type.MAVEN : Type.GRADLE; if ( parent instanceof DeclarationExpression ) { changeBaseScriptTypeFromDeclaration( (DeclarationExpression) parent, node ); } else if ( parent instanceof ImportNode || parent instanceof PackageNode ) { changeBaseScriptTypeFromPackageOrImport( source, parent, node ); } else if ( parent instanceof ClassNode ) { changeBaseScriptTypeFromClass( (ClassNode) parent ); } } }
Example #2
Source File: GroovyASTUtils.java From groovy-language-server with Apache License 2.0 | 6 votes |
public static Range findAddImportRange(ASTNode offsetNode, ASTNodeVisitor astVisitor) { ModuleNode moduleNode = (ModuleNode) GroovyASTUtils.getEnclosingNodeOfType(offsetNode, ModuleNode.class, astVisitor); if (moduleNode == null) { return new Range(new Position(0, 0), new Position(0, 0)); } ASTNode afterNode = null; if (afterNode == null) { List<ImportNode> importNodes = moduleNode.getImports(); if (importNodes.size() > 0) { afterNode = importNodes.get(importNodes.size() - 1); } } if (afterNode == null) { afterNode = moduleNode.getPackage(); } if (afterNode == null) { return new Range(new Position(0, 0), new Position(0, 0)); } Range nodeRange = GroovyLanguageServerUtils.astNodeToRange(afterNode); Position position = new Position(nodeRange.getEnd().getLine() + 1, 0); return new Range(position, position); }
Example #3
Source File: StaticImportVisitor.java From groovy with Apache License 2.0 | 6 votes |
private Expression transformMapEntryExpression(MapEntryExpression me, ClassNode constructorCallType) { Expression key = me.getKeyExpression(); Expression value = me.getValueExpression(); ModuleNode module = currentClass.getModule(); if (module != null && key instanceof ConstantExpression) { Map<String, ImportNode> importNodes = module.getStaticImports(); if (importNodes.containsKey(key.getText())) { ImportNode importNode = importNodes.get(key.getText()); if (importNode.getType().equals(constructorCallType)) { String newKey = importNode.getFieldName(); return new MapEntryExpression(new ConstantExpression(newKey), value.transformExpression(this)); } } } return me; }
Example #4
Source File: AstBuilderTransformation.java From groovy with Apache License 2.0 | 6 votes |
/** * Creates the trap and captures all the ways in which a class may be referenced via imports. * * @param imports all the imports from the source * @param importPackages all the imported packages from the source * @param source the reader source that contains source for the SourceUnit * @param sourceUnit the source unit being compiled. Used for error messages. */ AstBuilderInvocationTrap(List<ImportNode> imports, List<ImportNode> importPackages, ReaderSource source, SourceUnit sourceUnit) { super(source, sourceUnit); // factory type may be references as fully qualified, an import, or an alias factoryTargets.add("org.codehaus.groovy.ast.builder.AstBuilder");//default package if (imports != null) { for (ImportNode importStatement : imports) { if ("org.codehaus.groovy.ast.builder.AstBuilder".equals(importStatement.getType().getName())) { factoryTargets.add(importStatement.getAlias()); } } } if (importPackages != null) { for (ImportNode importPackage : importPackages) { if ("org.codehaus.groovy.ast.builder.".equals(importPackage.getPackageName())) { factoryTargets.add("AstBuilder"); break; } } } }
Example #5
Source File: FindTypeUsagesVisitor.java From netbeans with Apache License 2.0 | 6 votes |
@Override public void visitImports(ModuleNode node) { for (ImportNode importNode : node.getImports()) { final ClassNode importType = importNode.getType(); if (!importNode.isStar()) { // ImportNode itself doesn't contain line/column information, so we need set them manually importNode.setLineNumber(importType.getLineNumber()); importNode.setColumnNumber(importType.getColumnNumber()); importNode.setLastLineNumber(importType.getLastLineNumber()); importNode.setLastColumnNumber(importType.getLastColumnNumber()); if (isEquals(importNode)) { usages.add(new ASTUtils.FakeASTNode(importNode, importNode.getClassName())); } } } super.visitImports(node); }
Example #6
Source File: ASTNodeVisitor.java From groovy-language-server with Apache License 2.0 | 5 votes |
@Override public void visitImports(ModuleNode node) { if (node != null) { for (ImportNode importNode : node.getImports()) { pushASTNode(importNode); visitAnnotations(importNode); importNode.visit(this); popASTNode(); } for (ImportNode importStarNode : node.getStarImports()) { pushASTNode(importStarNode); visitAnnotations(importStarNode); importStarNode.visit(this); popASTNode(); } for (ImportNode importStaticNode : node.getStaticImports().values()) { pushASTNode(importStaticNode); visitAnnotations(importStaticNode); importStaticNode.visit(this); popASTNode(); } for (ImportNode importStaticStarNode : node.getStaticStarImports().values()) { pushASTNode(importStaticStarNode); visitAnnotations(importStaticStarNode); importStaticStarNode.visit(this); popASTNode(); } } }
Example #7
Source File: BaseScriptASTTransformation.java From groovy with Apache License 2.0 | 5 votes |
public void visit(ASTNode[] nodes, SourceUnit source) { init(nodes, source); AnnotatedNode parent = (AnnotatedNode) nodes[1]; AnnotationNode node = (AnnotationNode) nodes[0]; if (!MY_TYPE.equals(node.getClassNode())) return; if (parent instanceof DeclarationExpression) { changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node); } else if (parent instanceof ImportNode || parent instanceof PackageNode) { changeBaseScriptTypeFromPackageOrImport(source, parent, node); } else if (parent instanceof ClassNode) { changeBaseScriptTypeFromClass((ClassNode) parent, node); } }
Example #8
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 5 votes |
private ConstructedOuterNestedClassNode tryToConstructOuterNestedClassNodeViaStaticImport(final CompileUnit compileUnit, final ImportNode importNode, final String typeName, final BiConsumer<ConstructedOuterNestedClassNode, ClassNode> setRedirectListener) { String importClassName = importNode.getClassName(); ClassNode outerClassNode = compileUnit.getClass(importClassName); if (null == outerClassNode) return null; String outerNestedClassName = importClassName + "$" + typeName.replace('.', '$'); ConstructedOuterNestedClassNode constructedOuterNestedClassNode = new ConstructedOuterNestedClassNode(outerClassNode, outerNestedClassName); constructedOuterNestedClassNode.addSetRedirectListener(setRedirectListener); return constructedOuterNestedClassNode; }
Example #9
Source File: StaticImportVisitor.java From groovy with Apache License 2.0 | 5 votes |
private Expression findStaticProperty(Map<String, ImportNode> importNodes, String accessorName) { Expression result = null; ImportNode importNode = importNodes.get(accessorName); ClassNode importClass = importNode.getType(); String importMember = importNode.getFieldName(); result = findStaticPropertyAccessorByFullName(importClass, importMember); if (result == null) { result = findStaticPropertyAccessor(importClass, getPropNameForAccessor(importMember)); } return result; }
Example #10
Source File: FindUsagesPainter.java From netbeans with Apache License 2.0 | 5 votes |
/** * For the given {@link Line} and {@link ASTNode} returns colored text. In the * returned value there is the same text, but for example keywords are colored, * usage itself is bold, etc. * * @param node concrete usage node * @param line line where we have found the usage * @return colored text */ public static String colorASTNode(final ASTNode node, final Line line) { final int columnStart = node.getColumnNumber(); final int columnEnd = node.getLastColumnNumber(); if (node instanceof ClassNode) { return colorLine(line, ((ClassNode) node).getNameWithoutPackage()); } else if (node instanceof ConstructorNode) { return colorLine(line, ((ConstructorNode) node).getDeclaringClass().getNameWithoutPackage()); } else if (node instanceof ConstructorCallExpression) { return colorLine(line, ((ConstructorCallExpression) node).getType().getNameWithoutPackage()); } else if (node instanceof MethodNode) { return colorLine(line, ((MethodNode) node).getName()); } else if (node instanceof FieldNode) { return colorLine(line, ((FieldNode) node).getName()); } else if (node instanceof FakeASTNode) { // I know this isn't the nicest way, but I can't pass ImportNode directly and // don't see easier way how to find out if the FakeASTNode is based on ImportNode ASTNode originalNode = ((FakeASTNode) node).getOriginalNode(); if (originalNode instanceof ImportNode) { return colorLine(line, ((ImportNode) originalNode).getAlias()); } } final String beforePart = line.createPart(0, columnStart - 1).getText(); final String usagePart = line.createPart(columnStart - 1, columnEnd - columnStart).getText(); final String afterPart = line.createPart(columnEnd - 1, line.getText().length()).getText(); return buildHTML(beforePart, usagePart, afterPart); }
Example #11
Source File: VariableScopeVisitor.java From netbeans with Apache License 2.0 | 5 votes |
private void addAliasOccurrences(ModuleNode moduleNode, String findingName) { for (Map.Entry<String, ImportNode> entry : moduleNode.getStaticImports().entrySet()) { if (findingName.equals(entry.getKey())) { occurrences.add(new FakeASTNode(entry.getValue().getType(), entry.getKey())); } } }
Example #12
Source File: VariableScopeVisitor.java From netbeans with Apache License 2.0 | 5 votes |
@Override public void visitImports(ModuleNode node) { if (FindTypeUtils.isCaretOnClassNode(path, doc, cursorOffset)) { for (ImportNode importNode : node.getImports()) { addOccurrences(importNode.getType(), (ClassNode) FindTypeUtils.findCurrentNode(path, doc, cursorOffset)); } } else { // #233954 if (leaf instanceof ConstantExpression && leafParent instanceof MethodCallExpression) { addAliasOccurrences(node, ((MethodCallExpression) leafParent).getMethodAsString()); } // #234000 if (leaf instanceof VariableExpression) { addAliasOccurrences(node, ((VariableExpression) leaf).getName()); } // For both: #233954 and #234000 if (leaf instanceof BlockStatement) { for (Map.Entry<String, ImportNode> entry : node.getStaticImports().entrySet()) { OffsetRange range = ASTUtils.getNextIdentifierByName(doc, entry.getKey(), cursorOffset); if (range.containsInclusive(cursorOffset)) { occurrences.add(new FakeASTNode(entry.getValue().getType(), entry.getKey())); } } } } super.visitImports(node); }
Example #13
Source File: VariableScopeVisitor.java From netbeans with Apache License 2.0 | 5 votes |
@Override public void visitMethodCallExpression(MethodCallExpression methodCall) { if (!FindTypeUtils.isCaretOnClassNode(path, doc, cursorOffset)) { if (leaf instanceof MethodNode) { MethodNode method = (MethodNode) leaf; if (Methods.isSameMethod(method, methodCall)) { occurrences.add(methodCall); } } else if (leaf instanceof ConstantExpression && leafParent instanceof MethodCallExpression) { if (Methods.isSameMethod(methodCall, (MethodCallExpression) leafParent)) { occurrences.add(methodCall); } } // #234000 if (leaf instanceof BlockStatement) { ASTNode root = path.root(); if (root instanceof ModuleNode) { for (Map.Entry<String, ImportNode> entry : ((ModuleNode) root).getStaticImports().entrySet()) { String alias = entry.getKey(); if (!alias.equals(methodCall.getMethodAsString())) { continue; } OffsetRange range = ASTUtils.getNextIdentifierByName(doc, alias, cursorOffset); if (range.containsInclusive(cursorOffset)) { occurrences.add(methodCall); } } } } } super.visitMethodCallExpression(methodCall); }
Example #14
Source File: GroovyCompletionResult.java From netbeans with Apache License 2.0 | 5 votes |
@Override public void visitImports(ModuleNode node) { for (ImportNode importNode : node.getImports()) { imports.add(importNode.getType().getNameWithoutPackage()); } super.visitImports(node); }
Example #15
Source File: FindTypeUtils.java From netbeans with Apache License 2.0 | 5 votes |
private static ClassNode getCurrentImportNode(ModuleNode moduleNode, BaseDocument doc, int caret) { for (ImportNode importNode : moduleNode.getImports()) { if (isCaretOnImportStatement(importNode, doc, caret)) { if (!importNode.isStar()) { return ElementUtils.getType(importNode); } } } return null; }
Example #16
Source File: RemoveUnusedImportHint.java From netbeans with Apache License 2.0 | 5 votes |
@Override public void computeHints(GroovyHintsProvider.GroovyRuleContext context, List<Hint> result) { final ModuleNode moduleNode = context.getGroovyParserResult().getRootElement().getModuleNode(); if (null == moduleNode) { return; } List<ImportNode> importNodes = moduleNode.getImports(); for (ImportNode importNode : importNodes) { String alias = importNode.getAlias(); try { int find = 0; final FinderFactory.StringFwdFinder stringFwdFinder = new FinderFactory.StringFwdFinder(alias, true); while(-1 != (find = context.doc.find(stringFwdFinder, find+1, -1)) && skipUsage(find, context.doc)); if (-1 == find) { result.add(new Hint(this, "Unused Import", NbEditorUtilities.getFileObject(context.doc), ASTUtils.getRangeFull(importNode, context.doc), Collections.<HintFix>singletonList(new RemoveUnusedImportFix("Remove unused import", context.doc, importNode)), 1)); } } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } }
Example #17
Source File: VariableScopeVisitor.java From netbeans with Apache License 2.0 | 4 votes |
@Override public void visitVariableExpression(VariableExpression variableExpression) { final ClassNode visitedType = variableExpression.getType(); final String visitedName = variableExpression.getName(); if (FindTypeUtils.isCaretOnClassNode(path, doc, cursorOffset)) { addOccurrences(visitedType, (ClassNode) FindTypeUtils.findCurrentNode(path, doc, cursorOffset)); } else { if (leaf instanceof FieldNode) { if (visitedName.equals(((FieldNode) leaf).getName())) { occurrences.add(variableExpression); } } else if (leaf instanceof PropertyNode) { if (visitedName.equals(((PropertyNode) leaf).getField().getName())) { occurrences.add(variableExpression); } } else if (leaf instanceof Variable) { if (visitedName.equals(((Variable) leaf).getName())) { occurrences.add(variableExpression); } } else if (leaf instanceof ForStatement) { if (visitedName.equals(((ForStatement) leaf).getVariable().getName())) { occurrences.add(variableExpression); } } else if (leaf instanceof ConstantExpression && leafParent instanceof PropertyExpression) { PropertyExpression property = (PropertyExpression) leafParent; if (variableExpression.getName().equals(property.getPropertyAsString())) { occurrences.add(variableExpression); } // #234000 } else if (leaf instanceof BlockStatement) { ASTNode root = path.root(); if (root instanceof ModuleNode) { for (Map.Entry<String, ImportNode> entry : ((ModuleNode) root).getStaticImports().entrySet()) { String alias = entry.getKey(); if (!alias.equals(variableExpression.getName())) { continue; } OffsetRange range = ASTUtils.getNextIdentifierByName(doc, alias, cursorOffset); if (range.containsInclusive(cursorOffset)) { occurrences.add(variableExpression); } } } } } super.visitVariableExpression(variableExpression); }
Example #18
Source File: ElementUtils.java From netbeans with Apache License 2.0 | 4 votes |
public static ClassNode getDeclaringClass(ASTNode node) { if (node instanceof ClassNode) { return (ClassNode) node; } else if (node instanceof AnnotationNode) { return ((AnnotationNode) node).getClassNode(); } else if (node instanceof MethodNode) { return ((MethodNode) node).getDeclaringClass(); } else if (node instanceof FieldNode) { return ((FieldNode) node).getDeclaringClass(); } else if (node instanceof PropertyNode) { return ((PropertyNode) node).getDeclaringClass(); } else if (node instanceof Parameter) { return ((Parameter) node).getDeclaringClass(); } else if (node instanceof ForStatement) { return ((ForStatement) node).getVariableType().getDeclaringClass(); } else if (node instanceof CatchStatement) { return ((CatchStatement) node).getVariable().getDeclaringClass(); } else if (node instanceof ImportNode) { return ((ImportNode) node).getDeclaringClass(); } else if (node instanceof ClassExpression) { return ((ClassExpression) node).getType().getDeclaringClass(); } else if (node instanceof VariableExpression) { return ((VariableExpression) node).getDeclaringClass(); } else if (node instanceof DeclarationExpression) { DeclarationExpression declaration = ((DeclarationExpression) node); if (declaration.isMultipleAssignmentDeclaration()) { return declaration.getTupleExpression().getDeclaringClass(); } else { return declaration.getVariableExpression().getDeclaringClass(); } } else if (node instanceof ConstantExpression) { return ((ConstantExpression) node).getDeclaringClass(); } else if (node instanceof MethodCallExpression) { return ((MethodCallExpression) node).getType(); } else if (node instanceof ConstructorCallExpression) { return ((ConstructorCallExpression) node).getType(); } else if (node instanceof ArrayExpression) { return ((ArrayExpression) node).getDeclaringClass(); } throw new IllegalStateException("Not implemented yet - GroovyRefactoringElement.getDeclaringClass() ..looks like the type: " + node.getClass().getName() + " isn't handled at the moment!"); // NOI18N }
Example #19
Source File: ElementUtils.java From netbeans with Apache License 2.0 | 4 votes |
public static String getNameWithoutPackage(ASTNode node) { if (node instanceof FakeASTNode) { node = ((FakeASTNode) node).getOriginalNode(); } String name = null; if (node instanceof ClassNode) { name = ((ClassNode) node).getNameWithoutPackage(); } else if (node instanceof AnnotationNode) { return ((AnnotationNode) node).getText(); } else if (node instanceof MethodNode) { name = ((MethodNode) node).getName(); if ("<init>".equals(name)) { // NOI18N name = getDeclaringClassNameWithoutPackage(node); } } else if (node instanceof FieldNode) { name = ((FieldNode) node).getName(); } else if (node instanceof PropertyNode) { name = ((PropertyNode) node).getName(); } else if (node instanceof Parameter) { name = ((Parameter) node).getName(); } else if (node instanceof ForStatement) { name = ((ForStatement) node).getVariableType().getNameWithoutPackage(); } else if (node instanceof CatchStatement) { name = ((CatchStatement) node).getVariable().getName(); } else if (node instanceof ImportNode) { name = ((ImportNode) node).getType().getNameWithoutPackage(); } else if (node instanceof ClassExpression) { name = ((ClassExpression) node).getType().getNameWithoutPackage(); } else if (node instanceof VariableExpression) { name = ((VariableExpression) node).getName(); } else if (node instanceof DeclarationExpression) { DeclarationExpression declaration = ((DeclarationExpression) node); if (declaration.isMultipleAssignmentDeclaration()) { name = declaration.getTupleExpression().getType().getNameWithoutPackage(); } else { name = declaration.getVariableExpression().getType().getNameWithoutPackage(); } } else if (node instanceof ConstantExpression) { name = ((ConstantExpression) node).getText(); } else if (node instanceof MethodCallExpression) { name = ((MethodCallExpression) node).getMethodAsString(); } else if (node instanceof ConstructorCallExpression) { name = ((ConstructorCallExpression) node).getType().getNameWithoutPackage(); } else if (node instanceof ArrayExpression) { name = ((ArrayExpression) node).getElementType().getNameWithoutPackage(); } if (name != null) { return normalizeTypeName(name, null); } throw new IllegalStateException("Not implemented yet - GroovyRefactoringElement.getName() needs to be improve for type: " + node.getClass().getSimpleName()); // NOI18N }
Example #20
Source File: ElementUtils.java From netbeans with Apache License 2.0 | 4 votes |
/** * Returns type for the given ASTNode. For example if FieldNode is passed * as a parameter, it returns type of the given field etc. If the Method call * is passed as a parameter, the method tried to interfere proper type and return it * * @param node where we want to know declared type * @return type of the given node * @throws IllegalStateException if an implementation is missing for the given ASTNode type */ public static ClassNode getType(ASTNode node) { if (node instanceof FakeASTNode) { node = ((FakeASTNode) node).getOriginalNode(); } if (node instanceof ClassNode) { ClassNode clazz = ((ClassNode) node); if (clazz.getComponentType() != null) { return clazz.getComponentType(); } else { return clazz; } } else if (node instanceof AnnotationNode) { return ((AnnotationNode) node).getClassNode(); } else if (node instanceof FieldNode) { return ((FieldNode) node).getType(); } else if (node instanceof PropertyNode) { return ((PropertyNode) node).getType(); } else if (node instanceof MethodNode) { return ((MethodNode) node).getReturnType(); } else if (node instanceof Parameter) { return ((Parameter) node).getType(); } else if (node instanceof ForStatement) { return ((ForStatement) node).getVariableType(); } else if (node instanceof CatchStatement) { return ((CatchStatement) node).getVariable().getOriginType(); } else if (node instanceof ImportNode) { return ((ImportNode) node).getType(); } else if (node instanceof ClassExpression) { return ((ClassExpression) node).getType(); } else if (node instanceof VariableExpression) { return ((VariableExpression) node).getType(); } else if (node instanceof DeclarationExpression) { DeclarationExpression declaration = ((DeclarationExpression) node); if (declaration.isMultipleAssignmentDeclaration()) { return declaration.getTupleExpression().getType(); } else { return declaration.getVariableExpression().getType(); } } else if (node instanceof ConstructorCallExpression) { return ((ConstructorCallExpression) node).getType(); } else if (node instanceof ArrayExpression) { return ((ArrayExpression) node).getElementType(); } throw new IllegalStateException("Not implemented yet - GroovyRefactoringElement.getType() needs to be improve!"); // NOI18N }
Example #21
Source File: FindTypeUtils.java From netbeans with Apache License 2.0 | 4 votes |
private static OffsetRange getImportRange(ImportNode importNode, BaseDocument doc, int cursorOffset) { return getRange(importNode.getType(), doc, cursorOffset); }
Example #22
Source File: FindTypeUtils.java From netbeans with Apache License 2.0 | 4 votes |
private static boolean isCaretOnImportStatement(ImportNode importNode, BaseDocument doc, int cursorOffset) { if (getImportRange(importNode, doc, cursorOffset) != OffsetRange.NONE) { return true; } return false; }
Example #23
Source File: RemoveUnusedImportHint.java From netbeans with Apache License 2.0 | 4 votes |
public RemoveUnusedImportFix(String desc, BaseDocument baseDoc, ImportNode importNode) { this.desc = desc; this.baseDoc = baseDoc; this.importNode = importNode; }
Example #24
Source File: GroovyASTUtils.java From groovy-language-server with Apache License 2.0 | 4 votes |
public static ASTNode getDefinition(ASTNode node, boolean strict, ASTNodeVisitor astVisitor) { if (node == null) { return null; } ASTNode parentNode = astVisitor.getParent(node); if (node instanceof ExpressionStatement) { ExpressionStatement statement = (ExpressionStatement) node; node = statement.getExpression(); } if (node instanceof ClassNode) { return tryToResolveOriginalClassNode((ClassNode) node, strict, astVisitor); } else if (node instanceof ConstructorCallExpression) { ConstructorCallExpression callExpression = (ConstructorCallExpression) node; return GroovyASTUtils.getMethodFromCallExpression(callExpression, astVisitor); } else if (node instanceof DeclarationExpression) { DeclarationExpression declExpression = (DeclarationExpression) node; if (!declExpression.isMultipleAssignmentDeclaration()) { ClassNode originType = declExpression.getVariableExpression().getOriginType(); return tryToResolveOriginalClassNode(originType, strict, astVisitor); } } else if (node instanceof ClassExpression) { ClassExpression classExpression = (ClassExpression) node; return tryToResolveOriginalClassNode(classExpression.getType(), strict, astVisitor); } else if (node instanceof ImportNode) { ImportNode importNode = (ImportNode) node; return tryToResolveOriginalClassNode(importNode.getType(), strict, astVisitor); } else if (node instanceof MethodNode) { return node; } else if (node instanceof ConstantExpression && parentNode != null) { if (parentNode instanceof MethodCallExpression) { MethodCallExpression methodCallExpression = (MethodCallExpression) parentNode; return GroovyASTUtils.getMethodFromCallExpression(methodCallExpression, astVisitor); } else if (parentNode instanceof PropertyExpression) { PropertyExpression propertyExpression = (PropertyExpression) parentNode; PropertyNode propNode = GroovyASTUtils.getPropertyFromExpression(propertyExpression, astVisitor); if (propNode != null) { return propNode; } return GroovyASTUtils.getFieldFromExpression(propertyExpression, astVisitor); } } else if (node instanceof VariableExpression) { VariableExpression variableExpression = (VariableExpression) node; Variable accessedVariable = variableExpression.getAccessedVariable(); if (accessedVariable instanceof ASTNode) { return (ASTNode) accessedVariable; } // DynamicVariable is not an ASTNode, so skip it return null; } else if (node instanceof Variable) { return node; } return null; }
Example #25
Source File: JavaStubGenerator.java From groovy with Apache License 2.0 | 4 votes |
private String getAnnotationValue(Object memberValue) { String val = "null"; if (memberValue instanceof ListExpression) { StringBuilder sb = new StringBuilder("{"); boolean first = true; ListExpression le = (ListExpression) memberValue; for (Expression e : le.getExpressions()) { if (first) first = false; else sb.append(","); sb.append(getAnnotationValue(e)); } sb.append("}"); val = sb.toString(); } else if (memberValue instanceof ConstantExpression) { ConstantExpression ce = (ConstantExpression) memberValue; Object constValue = ce.getValue(); if (constValue instanceof AnnotationNode) { Writer writer = new StringBuilderWriter(); PrintWriter out = new PrintWriter(writer); printAnnotation(out, (AnnotationNode) constValue); val = writer.toString(); } else if (constValue instanceof Number || constValue instanceof Boolean) val = constValue.toString(); else val = "\"" + escapeSpecialChars(constValue.toString()) + "\""; } else if (memberValue instanceof PropertyExpression) { // assume must be static class field or enum value or class that Java can resolve val = ((Expression) memberValue).getText(); } else if (memberValue instanceof VariableExpression) { val = ((Expression) memberValue).getText(); //check for an alias ImportNode alias = currentModule.getStaticImports().get(val); if (alias != null) val = alias.getClassName() + "." + alias.getFieldName(); } else if (memberValue instanceof ClosureExpression) { // annotation closure; replaced with this specific class literal to cover the // case where annotation type uses Class<? extends Closure> for the closure's type val = "groovy.lang.Closure.class"; } else if (memberValue instanceof ClassExpression) { val = ((Expression) memberValue).getText() + ".class"; } return val; }
Example #26
Source File: CompletionProvider.java From groovy-language-server with Apache License 2.0 | 4 votes |
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> provideCompletion( TextDocumentIdentifier textDocument, Position position, CompletionContext context) { if (ast == null) { //this shouldn't happen, but let's avoid an exception if something //goes terribly wrong. return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList())); } URI uri = URI.create(textDocument.getUri()); ASTNode offsetNode = ast.getNodeAtLineAndColumn(uri, position.getLine(), position.getCharacter()); if (offsetNode == null) { return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList())); } ASTNode parentNode = ast.getParent(offsetNode); isIncomplete = false; List<CompletionItem> items = new ArrayList<>(); if (offsetNode instanceof PropertyExpression) { populateItemsFromPropertyExpression((PropertyExpression) offsetNode, position, items); } else if (parentNode instanceof PropertyExpression) { populateItemsFromPropertyExpression((PropertyExpression) parentNode, position, items); } else if (offsetNode instanceof MethodCallExpression) { populateItemsFromMethodCallExpression((MethodCallExpression) offsetNode, position, items); } else if (offsetNode instanceof ConstructorCallExpression) { populateItemsFromConstructorCallExpression((ConstructorCallExpression) offsetNode, position, items); } else if (parentNode instanceof MethodCallExpression) { populateItemsFromMethodCallExpression((MethodCallExpression) parentNode, position, items); } else if (offsetNode instanceof VariableExpression) { populateItemsFromVariableExpression((VariableExpression) offsetNode, position, items); } else if (offsetNode instanceof ImportNode) { populateItemsFromImportNode((ImportNode) offsetNode, position, items); } else if (offsetNode instanceof ClassNode) { populateItemsFromClassNode((ClassNode) offsetNode, position, items); } else if (offsetNode instanceof MethodNode) { populateItemsFromScope(offsetNode, "", items); } else if (offsetNode instanceof Statement) { populateItemsFromScope(offsetNode, "", items); } if (isIncomplete) { return CompletableFuture.completedFuture(Either.forRight(new CompletionList(true, items))); } return CompletableFuture.completedFuture(Either.forLeft(items)); }