langtools/test/tools/javac/6402516/CheckIsAccessible.java
author peterz
Fri, 28 May 2010 13:32:40 +0400
changeset 5594 3db39773da2e
parent 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 6402516
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary need Trees.getScope(TreePath)
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @build Checker CheckIsAccessible
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 * @run main CheckIsAccessible
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.source.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.source.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * Check the accessibility of items of a scope against the contents of string literals.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
public class CheckIsAccessible extends Checker {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    public static void main(String... args) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        Checker chk = new CheckIsAccessible();
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        chk.check("TestIsAccessible.java", "A.java");
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    protected boolean check(Scope s, String ref) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        System.err.println("checkIsAccessible: " + s + " " + s.getEnclosingClass() + " " + ref);
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        if (ref.length() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        Trees trees = getTrees();
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        String[] args = ref.split(" +", 3);
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        boolean expect = args[args.length - 1].equals("yes");
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        boolean actual;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        switch (args.length) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        case 2:
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
            TypeElement te = getTypeElement(args[0]);
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
            actual = trees.isAccessible(s, te);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
            if (actual != expect)
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
                error(s, ref, "accessible issue found: " + te + " " + actual);
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        case 3:
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
            DeclaredType site = getType(args[0]);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
            Element member = getMember(args[1]);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
            actual = trees.isAccessible(s, member, site);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            if (actual != expect)
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                error(s, ref, "accessible issue found: " + member + "@" + site + " " + actual);
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            throw new IllegalArgumentException(ref);
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        return (actual == expect);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    private TypeElement getTypeElement(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        TypeElement te = getElements().getTypeElement(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        if (te == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            throw new IllegalArgumentException("can't find element " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        return te;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    private DeclaredType getType(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        return (DeclaredType)(getTypeElement(name).asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    private Element getMember(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        int sep = name.indexOf("#");
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        String tname = name.substring(0, sep);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        String mname = name.substring(sep+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        TypeElement te = getTypeElement(tname);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        for (Element e: te.getEnclosedElements()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            if (mname.contentEquals(e.getSimpleName()))
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                return e;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        throw new IllegalArgumentException("can't find member " + mname + " in " + tname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
}