diff --git a/langtools/src/share/classes/com/sun/javadoc/AnnotatedType.java b/langtools/src/share/classes/com/sun/javadoc/AnnotatedType.java new file mode 100644 index 00000000000..c39260e0faf --- /dev/null +++ b/langtools/src/share/classes/com/sun/javadoc/AnnotatedType.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.sun.javadoc; + + +/** + * Represents an annotated type. + * For example: + *
+ * {@code @NonNull String} + * {@code @Positive int} + *+ * + * @author Mahmood Ali + * @since 1.8 + */ +public interface AnnotatedType extends Type { + + AnnotationDesc[] annotations(); + + Type underlyingType(); +} diff --git a/langtools/src/share/classes/com/sun/javadoc/ExecutableMemberDoc.java b/langtools/src/share/classes/com/sun/javadoc/ExecutableMemberDoc.java index f073a5886e7..f1dea97678e 100644 --- a/langtools/src/share/classes/com/sun/javadoc/ExecutableMemberDoc.java +++ b/langtools/src/share/classes/com/sun/javadoc/ExecutableMemberDoc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -87,6 +87,15 @@ public interface ExecutableMemberDoc extends MemberDoc { */ Parameter[] parameters(); + /** + * Get the receiver annotations of this executable element. + * Return an empty array if there are none. + * + * @return the receiver annotations of this executable element. + * @since 1.8 + */ + AnnotationDesc[] receiverAnnotations(); + /** * Return the throws tags in this method. * diff --git a/langtools/src/share/classes/com/sun/javadoc/Type.java b/langtools/src/share/classes/com/sun/javadoc/Type.java index c093fc94757..07b7c0d0509 100644 --- a/langtools/src/share/classes/com/sun/javadoc/Type.java +++ b/langtools/src/share/classes/com/sun/javadoc/Type.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -141,6 +141,16 @@ public interface Type { */ WildcardType asWildcardType(); + /** + * Returns this type as a
AnnotatedType
if it represents
+ * an annotated type.
+ *
+ * @return a AnnotatedType
if the type if an annotated type,
+ * or null if it is not
+ * @since 1.8
+ */
+ AnnotatedType asAnnotatedType();
+
/**
* Return this type as an AnnotationTypeDoc
if it represents
* an annotation type. Array dimensions are ignored.
diff --git a/langtools/src/share/classes/com/sun/javadoc/TypeVariable.java b/langtools/src/share/classes/com/sun/javadoc/TypeVariable.java
index c63421c517d..4212f20a8e7 100644
--- a/langtools/src/share/classes/com/sun/javadoc/TypeVariable.java
+++ b/langtools/src/share/classes/com/sun/javadoc/TypeVariable.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -55,4 +55,11 @@ public interface TypeVariable extends Type {
* which this type variable is declared.
*/
ProgramElementDoc owner();
+
+ /**
+ * Get the annotations of this program element.
+ * Return an empty array if there are none.
+ */
+ public AnnotationDesc[] annotations();
+
}
diff --git a/langtools/src/share/classes/com/sun/source/tree/AnnotatedTypeTree.java b/langtools/src/share/classes/com/sun/source/tree/AnnotatedTypeTree.java
new file mode 100644
index 00000000000..ca799344cc8
--- /dev/null
+++ b/langtools/src/share/classes/com/sun/source/tree/AnnotatedTypeTree.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.source.tree;
+
+import java.util.List;
+
+/**
+ * A tree node for an annotated type
+ *
+ * For example:
+ * + * {@code @}annotationType String + * {@code @}annotationType ( arguments ) Date + *+ * + * @see "JSR 308: Annotations on Java Types" + * + * @author Mahmood Ali + * @since 1.8 + */ +public interface AnnotatedTypeTree extends ExpressionTree { + List extends AnnotationTree> getAnnotations(); + ExpressionTree getUnderlyingType(); +} diff --git a/langtools/src/share/classes/com/sun/source/tree/MethodTree.java b/langtools/src/share/classes/com/sun/source/tree/MethodTree.java index 8dfd0ba8c02..d72302a647b 100644 --- a/langtools/src/share/classes/com/sun/source/tree/MethodTree.java +++ b/langtools/src/share/classes/com/sun/source/tree/MethodTree.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -52,6 +52,7 @@ public interface MethodTree extends Tree { Tree getReturnType(); List extends TypeParameterTree> getTypeParameters(); List extends VariableTree> getParameters(); + VariableTree getReceiverParameter(); List extends ExpressionTree> getThrows(); BlockTree getBody(); Tree getDefaultValue(); // for annotation types diff --git a/langtools/src/share/classes/com/sun/source/tree/Tree.java b/langtools/src/share/classes/com/sun/source/tree/Tree.java index 6ede0373a79..58ecb984b82 100644 --- a/langtools/src/share/classes/com/sun/source/tree/Tree.java +++ b/langtools/src/share/classes/com/sun/source/tree/Tree.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -46,11 +46,20 @@ public interface Tree { */ public enum Kind { + ANNOTATED_TYPE(AnnotatedTypeTree.class), + /** - * Used for instances of {@link AnnotationTree}. + * Used for instances of {@link AnnotationTree} + * representing declaration annotations. */ ANNOTATION(AnnotationTree.class), + /** + * Used for instances of {@link AnnotationTree} + * representing type annotations. + */ + TYPE_ANNOTATION(AnnotationTree.class), + /** * Used for instances of {@link ArrayAccessTree}. */ diff --git a/langtools/src/share/classes/com/sun/source/tree/TreeVisitor.java b/langtools/src/share/classes/com/sun/source/tree/TreeVisitor.java index 40d311e3126..ad549b229e6 100644 --- a/langtools/src/share/classes/com/sun/source/tree/TreeVisitor.java +++ b/langtools/src/share/classes/com/sun/source/tree/TreeVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -57,6 +57,7 @@ package com.sun.source.tree; * @since 1.6 */ public interface TreeVisitor
This is NOT part of any supported API.
+ * If you write code that depends on this, you do so at your own risk.
+ * This code and its internal interfaces are subject to change or
+ * deletion without notice.
+ */
+public class RuntimeInvisibleTypeAnnotations_attribute extends RuntimeTypeAnnotations_attribute {
+ RuntimeInvisibleTypeAnnotations_attribute(ClassReader cr, int name_index, int length)
+ throws IOException, Annotation.InvalidAnnotation {
+ super(cr, name_index, length);
+ }
+
+ public RuntimeInvisibleTypeAnnotations_attribute(ConstantPool cp, TypeAnnotation[] annotations)
+ throws ConstantPoolException {
+ this(cp.getUTF8Index(Attribute.RuntimeInvisibleTypeAnnotations), annotations);
+ }
+
+ public RuntimeInvisibleTypeAnnotations_attribute(int name_index, TypeAnnotation[] annotations) {
+ super(name_index, annotations);
+ }
+
+ public This is NOT part of any supported API.
+ * If you write code that depends on this, you do so at your own risk.
+ * This code and its internal interfaces are subject to change or
+ * deletion without notice.
+ */
+public abstract class RuntimeTypeAnnotations_attribute extends Attribute {
+ protected RuntimeTypeAnnotations_attribute(ClassReader cr, int name_index, int length)
+ throws IOException, Annotation.InvalidAnnotation {
+ super(name_index, length);
+ int num_annotations = cr.readUnsignedShort();
+ annotations = new TypeAnnotation[num_annotations];
+ for (int i = 0; i < annotations.length; i++)
+ annotations[i] = new TypeAnnotation(cr);
+ }
+
+ protected RuntimeTypeAnnotations_attribute(int name_index, TypeAnnotation[] annotations) {
+ super(name_index, length(annotations));
+ this.annotations = annotations;
+ }
+
+ private static int length(TypeAnnotation[] annos) {
+ int n = 2;
+ for (TypeAnnotation anno: annos)
+ n += anno.length();
+ return n;
+ }
+
+ public final TypeAnnotation[] annotations;
+}
diff --git a/langtools/src/share/classes/com/sun/tools/classfile/RuntimeVisibleTypeAnnotations_attribute.java b/langtools/src/share/classes/com/sun/tools/classfile/RuntimeVisibleTypeAnnotations_attribute.java
new file mode 100644
index 00000000000..851c1f7d907
--- /dev/null
+++ b/langtools/src/share/classes/com/sun/tools/classfile/RuntimeVisibleTypeAnnotations_attribute.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.tools.classfile;
+
+import java.io.IOException;
+
+/**
+ * See JSR 308 specification, Section 3.
+ *
+ * This is NOT part of any supported API.
+ * If you write code that depends on this, you do so at your own risk.
+ * This code and its internal interfaces are subject to change or
+ * deletion without notice.
+ */
+public class RuntimeVisibleTypeAnnotations_attribute extends RuntimeTypeAnnotations_attribute {
+ RuntimeVisibleTypeAnnotations_attribute(ClassReader cr, int name_index, int length)
+ throws IOException, Annotation.InvalidAnnotation {
+ super(cr, name_index, length);
+ }
+
+ public RuntimeVisibleTypeAnnotations_attribute(ConstantPool cp, TypeAnnotation[] annotations)
+ throws ConstantPoolException {
+ this(cp.getUTF8Index(Attribute.RuntimeVisibleTypeAnnotations), annotations);
+ }
+
+ public RuntimeVisibleTypeAnnotations_attribute(int name_index, TypeAnnotation[] annotations) {
+ super(name_index, annotations);
+ }
+
+ public This is NOT part of any supported API.
+ * If you write code that depends on this, you do so at your own risk.
+ * This code and its internal interfaces are subject to change or
+ * deletion without notice.
+ */
+public class TypeAnnotation {
+ TypeAnnotation(ClassReader cr) throws IOException, Annotation.InvalidAnnotation {
+ constant_pool = cr.getConstantPool();
+ position = read_position(cr);
+ annotation = new Annotation(cr);
+ }
+
+ public TypeAnnotation(ConstantPool constant_pool,
+ Annotation annotation, Position position) {
+ this.constant_pool = constant_pool;
+ this.position = position;
+ this.annotation = annotation;
+ }
+
+ public int length() {
+ int n = annotation.length();
+ n += position_length(position);
+ return n;
+ }
+
+ @Override
+ public String toString() {
+ try {
+ return "@" + constant_pool.getUTF8Value(annotation.type_index).toString().substring(1) +
+ " pos: " + position.toString();
+ } catch (Exception e) {
+ e.printStackTrace();
+ return e.toString();
+ }
+ }
+
+ public final ConstantPool constant_pool;
+ public final Position position;
+ public final Annotation annotation;
+
+ private static Position read_position(ClassReader cr) throws IOException, Annotation.InvalidAnnotation {
+ // Copied from ClassReader
+ int tag = cr.readUnsignedByte(); // TargetType tag is a byte
+ if (!TargetType.isValidTargetTypeValue(tag))
+ throw new Annotation.InvalidAnnotation("TypeAnnotation: Invalid type annotation target type value: " + String.format("0x%02X", tag));
+
+ TargetType type = TargetType.fromTargetTypeValue(tag);
+
+ Position position = new Position();
+ position.type = type;
+
+ switch (type) {
+ // type cast
+ case CAST:
+ // instanceof
+ case INSTANCEOF:
+ // new expression
+ case NEW:
+ position.offset = cr.readUnsignedShort();
+ break;
+ // local variable
+ case LOCAL_VARIABLE:
+ // resource variable
+ case RESOURCE_VARIABLE:
+ int table_length = cr.readUnsignedShort();
+ position.lvarOffset = new int[table_length];
+ position.lvarLength = new int[table_length];
+ position.lvarIndex = new int[table_length];
+ for (int i = 0; i < table_length; ++i) {
+ position.lvarOffset[i] = cr.readUnsignedShort();
+ position.lvarLength[i] = cr.readUnsignedShort();
+ position.lvarIndex[i] = cr.readUnsignedShort();
+ }
+ break;
+ // exception parameter
+ case EXCEPTION_PARAMETER:
+ position.exception_index = cr.readUnsignedByte();
+ break;
+ // method receiver
+ case METHOD_RECEIVER:
+ // Do nothing
+ break;
+ // type parameter
+ case CLASS_TYPE_PARAMETER:
+ case METHOD_TYPE_PARAMETER:
+ position.parameter_index = cr.readUnsignedByte();
+ break;
+ // type parameter bound
+ case CLASS_TYPE_PARAMETER_BOUND:
+ case METHOD_TYPE_PARAMETER_BOUND:
+ position.parameter_index = cr.readUnsignedByte();
+ position.bound_index = cr.readUnsignedByte();
+ break;
+ // class extends or implements clause
+ case CLASS_EXTENDS:
+ int in = cr.readUnsignedShort();
+ if (in == 0xFFFF)
+ in = -1;
+ position.type_index = in;
+ break;
+ // throws
+ case THROWS:
+ position.type_index = cr.readUnsignedShort();
+ break;
+ // method parameter
+ case METHOD_FORMAL_PARAMETER:
+ position.parameter_index = cr.readUnsignedByte();
+ break;
+ // method/constructor/reference type argument
+ case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
+ case METHOD_INVOCATION_TYPE_ARGUMENT:
+ case METHOD_REFERENCE_TYPE_ARGUMENT:
+ position.offset = cr.readUnsignedShort();
+ position.type_index = cr.readUnsignedByte();
+ break;
+ // We don't need to worry about these
+ case METHOD_RETURN:
+ case FIELD:
+ break;
+ // lambda formal parameter
+ case LAMBDA_FORMAL_PARAMETER:
+ position.parameter_index = cr.readUnsignedByte();
+ break;
+ case UNKNOWN:
+ throw new AssertionError("TypeAnnotation: UNKNOWN target type should never occur!");
+ default:
+ throw new AssertionError("TypeAnnotation: Unknown target type: " + type);
+ }
+
+ { // Write type path
+ int len = cr.readUnsignedByte();
+ List This is NOT part of any supported API. If you write code that depends
* on this, you do so at your own risk. This code and its internal interfaces
@@ -67,14 +67,21 @@ import static com.sun.tools.javac.code.Kinds.PCK;
*/
public class Annotations {
- private static final List
+ *
+ * Note that this method sets {@code mode} to {@code TYPE} first, before
+ * parsing annotations.
+ */
public JCExpression parseType() {
+ List
+ *
+ * annotations
is the list of annotations targeting
+ * the expression t
.
*/
- private JCExpression bracketsOpt(JCExpression t) {
+ private JCExpression bracketsOpt(JCExpression t,
+ List> dimAnnotations = ListBuffer.lb();
+ dimAnnotations.append(annos);
+
dims.append(parseExpression());
accept(RBRACKET);
- while (token.kind == LBRACKET) {
+ while (token.kind == LBRACKET
+ || token.kind == MONKEYS_AT) {
+ List