01: /*
02: * @(#)modifiers.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import pnuts.lang.PnutsFunction;
12: import pnuts.lang.Context;
13: import pnuts.lang.Package;
14: import pnuts.lang.Executable;
15: import java.lang.reflect.Member;
16: import java.lang.reflect.Modifier;
17:
18: public class modifiers implements Executable {
19:
20: static class ModifierChecker extends PnutsFunction {
21: private int mask;
22:
23: public ModifierChecker(String name, int mask) {
24: super (name);
25: this .mask = mask;
26: }
27:
28: public boolean defined(int nargs) {
29: return nargs == 1;
30: }
31:
32: protected Object exec(Object[] args, Context context) {
33: if (args.length != 1) {
34: undefined(args, context);
35: }
36: Object target = args[0];
37: int modifiers;
38: if (target instanceof Class) {
39: modifiers = ((Class) target).getModifiers();
40: } else if (target instanceof Member) {
41: modifiers = ((Member) target).getModifiers();
42: } else {
43: throw new IllegalArgumentException();
44: }
45: if ((mask & modifiers) == mask) {
46: return Boolean.TRUE;
47: } else {
48: return Boolean.FALSE;
49: }
50: }
51: }
52:
53: static String[] names = { "isAbstract", "isPublic", "isProtected",
54: "isPrivate", "isStatic", "isInterface" };
55: static int[] modifiers = { Modifier.ABSTRACT, Modifier.PUBLIC,
56: Modifier.PROTECTED, Modifier.PRIVATE, Modifier.STATIC,
57: Modifier.INTERFACE };
58:
59: public Object run(Context context) {
60: Package pkg = Package.getPackage("pnuts.lib", context);
61: for (int i = 0; i < names.length; i++) {
62: pkg.set(names[i].intern(), new ModifierChecker(names[i],
63: modifiers[i]), context);
64: }
65: return null;
66: }
67: }
|