Tue, 05 Mar 2013 14:19:49 +0000
8009129: Illegal access error when calling method reference
Summary: Javac generates method handle referencing non public type
Reviewed-by: jjg, rfield
1 /*
2 * Copyright (c) 2011, 2013, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.javac.tree;
29 import javax.tools.Diagnostic;
31 import com.sun.source.doctree.*;
32 import com.sun.tools.javac.parser.Tokens.Comment;
33 import com.sun.tools.javac.util.Assert;
34 import com.sun.tools.javac.util.DiagnosticSource;
35 import com.sun.tools.javac.util.JCDiagnostic;
36 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
37 import com.sun.tools.javac.util.List;
38 import com.sun.tools.javac.util.Name;
39 import java.io.IOException;
40 import java.io.StringWriter;
41 import javax.tools.JavaFileObject;
43 /**
44 * <p><b>This is NOT part of any supported API.
45 * If you write code that depends on this, you do so at your own risk.
46 * This code and its internal interfaces are subject to change or
47 * deletion without notice.</b>
48 */
49 public abstract class DCTree implements DocTree {
51 /**
52 * The position in the comment string.
53 * Use {@link #getSourcePosition getSourcePosition} to convert
54 * it to a position in the source file.
55 *
56 * TODO: why not simply translate all these values into
57 * source file positions? Is it useful to have string-offset
58 * positions as well?
59 */
60 public int pos;
62 public long getSourcePosition(DCDocComment dc) {
63 return dc.comment.getSourcePos(pos);
64 }
66 public JCDiagnostic.DiagnosticPosition pos(DCDocComment dc) {
67 return new SimpleDiagnosticPosition(dc.comment.getSourcePos(pos));
68 }
70 /** Convert a tree to a pretty-printed string. */
71 @Override
72 public String toString() {
73 StringWriter s = new StringWriter();
74 try {
75 new DocPretty(s).print(this);
76 }
77 catch (IOException e) {
78 // should never happen, because StringWriter is defined
79 // never to throw any IOExceptions
80 throw new AssertionError(e);
81 }
82 return s.toString();
83 }
85 public static class DCDocComment extends DCTree implements DocCommentTree {
86 final Comment comment; // required for the implicit source pos table
88 public final List<DCTree> firstSentence;
89 public final List<DCTree> body;
90 public final List<DCTree> tags;
92 public DCDocComment(Comment comment,
93 List<DCTree> firstSentence, List<DCTree> body, List<DCTree> tags) {
94 this.comment = comment;
95 this.firstSentence = firstSentence;
96 this.body = body;
97 this.tags = tags;
98 }
100 public Kind getKind() {
101 return Kind.DOC_COMMENT;
102 }
104 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
105 return v.visitDocComment(this, d);
106 }
108 public List<? extends DocTree> getFirstSentence() {
109 return firstSentence;
110 }
112 public List<? extends DocTree> getBody() {
113 return body;
114 }
116 public List<? extends DocTree> getBlockTags() {
117 return tags;
118 }
120 }
122 public static abstract class DCBlockTag extends DCTree implements BlockTagTree {
123 public String getTagName() {
124 return getKind().tagName;
125 }
126 }
128 public static abstract class DCInlineTag extends DCTree implements InlineTagTree {
129 public String getTagName() {
130 return getKind().tagName;
131 }
132 }
134 public static class DCAttribute extends DCTree implements AttributeTree {
135 public final Name name;
136 public final ValueKind vkind;
137 public final List<DCTree> value;
139 DCAttribute(Name name, ValueKind vkind, List<DCTree> value) {
140 Assert.check((vkind == ValueKind.EMPTY) ? (value == null) : (value != null));
141 this.name = name;
142 this.vkind = vkind;
143 this.value = value;
144 }
146 @Override
147 public Kind getKind() {
148 return Kind.ATTRIBUTE;
149 }
151 @Override
152 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
153 return v.visitAttribute(this, d);
154 }
156 @Override
157 public Name getName() {
158 return name;
159 }
161 @Override
162 public ValueKind getValueKind() {
163 return vkind;
164 }
166 @Override
167 public List<DCTree> getValue() {
168 return value;
169 }
170 }
172 public static class DCAuthor extends DCBlockTag implements AuthorTree {
173 public final List<DCTree> name;
175 DCAuthor(List<DCTree> name) {
176 this.name = name;
177 }
179 @Override
180 public Kind getKind() {
181 return Kind.AUTHOR;
182 }
184 @Override
185 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
186 return v.visitAuthor(this, d);
187 }
189 @Override
190 public List<? extends DocTree> getName() {
191 return name;
192 }
193 }
195 public static class DCComment extends DCTree implements CommentTree {
196 public final String body;
198 DCComment(String body) {
199 this.body = body;
200 }
202 @Override
203 public Kind getKind() {
204 return Kind.COMMENT;
205 }
207 @Override
208 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
209 return v.visitComment(this, d);
210 }
212 @Override
213 public String getBody() {
214 return body;
215 }
216 }
218 public static class DCDeprecated extends DCBlockTag implements DeprecatedTree {
219 public final List<DCTree> body;
221 DCDeprecated(List<DCTree> body) {
222 this.body = body;
223 }
225 @Override
226 public Kind getKind() {
227 return Kind.DEPRECATED;
228 }
230 @Override
231 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
232 return v.visitDeprecated(this, d);
233 }
235 @Override
236 public List<? extends DocTree> getBody() {
237 return body;
238 }
239 }
241 public static class DCDocRoot extends DCInlineTag implements DocRootTree {
243 @Override
244 public Kind getKind() {
245 return Kind.DOC_ROOT;
246 }
248 @Override
249 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
250 return v.visitDocRoot(this, d);
251 }
252 }
254 public static class DCEndElement extends DCTree implements EndElementTree {
255 public final Name name;
257 DCEndElement(Name name) {
258 this.name = name;
259 }
261 @Override
262 public Kind getKind() {
263 return Kind.END_ELEMENT;
264 }
266 @Override
267 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
268 return v.visitEndElement(this, d);
269 }
271 @Override
272 public Name getName() {
273 return name;
274 }
275 }
277 public static class DCEntity extends DCTree implements EntityTree {
278 public final Name name;
280 DCEntity(Name name) {
281 this.name = name;
282 }
284 @Override
285 public Kind getKind() {
286 return Kind.ENTITY;
287 }
289 @Override
290 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
291 return v.visitEntity(this, d);
292 }
294 @Override
295 public Name getName() {
296 return name;
297 }
298 }
300 public static class DCErroneous extends DCTree implements ErroneousTree, JCDiagnostic.DiagnosticPosition {
301 public final String body;
302 public final JCDiagnostic diag;
304 DCErroneous(String body, JCDiagnostic.Factory diags, DiagnosticSource diagSource, String code, Object... args) {
305 this.body = body;
306 this.diag = diags.error(diagSource, this, code, args);
307 }
309 @Override
310 public Kind getKind() {
311 return Kind.ERRONEOUS;
312 }
314 @Override
315 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
316 return v.visitErroneous(this, d);
317 }
319 @Override
320 public String getBody() {
321 return body;
322 }
324 @Override
325 public Diagnostic<JavaFileObject> getDiagnostic() {
326 return diag;
327 }
329 @Override
330 public JCTree getTree() {
331 return null;
332 }
334 @Override
335 public int getStartPosition() {
336 return pos;
337 }
339 @Override
340 public int getPreferredPosition() {
341 return pos + body.length() - 1;
342 }
344 @Override
345 public int getEndPosition(EndPosTable endPosTable) {
346 return pos + body.length();
347 }
348 }
350 public static class DCIdentifier extends DCTree implements IdentifierTree {
351 public final Name name;
353 DCIdentifier(Name name) {
354 this.name = name;
355 }
357 @Override
358 public Kind getKind() {
359 return Kind.IDENTIFIER;
360 }
362 @Override
363 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
364 return v.visitIdentifier(this, d);
365 }
367 @Override
368 public Name getName() {
369 return name;
370 }
371 }
373 public static class DCInheritDoc extends DCInlineTag implements InheritDocTree {
374 @Override
375 public Kind getKind() {
376 return Kind.INHERIT_DOC;
377 }
379 @Override
380 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
381 return v.visitInheritDoc(this, d);
382 }
383 }
385 public static class DCLink extends DCInlineTag implements LinkTree {
386 public final Kind kind;
387 public final DCReference ref;
388 public final List<DCTree> label;
390 DCLink(Kind kind, DCReference ref, List<DCTree> label) {
391 Assert.check(kind == Kind.LINK || kind == Kind.LINK_PLAIN);
392 this.kind = kind;
393 this.ref = ref;
394 this.label = label;
395 }
397 @Override
398 public Kind getKind() {
399 return kind;
400 }
402 @Override
403 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
404 return v.visitLink(this, d);
405 }
407 @Override
408 public ReferenceTree getReference() {
409 return ref;
410 }
412 @Override
413 public List<? extends DocTree> getLabel() {
414 return label;
415 }
416 }
418 public static class DCLiteral extends DCInlineTag implements LiteralTree {
419 public final Kind kind;
420 public final DCText body;
422 DCLiteral(Kind kind, DCText body) {
423 Assert.check(kind == Kind.CODE || kind == Kind.LITERAL);
424 this.kind = kind;
425 this.body = body;
426 }
428 @Override
429 public Kind getKind() {
430 return kind;
431 }
433 @Override
434 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
435 return v.visitLiteral(this, d);
436 }
438 @Override
439 public DCText getBody() {
440 return body;
441 }
442 }
444 public static class DCParam extends DCBlockTag implements ParamTree {
445 public final boolean isTypeParameter;
446 public final DCIdentifier name;
447 public final List<DCTree> description;
449 DCParam(boolean isTypeParameter, DCIdentifier name, List<DCTree> description) {
450 this.isTypeParameter = isTypeParameter;
451 this.name = name;
452 this.description = description;
453 }
455 @Override
456 public Kind getKind() {
457 return Kind.PARAM;
458 }
460 @Override
461 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
462 return v.visitParam(this, d);
463 }
465 @Override
466 public boolean isTypeParameter() {
467 return isTypeParameter;
468 }
470 @Override
471 public IdentifierTree getName() {
472 return name;
473 }
475 @Override
476 public List<? extends DocTree> getDescription() {
477 return description;
478 }
479 }
481 public static class DCReference extends DCTree implements ReferenceTree {
482 public final String signature;
484 // The following are not directly exposed through ReferenceTree
485 // use DocTrees.getElement(TreePath,ReferenceTree)
486 public final JCTree qualifierExpression;
487 public final Name memberName;
488 public final List<JCTree> paramTypes;
491 DCReference(String signature, JCTree qualExpr, Name member, List<JCTree> paramTypes) {
492 this.signature = signature;
493 qualifierExpression = qualExpr;
494 memberName = member;
495 this.paramTypes = paramTypes;
496 }
498 @Override
499 public Kind getKind() {
500 return Kind.REFERENCE;
501 }
503 @Override
504 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
505 return v.visitReference(this, d);
506 }
508 @Override
509 public String getSignature() {
510 return signature;
511 }
512 }
514 public static class DCReturn extends DCBlockTag implements ReturnTree {
515 public final List<DCTree> description;
517 DCReturn(List<DCTree> description) {
518 this.description = description;
519 }
521 @Override
522 public Kind getKind() {
523 return Kind.RETURN;
524 }
526 @Override
527 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
528 return v.visitReturn(this, d);
529 }
531 @Override
532 public List<? extends DocTree> getDescription() {
533 return description;
534 }
535 }
537 public static class DCSee extends DCBlockTag implements SeeTree {
538 public final List<DCTree> reference;
540 DCSee(List<DCTree> reference) {
541 this.reference = reference;
542 }
544 @Override
545 public Kind getKind() {
546 return Kind.SEE;
547 }
549 @Override
550 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
551 return v.visitSee(this, d);
552 }
554 @Override
555 public List<? extends DocTree> getReference() {
556 return reference;
557 }
558 }
560 public static class DCSerial extends DCBlockTag implements SerialTree {
561 public final List<DCTree> description;
563 DCSerial(List<DCTree> description) {
564 this.description = description;
565 }
567 @Override
568 public Kind getKind() {
569 return Kind.SERIAL;
570 }
572 @Override
573 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
574 return v.visitSerial(this, d);
575 }
577 @Override
578 public List<? extends DocTree> getDescription() {
579 return description;
580 }
581 }
583 public static class DCSerialData extends DCBlockTag implements SerialDataTree {
584 public final List<DCTree> description;
586 DCSerialData(List<DCTree> description) {
587 this.description = description;
588 }
590 @Override
591 public Kind getKind() {
592 return Kind.SERIAL_DATA;
593 }
595 @Override
596 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
597 return v.visitSerialData(this, d);
598 }
600 @Override
601 public List<? extends DocTree> getDescription() {
602 return description;
603 }
604 }
606 public static class DCSerialField extends DCBlockTag implements SerialFieldTree {
607 public final DCIdentifier name;
608 public final DCReference type;
609 public final List<DCTree> description;
611 DCSerialField(DCIdentifier name, DCReference type, List<DCTree> description) {
612 this.description = description;
613 this.name = name;
614 this.type = type;
615 }
617 @Override
618 public Kind getKind() {
619 return Kind.SERIAL_FIELD;
620 }
622 @Override
623 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
624 return v.visitSerialField(this, d);
625 }
627 @Override
628 public List<? extends DocTree> getDescription() {
629 return description;
630 }
632 @Override
633 public IdentifierTree getName() {
634 return name;
635 }
637 @Override
638 public ReferenceTree getType() {
639 return type;
640 }
641 }
643 public static class DCSince extends DCBlockTag implements SinceTree {
644 public final List<DCTree> body;
646 DCSince(List<DCTree> body) {
647 this.body = body;
648 }
650 @Override
651 public Kind getKind() {
652 return Kind.SINCE;
653 }
655 @Override
656 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
657 return v.visitSince(this, d);
658 }
660 @Override
661 public List<? extends DocTree> getBody() {
662 return body;
663 }
664 }
666 public static class DCStartElement extends DCTree implements StartElementTree {
667 public final Name name;
668 public final List<DCTree> attrs;
669 public final boolean selfClosing;
671 DCStartElement(Name name, List<DCTree> attrs, boolean selfClosing) {
672 this.name = name;
673 this.attrs = attrs;
674 this.selfClosing = selfClosing;
675 }
677 @Override
678 public Kind getKind() {
679 return Kind.START_ELEMENT;
680 }
682 @Override
683 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
684 return v.visitStartElement(this, d);
685 }
687 @Override
688 public Name getName() {
689 return name;
690 }
692 @Override
693 public List<? extends DocTree> getAttributes() {
694 return attrs;
695 }
697 @Override
698 public boolean isSelfClosing() {
699 return selfClosing;
700 }
701 }
703 public static class DCText extends DCTree implements TextTree {
704 public final String text;
706 DCText(String text) {
707 this.text = text;
708 }
710 @Override
711 public Kind getKind() {
712 return Kind.TEXT;
713 }
715 @Override
716 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
717 return v.visitText(this, d);
718 }
720 @Override
721 public String getBody() {
722 return text;
723 }
724 }
726 public static class DCThrows extends DCBlockTag implements ThrowsTree {
727 public final Kind kind;
728 public final DCReference name;
729 public final List<DCTree> description;
731 DCThrows(Kind kind, DCReference name, List<DCTree> description) {
732 Assert.check(kind == Kind.EXCEPTION || kind == Kind.THROWS);
733 this.kind = kind;
734 this.name = name;
735 this.description = description;
736 }
738 @Override
739 public Kind getKind() {
740 return kind;
741 }
743 @Override
744 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
745 return v.visitThrows(this, d);
746 }
748 @Override
749 public ReferenceTree getExceptionName() {
750 return name;
751 }
753 @Override
754 public List<? extends DocTree> getDescription() {
755 return description;
756 }
757 }
759 public static class DCUnknownBlockTag extends DCBlockTag implements UnknownBlockTagTree {
760 public final Name name;
761 public final List<DCTree> content;
763 DCUnknownBlockTag(Name name, List<DCTree> content) {
764 this.name = name;
765 this.content = content;
766 }
768 @Override
769 public Kind getKind() {
770 return Kind.UNKNOWN_BLOCK_TAG;
771 }
773 @Override
774 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
775 return v.visitUnknownBlockTag(this, d);
776 }
778 @Override
779 public String getTagName() {
780 return name.toString();
781 }
783 @Override
784 public List<? extends DocTree> getContent() {
785 return content;
786 }
787 }
789 public static class DCUnknownInlineTag extends DCInlineTag implements UnknownInlineTagTree {
790 public final Name name;
791 public final List<DCTree> content;
793 DCUnknownInlineTag(Name name, List<DCTree> content) {
794 this.name = name;
795 this.content = content;
796 }
798 @Override
799 public Kind getKind() {
800 return Kind.UNKNOWN_INLINE_TAG;
801 }
803 @Override
804 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
805 return v.visitUnknownInlineTag(this, d);
806 }
808 @Override
809 public String getTagName() {
810 return name.toString();
811 }
813 @Override
814 public List<? extends DocTree> getContent() {
815 return content;
816 }
817 }
819 public static class DCValue extends DCInlineTag implements ValueTree {
820 public final DCReference ref;
822 DCValue(DCReference ref) {
823 this.ref = ref;
824 }
826 @Override
827 public Kind getKind() {
828 return Kind.VALUE;
829 }
831 @Override
832 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
833 return v.visitValue(this, d);
834 }
836 @Override
837 public ReferenceTree getReference() {
838 return ref;
839 }
840 }
842 public static class DCVersion extends DCBlockTag implements VersionTree {
843 public final List<DCTree> body;
845 DCVersion(List<DCTree> body) {
846 this.body = body;
847 }
849 @Override
850 public Kind getKind() {
851 return Kind.VERSION;
852 }
854 @Override
855 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
856 return v.visitVersion(this, d);
857 }
859 @Override
860 public List<? extends DocTree> getBody() {
861 return body;
862 }
863 }
865 }