test/tools/javac/processing/model/element/TypeParamBounds.java

changeset 1
9a66ca7c79fa
child 495
fe17a9dbef03
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/model/element/TypeParamBounds.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,109 @@
     1.4 +/*
     1.5 + * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug     6423972
    1.30 + * @summary Tests TypeParameter.getBounds.
    1.31 + * @author  Scott Seligman
    1.32 + * @build   TypeParamBounds
    1.33 + * @compile -processor TypeParamBounds -proc:only TypeParamBounds.java
    1.34 + */
    1.35 +
    1.36 +import java.util.HashMap;
    1.37 +import java.util.List;
    1.38 +import java.util.Map;
    1.39 +import java.util.Set;
    1.40 +import javax.annotation.processing.*;
    1.41 +import javax.lang.model.SourceVersion;
    1.42 +import javax.lang.model.element.*;
    1.43 +import javax.lang.model.type.*;
    1.44 +import javax.lang.model.util.*;
    1.45 +
    1.46 +@SupportedSourceVersion(SourceVersion.RELEASE_6)
    1.47 +@SupportedAnnotationTypes("*")
    1.48 +public class TypeParamBounds extends AbstractProcessor {
    1.49 +
    1.50 +    Elements elements;
    1.51 +    Types types;
    1.52 +
    1.53 +    public void init(ProcessingEnvironment penv) {
    1.54 +        super.init(penv);
    1.55 +        elements = penv.getElementUtils();
    1.56 +        types = penv.getTypeUtils();
    1.57 +    }
    1.58 +
    1.59 +    public boolean process(Set<? extends TypeElement> annoTypes,
    1.60 +                           RoundEnvironment round) {
    1.61 +        if (!round.processingOver())
    1.62 +            doit(annoTypes, round);
    1.63 +        return true;
    1.64 +    }
    1.65 +
    1.66 +    private void doit(Set<? extends TypeElement> annoTypes,
    1.67 +                      RoundEnvironment round) {
    1.68 +        TypeElement gen = elements.getTypeElement("TypeParamBounds.Gen");
    1.69 +
    1.70 +        // For each type parameter of Gen, compare its bounds with the
    1.71 +        // bounds that are expected.
    1.72 +        //
    1.73 +        for (TypeParameterElement tparam : gen.getTypeParameters()) {
    1.74 +            System.out.println(tparam);
    1.75 +            List<? extends TypeMirror> bounds = tparam.getBounds();
    1.76 +            String[] expected = Gen.boundNames.get(tparam + "");
    1.77 +
    1.78 +            if (bounds.size() != expected.length)
    1.79 +                throw new AssertionError();
    1.80 +            int i = 0;
    1.81 +            for (TypeMirror bound : bounds) {
    1.82 +                Name got = types.asElement(bound).getSimpleName();
    1.83 +                String shoulda = expected[i++];
    1.84 +                System.out.println("  " + got);
    1.85 +                if (!got.contentEquals(shoulda))
    1.86 +                    throw new AssertionError(shoulda);
    1.87 +            }
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +
    1.92 +    // Fodder for the processor
    1.93 +
    1.94 +    static class Gen<T, U extends Object, V extends Number, W extends U,
    1.95 +                     X extends Runnable, Y extends CharSequence & Runnable,
    1.96 +                     Z extends Object & Runnable> {
    1.97 +
    1.98 +        // The names of the bounds of each type parameter of Gen.
    1.99 +        static Map<String, String[]> boundNames =
   1.100 +                new HashMap<String, String[]>();
   1.101 +
   1.102 +        static {
   1.103 +            boundNames.put("T", new String[] {"Object"});
   1.104 +            boundNames.put("U", new String[] {"Object"});
   1.105 +            boundNames.put("V", new String[] {"Number"});
   1.106 +            boundNames.put("W", new String[] {"U"});
   1.107 +            boundNames.put("X", new String[] {"Runnable"});
   1.108 +            boundNames.put("Y", new String[] {"CharSequence", "Runnable"});
   1.109 +            boundNames.put("Z", new String[] {"Object", "Runnable"});
   1.110 +        }
   1.111 +    }
   1.112 +}

mercurial