private void analyzePossibleConflicts_inLibrary(
     final RefactoringStatus result, Source unitSource, Source librarySource) {
   // prepare resolved unit
   CompilationUnit unit = null;
   try {
     unit = context.resolveCompilationUnit(unitSource, librarySource);
   } catch (AnalysisException e) {
   }
   if (unit == null) {
     return;
   }
   // check for conflicts in the unit
   final SourceRange elementRange = element.getVisibleRange();
   unit.accept(
       new RecursiveAstVisitor<Void>() {
         @Override
         public Void visitSimpleIdentifier(SimpleIdentifier node) {
           Element nameElement = node.getBestElement();
           if (nameElement != null && nameElement.getName().equals(newName)) {
             // duplicate declaration
             if (haveIntersectingRanges(element, nameElement)) {
               String message =
                   MessageFormat.format(
                       "Duplicate local {0} ''{1}''.", getElementKindName(nameElement), newName);
               result.addError(message, new RefactoringStatusContext(nameElement));
               return null;
             }
             // shadowing referenced element
             if (elementRange.contains(node.getOffset()) && !node.isQualified()) {
               nameElement = HierarchyUtils.getSyntheticAccessorVariable(nameElement);
               String nameElementSourceName = nameElement.getSource().getShortName();
               String message =
                   MessageFormat.format(
                       "Usage of {0} ''{1}'' declared in ''{2}'' will be shadowed by renamed {3}.",
                       getElementKindName(nameElement),
                       getElementQualifiedName(nameElement),
                       nameElementSourceName,
                       getElementKindName(element));
               result.addError(message, new RefactoringStatusContext(node));
             }
           }
           return null;
         }
       });
 }
 public void test_localVariable_types_invoked() throws Exception {
   Source source =
       addSource(
           createSource( //
               "const A = null;",
               "main() {",
               "  var myVar = (int p) => 'foo';",
               "  myVar(42);",
               "}"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnit unit = getAnalysisContext().getResolvedCompilationUnit(source, library);
   assertNotNull(unit);
   final boolean[] found = {false};
   final AnalysisException[] thrownException = new AnalysisException[1];
   unit.accept(
       new RecursiveAstVisitor<Void>() {
         @Override
         public Void visitSimpleIdentifier(SimpleIdentifier node) {
           if (node.getName().equals("myVar") && node.getParent() instanceof MethodInvocation) {
             try {
               found[0] = true;
               // check static type
               Type staticType = node.getStaticType();
               assertSame(getTypeProvider().getDynamicType(), staticType);
               // check propagated type
               FunctionType propagatedType = (FunctionType) node.getPropagatedType();
               assertEquals(getTypeProvider().getStringType(), propagatedType.getReturnType());
             } catch (AnalysisException e) {
               thrownException[0] = e;
             }
           }
           return null;
         }
       });
   if (thrownException[0] != null) {
     throw new AnalysisException("Exception", thrownException[0]);
   }
   assertTrue(found[0]);
 }
Esempio n. 3
0
  @Override
  public void process(final Context context, final CompilationUnit unit) {
    NodeList<CompilationUnitMember> declarations = unit.getDeclarations();
    // remove NodeList, it is declared in enginelib.dart
    for (Iterator<CompilationUnitMember> iter = declarations.iterator(); iter.hasNext(); ) {
      CompilationUnitMember member = iter.next();
      if (member instanceof ClassDeclaration) {
        ClassDeclaration classDeclaration = (ClassDeclaration) member;
        String name = classDeclaration.getName().getName();
        if (name.equals("NodeList")
            || name.equals("NodeLocator")
            || name.equals("NodeFoundException")) {
          iter.remove();
        }
      }
    }
    // process nodes
    unit.accept(
        new GeneralizingASTVisitor<Void>() {
          @Override
          public Void visitMethodDeclaration(MethodDeclaration node) {
            String name = node.getName().getName();
            if ("accept".equals(name) && node.getParameters().getParameters().size() == 1) {
              node.setReturnType(null);
              FormalParameter formalParameter = node.getParameters().getParameters().get(0);
              ((SimpleFormalParameter) formalParameter).getType().setTypeArguments(null);
            }
            return super.visitMethodDeclaration(node);
          }

          @Override
          public Void visitMethodInvocation(MethodInvocation node) {
            if (isMethodInClass(
                node, "toArray", "com.google.dart.engine.utilities.collection.IntList")) {
              replaceNode(node, node.getTarget());
              return null;
            }
            return super.visitMethodInvocation(node);
          }

          @Override
          public Void visitTypeName(TypeName node) {
            if (node.getName() instanceof SimpleIdentifier) {
              SimpleIdentifier nameNode = (SimpleIdentifier) node.getName();
              String name = nameNode.getName();
              if ("IntList".equals(name)) {
                replaceNode(node, typeName("List", typeName("int")));
                return null;
              }
            }
            return super.visitTypeName(node);
          }

          private boolean isMethodInClass(
              MethodInvocation node, String reqName, String reqClassName) {
            String name = node.getMethodName().getName();
            return Objects.equal(name, reqName)
                && JavaUtils.isMethodInClass(context.getNodeBinding(node), reqClassName);
          }
        });
  }
  public static Element getElementEnclosingOffset(CompilationUnit unit, final int offset) {
    final Element result[] = new Element[] {null};
    unit.accept(
        new GeneralizingAstVisitor<Void>() {
          @Override
          public Void visitClassDeclaration(ClassDeclaration node) {
            if (isNodeEnclosingOffset(node)) {
              result[0] = node.getElement();
              return super.visitClassDeclaration(node);
            }
            return null;
          }

          @Override
          public Void visitConstructorDeclaration(ConstructorDeclaration node) {
            if (isNodeEnclosingOffset(node)) {
              result[0] = node.getElement();
              return super.visitConstructorDeclaration(node);
            }
            return null;
          }

          @Override
          public Void visitFieldDeclaration(FieldDeclaration node) {
            if (isNodeEnclosingOffset(node)) {
              NodeList<VariableDeclaration> variables = node.getFields().getVariables();
              if (!variables.isEmpty()) {
                result[0] = variables.get(0).getElement();
              }
              return super.visitFieldDeclaration(node);
            }
            return null;
          }

          @Override
          public Void visitFunctionDeclaration(FunctionDeclaration node) {
            if (isNodeEnclosingOffset(node)) {
              result[0] = node.getElement();
              return super.visitFunctionDeclaration(node);
            }
            return null;
          }

          @Override
          public Void visitMethodDeclaration(MethodDeclaration node) {
            if (isNodeEnclosingOffset(node)) {
              result[0] = node.getElement();
              return super.visitMethodDeclaration(node);
            }
            return null;
          }

          @Override
          public Void visitNode(AstNode node) {
            if (isNodeEnclosingOffset(node)) {
              super.visitNode(node);
            }
            return null;
          }

          private boolean isNodeEnclosingOffset(AstNode node) {
            return node.getOffset() <= offset && offset <= node.getEnd();
          }
        });
    return result[0];
  }