public static void loadPermissions(URL url) throws IOException, PermissionParseException {

    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String line;
    Pattern ignore = Pattern.compile("^\\s*(//.*)?$");
    Pattern valid =
        Pattern.compile("^\\s*permission\\s+(\\S+)" + "(\\s+\"([^\"]*)\"(,\\s+\"([^\"]*)\")?)?;$");

    Set<Permission> perms = new HashSet<Permission>();

    while ((line = in.readLine()) != null) {
      if (ignore.matcher(line).matches()) {
        continue;
      }

      Matcher matcher = valid.matcher(line);
      if (!matcher.matches()) {
        throw new PermissionParseException("invalid syntax: " + line);
      }

      int nGroups = matcher.groupCount();
      String type = matcher.group(1);
      String name = expand(nGroups >= 3 ? matcher.group(3) : null);
      String actions = expand(nGroups >= 5 ? matcher.group(5) : null);

      try {
        Permission perm = getPermission(type, name, actions);
        perms.add(perm);
      } catch (Throwable e) {
        String message =
            String.format(
                "could not instantiate permission: " + "type=%s name=%s actions=",
                type, name, actions);
        throw new PermissionParseException(message, e);
      }
    }

    in.close();

    permSet.addAll(perms);
  }
Beispiel #2
0
  /*
     public String transformedName() {
  if (outer != null) {
      String ps = outer.toString();
      if (ps.equals("")) {
  	return name;
      } else {
  	if (outer.isClass()) {
  	    return ps+"$"+name;
  	} else {
  	    return ps+"."+name;
  	}
      }
  }
  return "";
     }
     */
  public Set /*<PCNode>*/ matchScope(
      Pattern simple_name_pattern, Set /*<PCNode>*/ classes, Set /*<PCNode>*/ packages) {
    Set this_scope = matchClass(simple_name_pattern);
    Set this_scope_names = new HashSet();
    Iterator tsi = this_scope.iterator();
    while (tsi.hasNext()) {
      PCNode pc = (PCNode) tsi.next();
      // Add name to shadow outer classes
      this_scope_names.add(pc.name);
    }
    if (is_class) {
      // Match inner classes of outer classes or same package
      Set outer_scope = outer.matchScope(simple_name_pattern, classes, packages);
      Iterator osi = outer_scope.iterator();
      while (osi.hasNext()) {
        PCNode pc = (PCNode) osi.next();
        if (!this_scope_names.contains(pc.name)) {
          this_scope.add(pc);
          // Nothing to shadow here
        }
      }
    } else {
      // Match specifically imported classes
      Iterator ci = classes.iterator();
      while (ci.hasNext()) {
        PCNode c = (PCNode) ci.next();
        if (!this_scope_names.contains(c.name)) {
          // Add name to list to shadow nonspecifically imported classes
          this_scope_names.add(c.name);
          // If it matches the pattern, add it to the list of matches
          if (simple_name_pattern.matcher(c.name).matches()) {
            this_scope.add(c);
          }
        }
      }
      // Match nonspecifically imported classes
      Set /*<String>*/ new_names = new HashSet();
      Iterator pi = packages.iterator();
      while (pi.hasNext()) {
        PCNode p = (PCNode) pi.next();
        Set /*<PCNode>*/ p_matches = p.matchSpecific(simple_name_pattern);
        Iterator pci = p_matches.iterator();
        while (pci.hasNext()) {
          PCNode pc = (PCNode) pci.next();
          if (!this_scope_names.contains(pc.name)) {
            // Nonspecifically imported classes do not shadow each other,
            // but they may shadow toplevel packages
            new_names.add(pc.name);
            // If it matches the pattern, add it to the list of matches
            if (simple_name_pattern.matcher(pc.name).matches()) {
              this_scope.add(pc);
            }
          }
        }
      }
      this_scope_names.addAll(new_names);

      // Finally, match toplevel classes and packages
      Iterator tli = root.root.matchSpecific(simple_name_pattern).iterator();
      while (tli.hasNext()) {
        PCNode tl = (PCNode) tli.next();
        if (!this_scope_names.contains(tl.name)) {
          // If it matches the pattern, add it to the list of matches
          if (simple_name_pattern.matcher(tl.name).matches()) {
            this_scope.add(tl);
          }
        }
      }
    }
    if (abc.main.Debug.v().namePatternProcessing)
      System.out.println(this + ".matchScope " + simple_name_pattern.pattern() + ": " + this_scope);
    return this_scope;
  }
  private void discoverAndRunProcs(
      Context context,
      Set<TypeElement> annotationsPresent,
      List<ClassSymbol> topLevelClasses,
      List<PackageSymbol> packageInfoFiles) {
    Map<String, TypeElement> unmatchedAnnotations =
        new HashMap<String, TypeElement>(annotationsPresent.size());

    for (TypeElement a : annotationsPresent) {
      unmatchedAnnotations.put(a.getQualifiedName().toString(), a);
    }

    // Give "*" processors a chance to match
    if (unmatchedAnnotations.size() == 0) unmatchedAnnotations.put("", null);

    DiscoveredProcessors.ProcessorStateIterator psi = discoveredProcs.iterator();
    // TODO: Create proper argument values; need past round
    // information to fill in this constructor.  Note that the 1
    // st round of processing could be the last round if there
    // were parse errors on the initial source files; however, we
    // are not doing processing in that case.

    Set<Element> rootElements = new LinkedHashSet<Element>();
    rootElements.addAll(topLevelClasses);
    rootElements.addAll(packageInfoFiles);
    rootElements = Collections.unmodifiableSet(rootElements);

    RoundEnvironment renv =
        new JavacRoundEnvironment(false, false, rootElements, JavacProcessingEnvironment.this);

    while (unmatchedAnnotations.size() > 0 && psi.hasNext()) {
      ProcessorState ps = psi.next();
      Set<String> matchedNames = new HashSet<String>();
      Set<TypeElement> typeElements = new LinkedHashSet<TypeElement>();

      for (Map.Entry<String, TypeElement> entry : unmatchedAnnotations.entrySet()) {
        String unmatchedAnnotationName = entry.getKey();
        if (ps.annotationSupported(unmatchedAnnotationName)) {
          matchedNames.add(unmatchedAnnotationName);
          TypeElement te = entry.getValue();
          if (te != null) typeElements.add(te);
        }
      }

      if (matchedNames.size() > 0 || ps.contributed) {
        boolean processingResult = callProcessor(ps.processor, typeElements, renv);
        ps.contributed = true;
        ps.removeSupportedOptions(unmatchedProcessorOptions);

        if (printProcessorInfo || verbose) {
          log.printNoteLines(
              "x.print.processor.info",
              ps.processor.getClass().getName(),
              matchedNames.toString(),
              processingResult);
        }

        if (processingResult) {
          unmatchedAnnotations.keySet().removeAll(matchedNames);
        }
      }
    }
    unmatchedAnnotations.remove("");

    if (lint && unmatchedAnnotations.size() > 0) {
      // Remove annotations processed by javac
      unmatchedAnnotations.keySet().removeAll(platformAnnotations);
      if (unmatchedAnnotations.size() > 0) {
        log = Log.instance(context);
        log.warning("proc.annotations.without.processors", unmatchedAnnotations.keySet());
      }
    }

    // Run contributing processors that haven't run yet
    psi.runContributingProcs(renv);

    // Debugging
    if (options.isSet("displayFilerState")) filer.displayState();
  }
 private Set<String> initUnmatchedProcessorOptions() {
   Set<String> unmatchedProcessorOptions = new HashSet<String>();
   unmatchedProcessorOptions.addAll(processorOptions.keySet());
   return unmatchedProcessorOptions;
 }