KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > env > AccessRuleSet


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.env;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.core.compiler.IProblem;
15
16 /**
17  * Definition of a set of access rules used to flag forbidden references to non API code.
18  */

19 public class AccessRuleSet {
20
21     private AccessRule[] accessRules;
22     public String JavaDoc[] messageTemplates;
23     public static final int MESSAGE_TEMPLATES_LENGTH = 4;
24     
25     /**
26      * Make a new set of access rules.
27      * @param accessRules the access rules to be contained by the new set
28      * @param messageTemplates a Sting[4] array specifying the messages for type,
29      * constructor, method and field access violation; each should contain as many
30      * placeholders as expected by the respective access violation message (that is,
31      * one for type and constructor, two for method and field); replaced by a
32      * default value if null.
33      */

34     public AccessRuleSet(AccessRule[] accessRules, String JavaDoc[] messageTemplates) {
35         this.accessRules = accessRules;
36         if (messageTemplates != null && messageTemplates.length == MESSAGE_TEMPLATES_LENGTH)
37             this.messageTemplates = messageTemplates;
38         else
39             this.messageTemplates = new String JavaDoc[] {"{0}", "{0}", "{0} {1}", "{0} {1}"}; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$
40
}
41     
42     /**
43      * @see java.lang.Object#equals(java.lang.Object)
44      */

45     public boolean equals(Object JavaDoc object) {
46         if (this == object)
47             return true;
48         if (!(object instanceof AccessRuleSet))
49             return false;
50         AccessRuleSet otherRuleSet = (AccessRuleSet) object;
51         if (this.messageTemplates.length != MESSAGE_TEMPLATES_LENGTH ||
52                 otherRuleSet.messageTemplates.length != MESSAGE_TEMPLATES_LENGTH)
53             return false; // guard
54
for (int i = 0; i < MESSAGE_TEMPLATES_LENGTH; i++)
55             if (!this.messageTemplates[i].equals(otherRuleSet.messageTemplates[i]))
56                 return false;
57         int rulesLength = this.accessRules.length;
58         if (rulesLength != otherRuleSet.accessRules.length) return false;
59         for (int i = 0; i < rulesLength; i++)
60             if (!this.accessRules[i].equals(otherRuleSet.accessRules[i]))
61                 return false;
62         return true;
63     }
64     
65     public AccessRule[] getAccessRules() {
66         return this.accessRules;
67     }
68     
69 /**
70  * Select the first access rule which is violated when accessing a given type,
71  * or null if no 'non accessible' access rule applies.
72  * @param targetTypeFilePath the target type file path, formed as:
73  * "org/eclipse/jdt/core/JavaCore"
74  * @return the first access restriction that applies if any, null else
75  */

76 public AccessRestriction getViolatedRestriction(char[] targetTypeFilePath) {
77     for (int i = 0, length = this.accessRules.length; i < length; i++) {
78         AccessRule accessRule = this.accessRules[i];
79         if (CharOperation.pathMatch(accessRule.pattern, targetTypeFilePath,
80                 true/*case sensitive*/, '/')) {
81             switch (accessRule.getProblemId()) {
82                 case IProblem.ForbiddenReference:
83                 case IProblem.DiscouragedReference:
84                     return new AccessRestriction(accessRule, this.messageTemplates);
85                 default:
86                     return null;
87             }
88         }
89     }
90     return null;
91 }
92     
93     public String JavaDoc toString() {
94         return toString(true/*wrap lines*/);
95     }
96     
97     public String JavaDoc toString(boolean wrap) {
98         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(200);
99         buffer.append("AccessRuleSet {"); //$NON-NLS-1$
100
if (wrap)
101             buffer.append('\n');
102         for (int i = 0, length = this.accessRules.length; i < length; i++) {
103             if (wrap)
104                 buffer.append('\t');
105             AccessRule accessRule = this.accessRules[i];
106             buffer.append(accessRule);
107             if (wrap)
108                 buffer.append('\n');
109             else if (i < length-1)
110                 buffer.append(", "); //$NON-NLS-1$
111
}
112         buffer.append("} [templates:\""); //$NON-NLS-1$
113
for (int i = 0; i < messageTemplates.length; i++)
114             buffer.append(this.messageTemplates[i]);
115         buffer.append("\"]"); //$NON-NLS-1$
116
return buffer.toString();
117     }
118 }
119
Popular Tags