@Override public void visitStaticMethodCallExpression(StaticMethodCallExpression expression) { List<MethodNode> possibleMethods = expression.getOwnerType().getMethods(expression.getMethodAsString()); if (!possibleMethods.isEmpty() && expression.getArguments() instanceof ArgumentListExpression) { ArgumentListExpression actualArguments = (ArgumentListExpression) expression.getArguments(); possibleMethods.stream() .filter(method -> areEquals(method.getParameters(), actualArguments)) .forEach(method -> indexer.addReference(createLocation(method), createLocation(expression))); } super.visitStaticMethodCallExpression(expression); }
@Override public void visitVariableExpression(VariableExpression expression) { if (expression.getAccessedVariable() != null) { SymbolInformation symbol = getVariableSymbolInformation(expression.getAccessedVariable()); if (Ranges.isValid(symbol.getLocation().getRange())) { indexer.addReference(symbol.getLocation(), createLocation(expression)); } else if (classFields.containsKey(expression.getAccessedVariable().getName())) { Location location = createLocation(classFields.get(expression.getAccessedVariable().getName())); indexer.addReference(location, createLocation(expression)); } } super.visitVariableExpression(expression); }
private SymbolInformation getVariableSymbolInformation(Variable variable) { final String containerName; final SymbolKind kind; final Location location; if (methodNode.isPresent()) { containerName = methodNode.get().getName(); } else { containerName = clazz.getName(); } if (variable instanceof DynamicVariable) { kind = SymbolKind.Field; location = GroovyLocations.createLocation(workspaceUriSupplier.get(uri)); } else if (variable instanceof FieldNode) { kind = SymbolKind.Field; location = createLocation(uri, (FieldNode) variable); } else if (variable instanceof Parameter) { kind = SymbolKind.Variable; location = createLocation(uri, (Parameter) variable); } else if (variable instanceof PropertyNode) { kind = SymbolKind.Field; location = createLocation(uri, (PropertyNode) variable); } else if (variable instanceof VariableExpression) { kind = SymbolKind.Variable; location = createLocation(uri, (VariableExpression) variable); } else { throw new IllegalArgumentException(String.format("Unknown type of variable: %s", variable)); } return new SymbolInformation(variable.getName(), kind, location, containerName); }
private void parseMethod(Indexer newIndexer, URI sourceUri, ClassNode parent, Map<String, Location> classes, Map<String, FieldNode> classFields, MethodNode method) { SymbolInformation methodSymbol = createSymbolInformation(method.getName(), SymbolKind.Method, GroovyLocations.createLocation(sourceUri, method), Optional.of(parent.getName())); newIndexer.addSymbol(sourceUri, methodSymbol); // Method parameters method.getVariableScope().getDeclaredVariables().values().forEach(variable -> { SymbolInformation variableSymbol = getVariableSymbolInformation(method.getName(), sourceUri, variable); newIndexer.addSymbol(sourceUri, variableSymbol); if (classes.containsKey(variable.getType().getName())) { newIndexer.addReference(classes.get(variable.getType().getName()), GroovyLocations.createLocation(sourceUri, variable.getType())); } }); // Return type if (classes.containsKey(method.getReturnType().getName())) { newIndexer.addReference(classes.get(method.getReturnType().getName()), GroovyLocations.createLocation(sourceUri, method.getReturnType())); } // We only want to visit the method if its not generated if (Ranges.isValid(methodSymbol.getLocation().getRange())) { // Visit the method if (method.getCode() instanceof BlockStatement) { BlockStatement blockStatement = (BlockStatement) method.getCode(); blockStatement.visit(new MethodVisitor(newIndexer, sourceUri, parent, classes, classFields, Optional.of(method), workspaceUriSupplier)); } } }
@Override public void visitClassExpression(ClassExpression expression) { // Add reference to class if (expression.getType() != null && classes.containsKey(expression.getType().getName())) { indexer.addReference(classes.get(expression.getType().getName()), createLocation(expression)); } super.visitClassExpression(expression); }
.visit(new MethodVisitor(newIndexer, sourceUri, sourceUnit.getAST().getScriptClassDummy(), classes, Maps.newHashMap(), Optional.absent(), workspaceUriSupplier));
ArgumentListExpression actualArguments = (ArgumentListExpression) call.getArguments(); possibleMethods.stream() .filter(method -> areEquals(method.getParameters(), actualArguments)) .forEach(method -> { indexer.addReference( createLocation(URI.create(classes.get(parentClass.getName()).getUri()), method), createLocation(call)); });
@Override public void visitDeclarationExpression(DeclarationExpression expression) { if (expression.getLeftExpression() instanceof Variable) { Variable var = (Variable) expression.getLeftExpression(); SymbolInformation symbol = getVariableSymbolInformation(var); indexer.addSymbol(uri, symbol); if (var.getType() != null && classes.containsKey(var.getType().getName())) { indexer.addReference(classes.get(var.getType().getName()), createLocation(var.getType())); } } super.visitDeclarationExpression(expression); }
@Override public void visitCatchStatement(CatchStatement statement) { // Add reference to class exception if (classes.containsKey(statement.getExceptionType().getName())) { indexer.addReference(classes.get(statement.getExceptionType().getName()), createLocation(statement.getExceptionType())); } // TODO(#125): add a symbol for the exception variables. Right now statement.getVariable() returns a Parameter // with an invalid location. super.visitCatchStatement(statement); }
@Override public void visitConstructorCallExpression(ConstructorCallExpression expression) { if (expression.getType() != null && classes.containsKey(expression.getType().getName())) { indexer.addReference(classes.get(expression.getType().getName()), createLocation(expression)); } super.visitConstructorCallExpression(expression); }
@Override public void visitPropertyExpression(PropertyExpression expression) { if (expression.getObjectExpression() instanceof VariableExpression) { // This means it's a non static reference to a class variable VariableExpression var = (VariableExpression) expression.getObjectExpression(); FieldNode field = var.getType().getField(expression.getProperty().getText()); if (classes.containsKey(var.getType().getName())) { indexer.addReference(createLocation(URI.create(classes.get(var.getType().getName()).getUri()), field), createLocation(expression.getProperty())); } } else if (expression.getObjectExpression() instanceof ClassExpression) { // This means it's a static reference to a class variable ClassExpression classExpression = (ClassExpression) expression.getObjectExpression(); FieldNode field = classExpression.getType().getField(expression.getProperty().getText()); if (classes.containsKey(classExpression.getType().getName())) { indexer.addReference( createLocation(URI.create(classes.get(classExpression.getType().getName()).getUri()), field), createLocation(expression.getProperty())); } } super.visitPropertyExpression(expression); }