|
Lines 860-863
Link Here
|
| 860 |
assertEqualString(preview, buf.toString()); |
860 |
assertEqualString(preview, buf.toString()); |
| 861 |
} |
861 |
} |
| 862 |
|
862 |
|
|
|
863 |
/** |
| 864 |
* ASTRewriterTests for PackageQualifiedType |
| 865 |
* @throws Exception |
| 866 |
* |
| 867 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=406469 |
| 868 |
*/ |
| 869 |
public void testPackageQualifiedTypeAnnotations() throws Exception { |
| 870 |
if (this.apiLevel < AST.JLS8) return; |
| 871 |
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test406469.bug", false, null); |
| 872 |
String contents = "package test406469.bug;\n" + |
| 873 |
"import java.lang.annotation.*;\n" + |
| 874 |
"public class X {\n" + |
| 875 |
" @Target(ElementType.TYPE_USE)\n" + |
| 876 |
" @Retention(RetentionPolicy.RUNTIME)\n" + |
| 877 |
" @Documented\n" + |
| 878 |
" static @interface NonNull { }\n" + |
| 879 |
" class Inner {}\n" + |
| 880 |
" \n" + |
| 881 |
" /**\n" + |
| 882 |
" * @param arg \n" + |
| 883 |
" */\n" + |
| 884 |
" test406469.bug.@NonNull IOException foo(\n" + |
| 885 |
" test406469.bug.@NonNull FileNotFoundException arg)\n" + |
| 886 |
" throws test406469.bug.@NonNull EOFException {\n" + |
| 887 |
" try {\n" + |
| 888 |
" test406469.bug.@NonNull IOError e = new test406469.bug.IOError();\n" + |
| 889 |
" throw e;\n" + |
| 890 |
" } catch (test406469.bug.@NonNull IOError e) {\n" + |
| 891 |
" }\n" + |
| 892 |
" return null;\n" + |
| 893 |
" } \n" + |
| 894 |
" test406469.bug.@NonNull X.@NonNull Inner fInner;\n" + |
| 895 |
"} \n" + |
| 896 |
"@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface Marker {} \n" + |
| 897 |
"@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface A {} \n" + |
| 898 |
"@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface B {} \n" + |
| 899 |
"\n" + |
| 900 |
"class Outer {\n" + |
| 901 |
" public class Inner {\n" + |
| 902 |
" public class Deeper {}\n" + |
| 903 |
" }\n" + |
| 904 |
"}\n" + |
| 905 |
"class IOException extends Exception {private static final long serialVersionUID=10001L;}\n" + |
| 906 |
"class FileNotFoundException extends Exception{private static final long serialVersionUID=10002L;}\n" + |
| 907 |
"class EOFException extends Exception{private static final long serialVersionUID=10003L;}\n" + |
| 908 |
"class IOError extends Exception{private static final long serialVersionUID=10004L;}\n"; |
| 909 |
StringBuffer buf = new StringBuffer(contents); |
| 910 |
ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null); |
| 911 |
CompilationUnit astRoot= createAST(cu, /* resolve */ true, false); |
| 912 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
| 913 |
AST ast= astRoot.getAST(); |
| 914 |
TypeDeclaration typeDeclaration= findTypeDeclaration(astRoot, "X"); |
| 915 |
MethodDeclaration methodDeclaration= findMethodDeclaration(typeDeclaration, "foo"); |
| 916 |
{ //replace an annotation. |
| 917 |
PackageQualifiedType packageQualifiedType = (PackageQualifiedType) methodDeclaration.getReturnType2(); |
| 918 |
MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation(); |
| 919 |
markerAnnotation.setTypeName(ast.newSimpleName("Marker")); |
| 920 |
rewrite.replace((ASTNode) packageQualifiedType.annotations().get(0), markerAnnotation, null); |
| 921 |
|
| 922 |
// remove an annotation |
| 923 |
SingleVariableDeclaration param = (SingleVariableDeclaration) methodDeclaration.parameters().get(0); |
| 924 |
packageQualifiedType = (PackageQualifiedType) param.getType(); |
| 925 |
rewrite.remove((ASTNode) packageQualifiedType.annotations().get(0), null); |
| 926 |
|
| 927 |
// insert an annotation |
| 928 |
packageQualifiedType = (PackageQualifiedType) methodDeclaration.thrownExceptionTypes().get(0); |
| 929 |
markerAnnotation= ast.newMarkerAnnotation(); |
| 930 |
markerAnnotation.setTypeName(ast.newSimpleName("Marker")); |
| 931 |
rewrite.getListRewrite(packageQualifiedType, PackageQualifiedType.ANNOTATIONS_PROPERTY).insertLast(markerAnnotation, null); |
| 932 |
} |
| 933 |
String preview= evaluateRewrite(cu, rewrite); |
| 934 |
String contentsmodified = "package test406469.bug;\n" + |
| 935 |
"import java.lang.annotation.*;\n" + |
| 936 |
"public class X {\n" + |
| 937 |
" @Target(ElementType.TYPE_USE)\n" + |
| 938 |
" @Retention(RetentionPolicy.RUNTIME)\n" + |
| 939 |
" @Documented\n" + |
| 940 |
" static @interface NonNull { }\n" + |
| 941 |
" class Inner {}\n" + |
| 942 |
" \n" + |
| 943 |
" /**\n" + |
| 944 |
" * @param arg \n" + |
| 945 |
" */\n" + |
| 946 |
" test406469.bug.@Marker IOException foo(\n" + |
| 947 |
" FileNotFoundException arg)\n" + |
| 948 |
" throws test406469.bug.@NonNull @Marker EOFException {\n" + |
| 949 |
" try {\n" + |
| 950 |
" test406469.bug.@NonNull IOError e = new test406469.bug.IOError();\n" + |
| 951 |
" throw e;\n" + |
| 952 |
" } catch (test406469.bug.@NonNull IOError e) {\n" + |
| 953 |
" }\n" + |
| 954 |
" return null;\n" + |
| 955 |
" } \n" + |
| 956 |
" test406469.bug.@NonNull X.@NonNull Inner fInner;\n" + |
| 957 |
"} \n" + |
| 958 |
"@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface Marker {} \n" + |
| 959 |
"@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface A {} \n" + |
| 960 |
"@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_USE) @interface B {} \n" + |
| 961 |
"\n" + |
| 962 |
"class Outer {\n" + |
| 963 |
" public class Inner {\n" + |
| 964 |
" public class Deeper {}\n" + |
| 965 |
" }\n" + |
| 966 |
"}\n" + |
| 967 |
"class IOException extends Exception {private static final long serialVersionUID=10001L;}\n" + |
| 968 |
"class FileNotFoundException extends Exception{private static final long serialVersionUID=10002L;}\n" + |
| 969 |
"class EOFException extends Exception{private static final long serialVersionUID=10003L;}\n" + |
| 970 |
"class IOError extends Exception{private static final long serialVersionUID=10004L;}\n"; |
| 971 |
assertEqualString(preview, contentsmodified); |
| 972 |
} |
| 973 |
|
| 863 |
} |
974 |
} |