Beispiel #1
0
  public Collection<IToken> getInterfaceForLocal(
      String activationToken, boolean addAttributeAccess, boolean addLocalsFromHasAttr) {
    Set<SourceToken> comps = new HashSet<SourceToken>();

    Iterator<SimpleNode> it = this.scope.topDownIterator();
    if (!it.hasNext()) {
      return new ArrayList<IToken>();
    }

    SimpleNode element = it.next();

    String dottedActTok = activationToken + '.';
    // ok, that's the scope we have to analyze
    SequencialASTIteratorVisitor visitor = SequencialASTIteratorVisitor.create(element);

    ArrayList<Class> classes = new ArrayList<Class>(2);
    if (addAttributeAccess) {
      classes.add(Attribute.class);
    }
    if (addLocalsFromHasAttr) {
      classes.add(Call.class);
    }
    Iterator<ASTEntry> iterator = visitor.getIterator(classes.toArray(new Class[classes.size()]));

    while (iterator.hasNext()) {
      ASTEntry entry = iterator.next();
      if (entry.node instanceof Attribute) {
        String rep = NodeUtils.getFullRepresentationString(entry.node);
        if (rep.startsWith(dottedActTok)) {
          rep = rep.substring(dottedActTok.length());
          if (NodeUtils.isValidNameRepresentation(
              rep)) { // that'd be something that can happen when trying to recreate the parsing
            comps.add(
                new SourceToken(
                    entry.node,
                    FullRepIterable.getFirstPart(rep),
                    "",
                    "",
                    "",
                    IToken.TYPE_OBJECT_FOUND_INTERFACE));
          }
        }
      } else if (entry.node instanceof Call) {
        Call call = (Call) entry.node;
        if ("hasattr".equals(NodeUtils.getFullRepresentationString(call.func))
            && call.args != null
            && call.args.length == 2) {
          String rep = NodeUtils.getFullRepresentationString(call.args[0]);
          if (rep.equals(activationToken)) {
            exprType node = call.args[1];
            if (node instanceof Str) {
              Str str = (Str) node;
              String attrName = str.s;
              if (NodeUtils.isValidNameRepresentation(attrName)) {
                comps.add(
                    new SourceToken(
                        node, attrName, "", "", "", IToken.TYPE_OBJECT_FOUND_INTERFACE));
              }
            }
          }
        }
      }
    }
    return new ArrayList<IToken>(comps);
  }