提交 ab6d3e9c 编写于 作者: D darcy

6961571: Update visitors to support ARM's ElementKind.RESOURCE_VARIABLE

Reviewed-by: jjg
上级 c2bfaaf2
/*
* Copyright (c) 2010, 2011, 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 javax.lang.model.type;
import java.util.List;
/**
* Represents a disjunctive type.
*
* As of the {@link javax.lang.model.SourceVersion#RELEASE_7
* RELEASE_7} source version, disjunctive types can appear as the type
* of a multi-catch exception parameter.
*
* @since 1.7
*/
public interface DisjunctiveType extends TypeMirror {
/**
* Return the alternatives comprising this disjunctive type.
*
* The alternatives are formally referred to as <i>disjuncts</i>.
*
* @return the alternatives comprising this disjunctive type.
*/
List<? extends TypeMirror> getAlternatives();
}
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2011, 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
......@@ -137,7 +137,14 @@ public enum TypeKind {
* An implementation-reserved type.
* This is not the type you are looking for.
*/
OTHER;
OTHER,
/**
* A disjunctive type.
*
* @since 1.7
*/
DISJUNCTIVE;
/**
* Returns {@code true} if this kind corresponds to a primitive
......
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2011, 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
......@@ -162,4 +162,14 @@ public interface TypeVisitor<R, P> {
* a visitor implementation may optionally throw this exception
*/
R visitUnknown(TypeMirror t, P p);
/**
* Visits a disjunctive type.
*
* @param t the type to visit
* @param p a visitor-specified parameter
* @return a visitor-specified result
* @since 1.7
*/
R visitDisjunctive(DisjunctiveType t, P p);
}
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2011, 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
......@@ -29,7 +29,8 @@ import javax.lang.model.type.*;
/**
* A skeletal visitor of types with default behavior appropriate for
* the version 6 language level.
* the {@link javax.lang.model.SourceVersion#RELEASE_6 RELEASE_6}
* source version.
*
* <p> <b>WARNING:</b> The {@code TypeVisitor} interface implemented
* by this class may have methods added to it in the future to
......@@ -94,6 +95,20 @@ public abstract class AbstractTypeVisitor6<R, P> implements TypeVisitor<R, P> {
return t.accept(this, null);
}
/**
* Visits a {@code DisjunctiveType} element by calling {@code
* visitUnknown}.
* @param t {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of {@code visitUnknown}
*
* @since 1.7
*/
public R visitDisjunctive(DisjunctiveType t, P p) {
return visitUnknown(t, p);
}
/**
* {@inheritDoc}
*
......
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2011 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
......@@ -29,7 +29,8 @@ import javax.lang.model.type.*;
/**
* A skeletal visitor of types with default behavior appropriate for
* the version 7 language level.
* the {@link javax.lang.model.SourceVersion#RELEASE_7 RELEASE_7}
* source version.
*
* <p> <b>WARNING:</b> The {@code TypeVisitor} interface implemented
* by this class may have methods added to it in the future to
......@@ -64,4 +65,13 @@ public abstract class AbstractTypeVisitor7<R, P> extends AbstractTypeVisitor6<R,
protected AbstractTypeVisitor7() {
super();
}
/**
* Visits a {@code DisjunctiveType} in a manner defined by a subclass.
*
* @param t {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of the visit as defined by a subclass
*/
public abstract R visitDisjunctive(DisjunctiveType t, P p);
}
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2011, 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
......@@ -199,7 +199,8 @@ public class ElementKindVisitor6<R, P>
* Visits a variable element, dispatching to the visit method for
* the specific {@linkplain ElementKind kind} of variable, {@code
* ENUM_CONSTANT}, {@code EXCEPTION_PARAMETER}, {@code FIELD},
* {@code LOCAL_VARIABLE}, or {@code PARAMETER}.
* {@code LOCAL_VARIABLE}, {@code PARAMETER}, or {@code RESOURCE_VARIABLE}.
*
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of the kind-specific visit method
......@@ -223,10 +224,12 @@ public class ElementKindVisitor6<R, P>
case PARAMETER:
return visitVariableAsParameter(e, p);
case RESOURCE_VARIABLE:
return visitVariableAsResourceVariable(e, p);
default:
throw new AssertionError("Bad kind " + k + " for VariableElement" + e);
}
}
/**
......@@ -289,6 +292,20 @@ public class ElementKindVisitor6<R, P>
return defaultAction(e, p);
}
/**
* Visits a {@code RESOURCE_VARIABLE} variable element by calling
* {@code visitUnknown}.
*
* @param e the element to visit
* @param p a visitor-specified parameter
* @return the result of {@code visitUnknown}
*
* @since 1.7
*/
public R visitVariableAsResourceVariable(VariableElement e, P p) {
return visitUnknown(e, p);
}
/**
* Visits an executable element, dispatching to the visit method
* for the specific {@linkplain ElementKind kind} of executable,
......
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2011 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
......@@ -34,7 +34,7 @@ import javax.lang.model.SourceVersion;
/**
* A visitor of program elements based on their {@linkplain
* ElementKind kind} with default behavior appropriate for the {@link
* SourceVersion#RELEASE_6 RELEASE_6} source version. For {@linkplain
* SourceVersion#RELEASE_7 RELEASE_7} source version. For {@linkplain
* Element elements} <tt><i>XYZ</i></tt> that may have more than one
* kind, the <tt>visit<i>XYZ</i></tt> methods in this class delegate
* to the <tt>visit<i>XYZKind</i></tt> method corresponding to the
......@@ -94,4 +94,17 @@ public class ElementKindVisitor7<R, P> extends ElementKindVisitor6<R, P> {
protected ElementKindVisitor7(R defaultValue) {
super(defaultValue);
}
/**
* Visits a {@code RESOURCE_VARIABLE} variable element by calling
* {@code defaultAction}.
*
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of {@code defaultAction}
*/
@Override
public R visitVariableAsResourceVariable(VariableElement e, P p) {
return defaultAction(e, p);
}
}
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2011, 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
......@@ -152,8 +152,8 @@ public class ElementScanner6<R, P> extends AbstractElementVisitor6<R, P> {
/**
* {@inheritDoc} This implementation scans the enclosed elements.
*
* @param e the element to visit
* @param p a visitor-specified parameter
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of scanning
*/
public R visitPackage(PackageElement e, P p) {
......@@ -163,8 +163,8 @@ public class ElementScanner6<R, P> extends AbstractElementVisitor6<R, P> {
/**
* {@inheritDoc} This implementation scans the enclosed elements.
*
* @param e the element to visit
* @param p a visitor-specified parameter
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of scanning
*/
public R visitType(TypeElement e, P p) {
......@@ -172,21 +172,28 @@ public class ElementScanner6<R, P> extends AbstractElementVisitor6<R, P> {
}
/**
* {@inheritDoc} This implementation scans the enclosed elements.
* {@inheritDoc}
*
* This implementation scans the enclosed elements, unless the
* element is a {@code RESOURCE_VARIABLE} in which case {@code
* visitUnknown} is called.
*
* @param e the element to visit
* @param p a visitor-specified parameter
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of scanning
*/
public R visitVariable(VariableElement e, P p) {
return scan(e.getEnclosedElements(), p);
if (e.getKind() != ElementKind.RESOURCE_VARIABLE)
return scan(e.getEnclosedElements(), p);
else
return visitUnknown(e, p);
}
/**
* {@inheritDoc} This implementation scans the parameters.
*
* @param e the element to visit
* @param p a visitor-specified parameter
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of scanning
*/
public R visitExecutable(ExecutableElement e, P p) {
......@@ -196,8 +203,8 @@ public class ElementScanner6<R, P> extends AbstractElementVisitor6<R, P> {
/**
* {@inheritDoc} This implementation scans the enclosed elements.
*
* @param e the element to visit
* @param p a visitor-specified parameter
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of scanning
*/
public R visitTypeParameter(TypeParameterElement e, P p) {
......
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2011, 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
......@@ -105,4 +105,16 @@ public class ElementScanner7<R, P> extends ElementScanner6<R, P> {
protected ElementScanner7(R defaultValue){
super(defaultValue);
}
/**
* This implementation scans the enclosed elements.
*
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of scanning
*/
@Override
public R visitVariable(VariableElement e, P p) {
return scan(e.getEnclosedElements(), p);
}
}
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2011, 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
......@@ -36,8 +36,8 @@ import javax.annotation.processing.SupportedSourceVersion;
/**
* A simple visitor for annotation values with default behavior
* appropriate for the {@link SourceVersion#RELEASE_7 RELEASE_7}
* source version. Visit methods call {@link
* #defaultAction} passing their arguments to {@code defaultAction}'s
* source version. Visit methods call {@link #defaultAction
* defaultAction} passing their arguments to {@code defaultAction}'s
* corresponding parameters.
*
* <p> Methods in this class may be overridden subject to their
......
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2011, 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
......@@ -38,8 +38,11 @@ import static javax.lang.model.SourceVersion.*;
* source version.
*
* Visit methods corresponding to {@code RELEASE_6} language
* constructs call {@link #defaultAction}, passing their arguments to
* {@code defaultAction}'s corresponding parameters.
* constructs call {@link #defaultAction defaultAction}, passing their
* arguments to {@code defaultAction}'s corresponding parameters.
*
* For constructs introduced in {@code RELEASE_7} and later, {@code
* visitUnknown} is called instead.
*
* <p> Methods in this class may be overridden subject to their
* general contract. Note that annotating methods in concrete
......@@ -137,14 +140,21 @@ public class SimpleElementVisitor6<R, P> extends AbstractElementVisitor6<R, P> {
}
/**
* {@inheritDoc} This implementation calls {@code defaultAction}.
* {@inheritDoc}
*
* This implementation calls {@code defaultAction}, unless the
* element is a {@code RESOURCE_VARIABLE} in which case {@code
* visitUnknown} is called.
*
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of {@code defaultAction}
* @return the result of {@code defaultAction} or {@code visitUnknown}
*/
public R visitVariable(VariableElement e, P p) {
return defaultAction(e, p);
if (e.getKind() != ElementKind.RESOURCE_VARIABLE)
return defaultAction(e, p);
else
return visitUnknown(e, p);
}
/**
......
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2011, 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
......@@ -36,9 +36,10 @@ import static javax.lang.model.SourceVersion.*;
* appropriate for the {@link SourceVersion#RELEASE_7 RELEASE_7}
* source version.
*
* Visit methods corresponding to {@code RELEASE_7} language
* constructs call {@link #defaultAction}, passing their arguments to
* {@code defaultAction}'s corresponding parameters.
* Visit methods corresponding to {@code RELEASE_7} and earlier
* language constructs call {@link #defaultAction defaultAction},
* passing their arguments to {@code defaultAction}'s corresponding
* parameters.
*
* <p> Methods in this class may be overridden subject to their
* general contract. Note that annotating methods in concrete
......@@ -89,4 +90,16 @@ public class SimpleElementVisitor7<R, P> extends SimpleElementVisitor6<R, P> {
protected SimpleElementVisitor7(R defaultValue){
super(defaultValue);
}
/**
* This implementation calls {@code defaultAction}.
*
* @param e {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of {@code defaultAction}
*/
@Override
public R visitVariable(VariableElement e, P p) {
return defaultAction(e, p);
}
}
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2011, 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
......@@ -36,8 +36,11 @@ import static javax.lang.model.SourceVersion.*;
* {@link SourceVersion#RELEASE_6 RELEASE_6} source version.
*
* Visit methods corresponding to {@code RELEASE_6} language
* constructs call {@link #defaultAction}, passing their arguments to
* {@code defaultAction}'s corresponding parameters.
* constructs call {@link #defaultAction defaultAction}, passing their
* arguments to {@code defaultAction}'s corresponding parameters.
*
* For constructs introduced in {@code RELEASE_7} and later, {@code
* visitUnknown} is called instead.
*
* <p> Methods in this class may be overridden subject to their
* general contract. Note that annotating methods in concrete
......
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2011, 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
......@@ -34,9 +34,10 @@ import static javax.lang.model.SourceVersion.*;
* A simple visitor of types with default behavior appropriate for the
* {@link SourceVersion#RELEASE_7 RELEASE_7} source version.
*
* Visit methods corresponding to {@code RELEASE_7} language
* constructs call {@link #defaultAction}, passing their arguments to
* {@code defaultAction}'s corresponding parameters.
* Visit methods corresponding to {@code RELEASE_7} and earlier
* language constructs call {@link #defaultAction defaultAction},
* passing their arguments to {@code defaultAction}'s corresponding
* parameters.
*
* <p> Methods in this class may be overridden subject to their
* general contract. Note that annotating methods in concrete
......@@ -88,4 +89,17 @@ public class SimpleTypeVisitor7<R, P> extends SimpleTypeVisitor6<R, P> {
protected SimpleTypeVisitor7(R defaultValue){
super(defaultValue);
}
/**
* This implementation visits a {@code DisjunctiveType} by calling
* {@code defaultAction}.
*
* @param t {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of {@code defaultAction}
*/
@Override
public R visitDisjunctive(DisjunctiveType t, P p) {
return defaultAction(t, p);
}
}
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2011, 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
......@@ -92,4 +92,17 @@ public class TypeKindVisitor7<R, P> extends TypeKindVisitor6<R, P> {
protected TypeKindVisitor7(R defaultValue) {
super(defaultValue);
}
/**
* This implementation visits a {@code DisjunctiveType} by calling
* {@code defaultAction}.
*
* @param t {@inheritDoc}
* @param p {@inheritDoc}
* @return the result of {@code defaultAction}
*/
@Override
public R visitDisjunctive(DisjunctiveType t, P p) {
return defaultAction(t, p);
}
}
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2011, 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
......
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2011, 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
......@@ -23,7 +23,7 @@
/*
* @test
* @bug 6911256 6964740 6967842
* @bug 6911256 6964740 6967842 6961571
* @summary Test that the resource variable kind is appropriately set
* @author Joseph D. Darcy
* @library ../../../lib
......@@ -31,8 +31,6 @@
* @compile -processor TestResourceVariable -proc:only TestResourceVariable.java
*/
// Bug should be filed for this misbehavior
import java.io.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
......@@ -82,6 +80,33 @@ public class TestResourceVariable extends JavacTestingAbstractProcessor implemen
try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {}
}
/**
* Verify that a resource variable modeled as an element behaves
* as expected under 6 and 7 specific visitors.
*/
private static void testResourceVariable(Element element) {
ElementVisitor visitor6 = new ElementKindVisitor6<Void, Void>() {};
try {
visitor6.visit(element);
throw new RuntimeException("Expected UnknownElementException not thrown.");
} catch (UnknownElementException uee) {
; // Expected.
}
ElementKindVisitor7 visitor7 = new ElementKindVisitor7<Object, Void>() {
@Override
public Object visitVariableAsResourceVariable(VariableElement e,
Void p) {
return e; // a non-null value
}
};
if (visitor7.visit(element) == null) {
throw new RuntimeException("Null result of resource variable visitation.");
}
}
class ResourceVariableScanner extends TreeScanner<Void, CompilationUnitTree> {
private Trees trees;
......@@ -92,17 +117,14 @@ public class TestResourceVariable extends JavacTestingAbstractProcessor implemen
@Override
public Void visitVariable(VariableTree node, CompilationUnitTree cu) {
Element element = trees.getElement(trees.getPath(cu, node));
if (element == null) {
System.out.println("Null variable element: " + node);
} else {
System.out.println("Name: " + element.getSimpleName() +
"\tKind: " + element.getKind());
}
if (element != null &&
element.getKind() == ElementKind.RESOURCE_VARIABLE) {
System.out.println("Name: " + element.getSimpleName() +
"\tKind: " + element.getKind());
if (element.getKind() == ElementKind.RESOURCE_VARIABLE) {
testResourceVariable(element);
resourceVariableCount++;
}
return super.visitVariable(node, cu);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册