30 import java.io.FilterInputStream; |
30 import java.io.FilterInputStream; |
31 import java.io.InputStream; |
31 import java.io.InputStream; |
32 import java.io.IOException; |
32 import java.io.IOException; |
33 import java.io.OutputStream; |
33 import java.io.OutputStream; |
34 import java.io.PrintWriter; |
34 import java.io.PrintWriter; |
|
35 import java.io.Reader; |
35 import java.io.StringWriter; |
36 import java.io.StringWriter; |
36 import java.io.Writer; |
37 import java.io.Writer; |
|
38 import java.net.URI; |
37 import java.security.DigestInputStream; |
39 import java.security.DigestInputStream; |
38 import java.security.MessageDigest; |
40 import java.security.MessageDigest; |
39 import java.security.NoSuchAlgorithmException; |
41 import java.security.NoSuchAlgorithmException; |
40 import java.text.MessageFormat; |
42 import java.text.MessageFormat; |
41 import java.util.ArrayList; |
43 import java.util.ArrayList; |
47 import java.util.Locale; |
49 import java.util.Locale; |
48 import java.util.Map; |
50 import java.util.Map; |
49 import java.util.MissingResourceException; |
51 import java.util.MissingResourceException; |
50 import java.util.ResourceBundle; |
52 import java.util.ResourceBundle; |
51 |
53 |
|
54 import javax.lang.model.element.Modifier; |
|
55 import javax.lang.model.element.NestingKind; |
52 import javax.tools.Diagnostic; |
56 import javax.tools.Diagnostic; |
53 import javax.tools.DiagnosticListener; |
57 import javax.tools.DiagnosticListener; |
54 import javax.tools.JavaFileManager; |
58 import javax.tools.JavaFileManager; |
55 import javax.tools.JavaFileObject; |
59 import javax.tools.JavaFileObject; |
56 import javax.tools.StandardJavaFileManager; |
60 import javax.tools.StandardJavaFileManager; |
57 import javax.tools.StandardLocation; |
61 import javax.tools.StandardLocation; |
58 |
62 |
59 import com.sun.tools.classfile.*; |
63 import com.sun.tools.classfile.*; |
|
64 import java.net.URISyntaxException; |
|
65 import java.net.URL; |
|
66 import java.net.URLConnection; |
60 |
67 |
61 /** |
68 /** |
62 * "Main" class for javap, normally accessed from the command line |
69 * "Main" class for javap, normally accessed from the command line |
63 * via Main, or from JSR199 via DisassemblerTool. |
70 * via Main, or from JSR199 via DisassemblerTool. |
64 * |
71 * |
605 return ok; |
612 return ok; |
606 } |
613 } |
607 |
614 |
608 protected boolean writeClass(ClassWriter classWriter, String className) |
615 protected boolean writeClass(ClassWriter classWriter, String className) |
609 throws IOException, ConstantPoolException { |
616 throws IOException, ConstantPoolException { |
610 JavaFileObject fo; |
617 JavaFileObject fo = open(className); |
611 if (className.endsWith(".class")) { |
618 if (fo == null) { |
612 if (fileManager instanceof StandardJavaFileManager) { |
619 reportError("err.class.not.found", className); |
613 StandardJavaFileManager sfm = (StandardJavaFileManager) fileManager; |
620 return false; |
614 fo = sfm.getJavaFileObjects(className).iterator().next(); |
|
615 } else { |
|
616 reportError("err.not.standard.file.manager", className); |
|
617 return false; |
|
618 } |
|
619 } else { |
|
620 fo = getClassFileObject(className); |
|
621 if (fo == null) { |
|
622 // see if it is an inner class, by replacing dots to $, starting from the right |
|
623 String cn = className; |
|
624 int lastDot; |
|
625 while (fo == null && (lastDot = cn.lastIndexOf(".")) != -1) { |
|
626 cn = cn.substring(0, lastDot) + "$" + cn.substring(lastDot + 1); |
|
627 fo = getClassFileObject(cn); |
|
628 } |
|
629 } |
|
630 if (fo == null) { |
|
631 reportError("err.class.not.found", className); |
|
632 return false; |
|
633 } |
|
634 } |
621 } |
635 |
622 |
636 ClassFileInfo cfInfo = read(fo); |
623 ClassFileInfo cfInfo = read(fo); |
637 if (!className.endsWith(".class")) { |
624 if (!className.endsWith(".class")) { |
638 String cfName = cfInfo.cf.getName(); |
625 String cfName = cfInfo.cf.getName(); |
673 } |
660 } |
674 |
661 |
675 return true; |
662 return true; |
676 } |
663 } |
677 |
664 |
|
665 protected JavaFileObject open(String className) throws IOException { |
|
666 // for compatibility, first see if it is a class name |
|
667 JavaFileObject fo = getClassFileObject(className); |
|
668 if (fo != null) |
|
669 return fo; |
|
670 |
|
671 // see if it is an inner class, by replacing dots to $, starting from the right |
|
672 String cn = className; |
|
673 int lastDot; |
|
674 while ((lastDot = cn.lastIndexOf(".")) != -1) { |
|
675 cn = cn.substring(0, lastDot) + "$" + cn.substring(lastDot + 1); |
|
676 fo = getClassFileObject(cn); |
|
677 if (fo != null) |
|
678 return fo; |
|
679 } |
|
680 |
|
681 if (!className.endsWith(".class")) |
|
682 return null; |
|
683 |
|
684 if (fileManager instanceof StandardJavaFileManager) { |
|
685 StandardJavaFileManager sfm = (StandardJavaFileManager) fileManager; |
|
686 fo = sfm.getJavaFileObjects(className).iterator().next(); |
|
687 if (fo != null && fo.getLastModified() != 0) { |
|
688 return fo; |
|
689 } |
|
690 } |
|
691 |
|
692 // see if it is a URL, and if so, wrap it in just enough of a JavaFileObject |
|
693 // to suit javap's needs |
|
694 if (className.matches("^[A-Za-z]+:.*")) { |
|
695 try { |
|
696 final URI uri = new URI(className); |
|
697 final URL url = uri.toURL(); |
|
698 final URLConnection conn = url.openConnection(); |
|
699 return new JavaFileObject() { |
|
700 public Kind getKind() { |
|
701 return JavaFileObject.Kind.CLASS; |
|
702 } |
|
703 |
|
704 public boolean isNameCompatible(String simpleName, Kind kind) { |
|
705 throw new UnsupportedOperationException(); |
|
706 } |
|
707 |
|
708 public NestingKind getNestingKind() { |
|
709 throw new UnsupportedOperationException(); |
|
710 } |
|
711 |
|
712 public Modifier getAccessLevel() { |
|
713 throw new UnsupportedOperationException(); |
|
714 } |
|
715 |
|
716 public URI toUri() { |
|
717 return uri; |
|
718 } |
|
719 |
|
720 public String getName() { |
|
721 return url.toString(); |
|
722 } |
|
723 |
|
724 public InputStream openInputStream() throws IOException { |
|
725 return conn.getInputStream(); |
|
726 } |
|
727 |
|
728 public OutputStream openOutputStream() throws IOException { |
|
729 throw new UnsupportedOperationException(); |
|
730 } |
|
731 |
|
732 public Reader openReader(boolean ignoreEncodingErrors) throws IOException { |
|
733 throw new UnsupportedOperationException(); |
|
734 } |
|
735 |
|
736 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
|
737 throw new UnsupportedOperationException(); |
|
738 } |
|
739 |
|
740 public Writer openWriter() throws IOException { |
|
741 throw new UnsupportedOperationException(); |
|
742 } |
|
743 |
|
744 public long getLastModified() { |
|
745 return conn.getLastModified(); |
|
746 } |
|
747 |
|
748 public boolean delete() { |
|
749 throw new UnsupportedOperationException(); |
|
750 } |
|
751 |
|
752 }; |
|
753 } catch (URISyntaxException ignore) { |
|
754 } catch (IOException ignore) { |
|
755 } |
|
756 } |
|
757 |
|
758 return null; |
|
759 } |
|
760 |
678 public static class ClassFileInfo { |
761 public static class ClassFileInfo { |
679 ClassFileInfo(JavaFileObject fo, ClassFile cf, byte[] digest, int size) { |
762 ClassFileInfo(JavaFileObject fo, ClassFile cf, byte[] digest, int size) { |
680 this.fo = fo; |
763 this.fo = fo; |
681 this.cf = cf; |
764 this.cf = cf; |
682 this.digest = digest; |
765 this.digest = digest; |