jdk/make/modules/tools/src/com/sun/classanalyzer/AnnotatedDependency.java
changeset 8852 c228cf346138
parent 8851 e630c590eb10
parent 8717 f75a1efb1412
child 8853 6aa795396cc8
child 9067 c0b85430843d
equal deleted inserted replaced
8851:e630c590eb10 8852:c228cf346138
     1 /*
       
     2  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  *
       
    23  */
       
    24 package com.sun.classanalyzer;
       
    25 
       
    26 import java.io.BufferedReader;
       
    27 import java.io.File;
       
    28 import java.io.IOException;
       
    29 import java.io.InputStream;
       
    30 import java.io.InputStreamReader;
       
    31 import java.util.ArrayList;
       
    32 import java.util.Collections;
       
    33 import java.util.List;
       
    34 import java.util.Set;
       
    35 import java.util.TreeSet;
       
    36 import java.util.Map;
       
    37 
       
    38 import com.sun.classanalyzer.Module.Reference;
       
    39 import java.util.LinkedList;
       
    40 import java.util.TreeMap;
       
    41 
       
    42 /**
       
    43  *
       
    44  * @author Mandy Chung
       
    45  */
       
    46 public abstract class AnnotatedDependency implements Comparable<AnnotatedDependency> {
       
    47 
       
    48     final Klass from;
       
    49     final List<String> classes;
       
    50     protected boolean optional;
       
    51     String description;
       
    52     Klass.Method method;
       
    53     private List<Filter> filters = null;
       
    54 
       
    55     public AnnotatedDependency(Klass klass) {
       
    56         this(klass, false);
       
    57     }
       
    58 
       
    59     public AnnotatedDependency(Klass klass, boolean optional) {
       
    60         this.from = klass;
       
    61         this.classes = new ArrayList<String>();
       
    62         this.optional = optional;
       
    63     }
       
    64 
       
    65     abstract String getTag();
       
    66 
       
    67     abstract boolean isDynamic();
       
    68 
       
    69     void setMethod(Klass.Method m) {
       
    70         this.method = m;
       
    71     }
       
    72 
       
    73     void addElement(String element, List<String> value) {
       
    74         if (element.equals("value")) {
       
    75             addValue(value);
       
    76         } else if (element.equals("description")) {
       
    77             description = value.get(0);
       
    78         } else if (element.equals("optional")) {
       
    79             optional = value.get(0).equals("1") || Boolean.parseBoolean(value.get(0));
       
    80         }
       
    81     }
       
    82 
       
    83     void addValue(List<String> value) {
       
    84         for (String s : value) {
       
    85             if ((s = s.trim()).length() > 0) {
       
    86                 classes.add(s);
       
    87             }
       
    88         }
       
    89     }
       
    90 
       
    91     List<String> getValue() {
       
    92         return classes;
       
    93     }
       
    94 
       
    95     boolean isOptional() {
       
    96         return optional;
       
    97     }
       
    98 
       
    99     boolean isEmpty() {
       
   100         return classes.isEmpty();
       
   101     }
       
   102 
       
   103     boolean matches(String classname) {
       
   104         synchronized (this) {
       
   105             // initialize filters
       
   106             if (filters == null) {
       
   107                 filters = new ArrayList<Filter>();
       
   108                 for (String pattern : classes) {
       
   109                     filters.add(new Filter(pattern));
       
   110                 }
       
   111 
       
   112             }
       
   113         }
       
   114 
       
   115         for (Filter f : filters) {
       
   116             if (f.matches(classname)) {
       
   117                 return true;
       
   118             }
       
   119         }
       
   120         return false;
       
   121     }
       
   122 
       
   123     @Override
       
   124     public String toString() {
       
   125         StringBuilder sb = new StringBuilder();
       
   126         for (String v : getValue()) {
       
   127             if (sb.length() == 0) {
       
   128                 sb.append(getTag());
       
   129                 sb.append("\n");
       
   130             } else {
       
   131                 sb.append("\n");
       
   132             }
       
   133             sb.append("  ");
       
   134             sb.append(from.getClassName()).append(" -> ");
       
   135             sb.append(v);
       
   136         }
       
   137         return sb.toString();
       
   138     }
       
   139 
       
   140     @Override
       
   141     public int compareTo(AnnotatedDependency o) {
       
   142         if (from == o.from) {
       
   143             if (this.getClass().getName().equals(o.getClass().getName())) {
       
   144                 String s1 = classes.isEmpty() ? "" : classes.get(0);
       
   145                 String s2 = o.classes.isEmpty() ? "" : o.classes.get(0);
       
   146                 return s1.compareTo(s2);
       
   147             } else {
       
   148                 return this.getClass().getName().compareTo(o.getClass().getName());
       
   149             }
       
   150 
       
   151         } else {
       
   152             return from.compareTo(o.from);
       
   153         }
       
   154     }
       
   155 
       
   156     @Override
       
   157     public int hashCode() {
       
   158         int hashcode = 7 + 73 * from.hashCode();
       
   159         for (String s : classes) {
       
   160             hashcode ^= s.hashCode();
       
   161         }
       
   162         return hashcode;
       
   163     }
       
   164 
       
   165     @Override
       
   166     public boolean equals(Object obj) {
       
   167         if (!(obj instanceof AnnotatedDependency)) {
       
   168             return false;
       
   169         }
       
   170         AnnotatedDependency other = (AnnotatedDependency) obj;
       
   171         boolean ret = this.from.equals(other.from) && this.classes.size() == other.classes.size();
       
   172         if (ret == true) {
       
   173             for (int i = 0; i < this.classes.size(); i++) {
       
   174                 ret = ret && this.classes.get(i).equals(other.classes.get(i));
       
   175             }
       
   176         }
       
   177         return ret;
       
   178     }
       
   179 
       
   180     static class ClassForName extends AnnotatedDependency {
       
   181 
       
   182         public ClassForName(Klass klass, boolean optional) {
       
   183             super(klass, optional);
       
   184         }
       
   185 
       
   186         @Override
       
   187         String getTag() {
       
   188             if (this.optional) {
       
   189                 return TAG + "(optional)";
       
   190             } else {
       
   191                 return TAG;
       
   192             }
       
   193         }
       
   194 
       
   195         @Override
       
   196         boolean isDynamic() {
       
   197             return true;
       
   198         }
       
   199         static final String TYPE = "sun.annotation.ClassForName";
       
   200         static final String TAG = "@ClassForName";
       
   201     }
       
   202 
       
   203     static class NativeFindClass extends AnnotatedDependency {
       
   204 
       
   205         public NativeFindClass(Klass klass, boolean optional) {
       
   206             super(klass, optional);
       
   207         }
       
   208 
       
   209         @Override
       
   210         String getTag() {
       
   211             if (this.optional) {
       
   212                 return TAG + "(optional)";
       
   213             } else {
       
   214                 return TAG;
       
   215             }
       
   216         }
       
   217 
       
   218         @Override
       
   219         boolean isDynamic() {
       
   220             return true;
       
   221         }
       
   222         static final String TYPE = "sun.annotation.NativeFindClass";
       
   223         static final String TAG = "@NativeFindClass";
       
   224     }
       
   225 
       
   226     static class Provider extends AnnotatedDependency {
       
   227 
       
   228         private List<String> services = new ArrayList<String>();
       
   229 
       
   230         Provider(Klass klass) {
       
   231             super(klass, true);
       
   232         }
       
   233 
       
   234         @Override
       
   235         boolean isDynamic() {
       
   236             return true;
       
   237         }
       
   238 
       
   239         public List<String> services() {
       
   240             return services;
       
   241         }
       
   242 
       
   243         @Override
       
   244         void addElement(String element, List<String> value) {
       
   245             if (element.equals("service")) {
       
   246                 List<String> configFiles = new ArrayList<String>();
       
   247                 for (String s : value) {
       
   248                     if ((s = s.trim()).length() > 0) {
       
   249                         configFiles.add(metaInfPath + s);
       
   250                     }
       
   251                 }
       
   252                 addValue(configFiles);
       
   253             }
       
   254         }
       
   255 
       
   256         @Override
       
   257         void addValue(List<String> value) {
       
   258             for (String s : value) {
       
   259                 if ((s = s.trim()).length() > 0) {
       
   260                     if (s.startsWith("META-INF")) {
       
   261                         services.add(s);
       
   262                         readServiceConfiguration(s, classes);
       
   263                     } else {
       
   264                         throw new RuntimeException("invalid value" + s);
       
   265                     }
       
   266                 }
       
   267             }
       
   268         }
       
   269 
       
   270         boolean isEmpty() {
       
   271             return services.isEmpty();
       
   272         }
       
   273         static final String metaInfPath =
       
   274                 "META-INF" + File.separator + "services" + File.separator;
       
   275 
       
   276         static void readServiceConfiguration(String config, List<String> names) {
       
   277             BufferedReader br = null;
       
   278             try {
       
   279                 InputStream is = ClassPath.open(config);
       
   280                 if (is != null) {
       
   281                     // Properties doesn't perserve the order of the input file
       
   282                     br = new BufferedReader(new InputStreamReader(is, "utf-8"));
       
   283                     int lc = 1;
       
   284                     while ((lc = parseLine(br, lc, names)) >= 0);
       
   285                 }
       
   286             } catch (IOException ex) {
       
   287                 throw new RuntimeException(ex);
       
   288             } finally {
       
   289                 if (br != null) {
       
   290                     try {
       
   291                         br.close();
       
   292                     } catch (IOException ex) {
       
   293                         throw new RuntimeException(ex);
       
   294                     }
       
   295                 }
       
   296             }
       
   297         }
       
   298 
       
   299         // Parse a single line from the given configuration file, adding the name
       
   300         // on the line to the names list.
       
   301         //
       
   302         private static int parseLine(BufferedReader r, int lc, List<String> names) throws IOException {
       
   303             String ln = r.readLine();
       
   304             if (ln == null) {
       
   305                 return -1;
       
   306             }
       
   307             int ci = ln.indexOf('#');
       
   308             if (ci >= 0) {
       
   309                 ln = ln.substring(0, ci);
       
   310             }
       
   311             ln = ln.trim();
       
   312             int n = ln.length();
       
   313             if (n != 0) {
       
   314                 if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) {
       
   315                     throw new RuntimeException("Illegal configuration-file syntax");
       
   316                 }
       
   317                 int cp = ln.codePointAt(0);
       
   318                 if (!Character.isJavaIdentifierStart(cp)) {
       
   319                     throw new RuntimeException("Illegal provider-class name: " + ln);
       
   320                 }
       
   321                 for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
       
   322                     cp = ln.codePointAt(i);
       
   323                     if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) {
       
   324                         throw new RuntimeException("Illegal provider-class name: " + ln);
       
   325                     }
       
   326                 }
       
   327                 if (!names.contains(ln)) {
       
   328                     names.add(ln);
       
   329                 }
       
   330             }
       
   331             return lc + 1;
       
   332         }
       
   333 
       
   334         @Override
       
   335         String getTag() {
       
   336             return TAG;
       
   337         }
       
   338 
       
   339         @Override
       
   340         public boolean equals(Object obj) {
       
   341             if (!(obj instanceof AnnotatedDependency)) {
       
   342                 return false;
       
   343             }
       
   344             Provider other = (Provider) obj;
       
   345             boolean ret = this.from.equals(other.from) &&
       
   346                     this.services.size() == other.services.size();
       
   347             if (ret == true) {
       
   348                 for (int i = 0; i < this.services.size(); i++) {
       
   349                     ret = ret && this.services.get(i).equals(other.services.get(i));
       
   350                 }
       
   351             }
       
   352             return ret;
       
   353         }
       
   354 
       
   355         @Override
       
   356         public int hashCode() {
       
   357             int hashcode = 7 + 73 * from.hashCode();
       
   358             for (String s : services) {
       
   359                 hashcode ^= s.hashCode();
       
   360             }
       
   361             return hashcode;
       
   362         }
       
   363 
       
   364         @Override
       
   365         public List<String> getValue() {
       
   366             List<String> result = new ArrayList<String>();
       
   367             result.addAll(services);
       
   368             return result;
       
   369         }
       
   370         static final String TYPE = "sun.annotation.Provider";
       
   371         static final String TAG = "@Provider";
       
   372     }
       
   373 
       
   374     static class OptionalDependency extends AnnotatedDependency {
       
   375 
       
   376         static boolean isOptional(Klass from, Klass to) {
       
   377             synchronized (OptionalDependency.class) {
       
   378                 if (optionalDepsMap == null) {
       
   379                     // Build a map of classes to its optional dependencies
       
   380                     initDependencies();
       
   381                 }
       
   382             }
       
   383             for (Reference ref : optionalDepsMap.keySet()) {
       
   384                 if (ref.referrer() == from && ref.referree() == to) {
       
   385                     return true;
       
   386                 }
       
   387             }
       
   388             return false;
       
   389         }
       
   390 
       
   391         OptionalDependency(Klass klass) {
       
   392             super(klass, true);
       
   393         }
       
   394 
       
   395         @Override
       
   396         boolean isDynamic() {
       
   397             return false;
       
   398         }
       
   399 
       
   400         @Override
       
   401         String getTag() {
       
   402             return TAG;
       
   403         }
       
   404         static final String TYPE = "sun.annotation.Optional";
       
   405         static final String TAG = "@Optional";
       
   406     }
       
   407 
       
   408     static class CompilerInline extends AnnotatedDependency {
       
   409 
       
   410         public CompilerInline(Klass klass) {
       
   411             super(klass);
       
   412         }
       
   413 
       
   414         @Override
       
   415         String getTag() {
       
   416             return TAG;
       
   417         }
       
   418 
       
   419         @Override
       
   420         boolean isDynamic() {
       
   421             return false;
       
   422         }
       
   423         static final String TYPE = "sun.annotation.Inline";
       
   424         static final String TAG = "@Inline";
       
   425     }
       
   426 
       
   427     static class Filter {
       
   428 
       
   429         final String pattern;
       
   430         final String regex;
       
   431 
       
   432         Filter(String pattern) {
       
   433             this.pattern = pattern;
       
   434 
       
   435             boolean isRegex = false;
       
   436             for (int i = 0; i < pattern.length(); i++) {
       
   437                 char p = pattern.charAt(i);
       
   438                 if (p == '*' || p == '[' || p == ']') {
       
   439                     isRegex = true;
       
   440                     break;
       
   441                 }
       
   442             }
       
   443 
       
   444             if (isRegex) {
       
   445                 this.regex = convertToRegex(pattern);
       
   446             } else {
       
   447                 this.regex = null;
       
   448             }
       
   449         }
       
   450 
       
   451         private String convertToRegex(String pattern) {
       
   452             StringBuilder sb = new StringBuilder();
       
   453             int i = 0;
       
   454             int index = 0;
       
   455             int plen = pattern.length();
       
   456             while (i < plen) {
       
   457                 char p = pattern.charAt(i);
       
   458                 if (p == '*') {
       
   459                     sb.append("(").append(pattern.substring(index, i)).append(")");
       
   460                     if (i + 1 < plen && pattern.charAt(i + 1) == '*') {
       
   461                         sb.append(".*");
       
   462                         index = i + 2;
       
   463                     } else {
       
   464                         sb.append("[^\\.]*");
       
   465                         index = i + 1;
       
   466                     }
       
   467                 } else if (p == '[') {
       
   468                     int j = i + 1;
       
   469                     while (j < plen) {
       
   470                         if (pattern.charAt(j) == ']') {
       
   471                             break;
       
   472                         }
       
   473                         j++;
       
   474                     }
       
   475                     if (j >= plen || pattern.charAt(j) != ']') {
       
   476                         throw new RuntimeException("Malformed pattern " + pattern);
       
   477                     }
       
   478                     sb.append("(").append(pattern.substring(index, i)).append(")");
       
   479                     sb.append(pattern.substring(i, j + 1));
       
   480                     index = j + 1;
       
   481                     i = j;
       
   482                 }
       
   483                 i++;
       
   484             }
       
   485             if (index < plen) {
       
   486                 sb.append("(").append(pattern.substring(index, plen)).append(")");
       
   487             }
       
   488             return sb.toString();
       
   489         }
       
   490 
       
   491         boolean matches(String name) {
       
   492             if (regex == null) {
       
   493                 // the pattern is not a regex
       
   494                 return name.equals(pattern);
       
   495             } else {
       
   496                 return name.matches(regex);
       
   497             }
       
   498         }
       
   499     }
       
   500 
       
   501     static boolean isValidType(String type) {
       
   502         if (type.endsWith("(optional)")) {
       
   503             int len = type.length() - "(optional)".length();
       
   504             type = type.substring(0, len);
       
   505         }
       
   506         return type.equals(ClassForName.TYPE) || type.equals(ClassForName.TAG) ||
       
   507                 type.equals(NativeFindClass.TYPE) || type.equals(NativeFindClass.TAG) ||
       
   508                 type.equals(Provider.TYPE) || type.equals(Provider.TAG) ||
       
   509                 type.equals(CompilerInline.TYPE) || type.equals(CompilerInline.TAG) ||
       
   510                 type.equals(OptionalDependency.TYPE) || type.equals(OptionalDependency.TAG);
       
   511     }
       
   512 
       
   513     static AnnotatedDependency newAnnotatedDependency(String tag, String value, Klass klass) {
       
   514         AnnotatedDependency dep = newAnnotatedDependency(tag, klass);
       
   515         if (dep != null) {
       
   516             dep.addValue(Collections.singletonList(value));
       
   517         }
       
   518         return dep;
       
   519     }
       
   520     static List<AnnotatedDependency> annotatedDependencies = new LinkedList<AnnotatedDependency>();
       
   521     static List<AnnotatedDependency> optionalDependencies = new LinkedList<AnnotatedDependency>();
       
   522 
       
   523     static AnnotatedDependency newAnnotatedDependency(String type, Klass klass) {
       
   524         boolean optional = false;
       
   525         if (type.endsWith("(optional)")) {
       
   526             optional = true;
       
   527             int len = type.length() - "(optional)".length();
       
   528             type = type.substring(0, len);
       
   529         }
       
   530 
       
   531         if (type.equals(OptionalDependency.TYPE) || type.equals(OptionalDependency.TAG)) {
       
   532             return newOptionalDependency(klass);
       
   533         }
       
   534 
       
   535         AnnotatedDependency dep;
       
   536         if (type.equals(ClassForName.TYPE) || type.equals(ClassForName.TAG)) {
       
   537             dep = new ClassForName(klass, optional);
       
   538         } else if (type.equals(NativeFindClass.TYPE) || type.equals(NativeFindClass.TAG)) {
       
   539             dep = new NativeFindClass(klass, optional);
       
   540         } else if (type.equals(Provider.TYPE) || type.equals(Provider.TAG)) {
       
   541             dep = new Provider(klass);
       
   542         } else if (type.equals(CompilerInline.TYPE) || type.equals(CompilerInline.TAG)) {
       
   543             dep = new CompilerInline(klass);
       
   544         } else {
       
   545             return null;
       
   546         }
       
   547         klass.addAnnotatedDep(dep);
       
   548         annotatedDependencies.add(dep);
       
   549         return dep;
       
   550     }
       
   551 
       
   552     static OptionalDependency newOptionalDependency(Klass klass) {
       
   553         OptionalDependency dep = new OptionalDependency(klass);
       
   554         optionalDependencies.add(dep);
       
   555         return dep;
       
   556     }
       
   557     static Map<Reference, Set<AnnotatedDependency>> annotatedDepsMap = null;
       
   558     static Map<Reference, Set<AnnotatedDependency>> optionalDepsMap = null;
       
   559 
       
   560     static Map<Reference, Set<AnnotatedDependency>> getReferences(Module m) {
       
   561         // ensure it's initialized
       
   562         initDependencies();
       
   563 
       
   564         Map<Reference, Set<AnnotatedDependency>> result = new TreeMap<Reference, Set<AnnotatedDependency>>();
       
   565         for (Reference ref : annotatedDepsMap.keySet()) {
       
   566             if (m.contains(ref.referrer()) && m.isModuleDependence(ref.referree())) {
       
   567                 result.put(ref, annotatedDepsMap.get(ref));
       
   568             }
       
   569         }
       
   570         return result;
       
   571     }
       
   572 
       
   573     static Set<Module.Dependency> getDependencies(Module m) {
       
   574         // ensure it's initialized
       
   575         initDependencies();
       
   576 
       
   577         Set<Module.Dependency> deps = new TreeSet<Module.Dependency>();
       
   578         for (Reference ref : annotatedDepsMap.keySet()) {
       
   579             if (m.contains(ref.referrer())) {
       
   580                 Module other = m.getModuleDependence(ref.referree());
       
   581                 if (other != null) {
       
   582                     for (AnnotatedDependency ad : annotatedDepsMap.get(ref)) {
       
   583                         Module.Dependency d = new Module.Dependency(other, ad.isOptional(), ad.isDynamic());
       
   584                         deps.add(d);
       
   585                     }
       
   586                 }
       
   587             }
       
   588         }
       
   589         return deps;
       
   590     }
       
   591 
       
   592     synchronized static void initDependencies() {
       
   593         if (annotatedDepsMap != null) {
       
   594             return;
       
   595         }
       
   596 
       
   597         // Build a map of references to its dependencies
       
   598         annotatedDepsMap = new TreeMap<Reference, Set<AnnotatedDependency>>();
       
   599         optionalDepsMap = new TreeMap<Reference, Set<AnnotatedDependency>>();
       
   600 
       
   601         for (Klass k : Klass.getAllClasses()) {
       
   602             for (AnnotatedDependency ad : annotatedDependencies) {
       
   603                 if (ad.matches(k.getClassName())) {
       
   604                     Reference ref = new Reference(ad.from, k);
       
   605                     Set<AnnotatedDependency> set = annotatedDepsMap.get(ref);
       
   606                     if (set == null) {
       
   607                         set = new TreeSet<AnnotatedDependency>();
       
   608                         annotatedDepsMap.put(ref, set);
       
   609                     }
       
   610                     set.add(ad);
       
   611                 }
       
   612             }
       
   613 
       
   614             for (AnnotatedDependency ad : optionalDependencies) {
       
   615                 if (ad.matches(k.getClassName())) {
       
   616                     Reference ref = new Reference(ad.from, k);
       
   617                     Set<AnnotatedDependency> set = optionalDepsMap.get(ref);
       
   618                     if (set == null) {
       
   619                         set = new TreeSet<AnnotatedDependency>();
       
   620                         optionalDepsMap.put(ref, set);
       
   621                     }
       
   622                     set.add(ad);
       
   623                 }
       
   624             }
       
   625         }
       
   626     }
       
   627 }