001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.env;
011:
012: import org.eclipse.jdt.core.compiler.CharOperation;
013: import org.eclipse.jdt.core.compiler.IProblem;
014:
015: /**
016: * Definition of a set of access rules used to flag forbidden references to non API code.
017: */
018: public class AccessRuleSet {
019:
020: private AccessRule[] accessRules;
021: public String[] messageTemplates;
022: public static final int MESSAGE_TEMPLATES_LENGTH = 4;
023:
024: /**
025: * Make a new set of access rules.
026: * @param accessRules the access rules to be contained by the new set
027: * @param messageTemplates a Sting[4] array specifying the messages for type,
028: * constructor, method and field access violation; each should contain as many
029: * placeholders as expected by the respective access violation message (that is,
030: * one for type and constructor, two for method and field); replaced by a
031: * default value if null.
032: */
033: public AccessRuleSet(AccessRule[] accessRules,
034: String[] messageTemplates) {
035: this .accessRules = accessRules;
036: if (messageTemplates != null
037: && messageTemplates.length == MESSAGE_TEMPLATES_LENGTH)
038: this .messageTemplates = messageTemplates;
039: else
040: this .messageTemplates = new String[] {
041: "{0}", "{0}", "{0} {1}", "{0} {1}" }; //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$
042: }
043:
044: /**
045: * @see java.lang.Object#equals(java.lang.Object)
046: */
047: public boolean equals(Object object) {
048: if (this == object)
049: return true;
050: if (!(object instanceof AccessRuleSet))
051: return false;
052: AccessRuleSet otherRuleSet = (AccessRuleSet) object;
053: if (this .messageTemplates.length != MESSAGE_TEMPLATES_LENGTH
054: || otherRuleSet.messageTemplates.length != MESSAGE_TEMPLATES_LENGTH)
055: return false; // guard
056: for (int i = 0; i < MESSAGE_TEMPLATES_LENGTH; i++)
057: if (!this .messageTemplates[i]
058: .equals(otherRuleSet.messageTemplates[i]))
059: return false;
060: int rulesLength = this .accessRules.length;
061: if (rulesLength != otherRuleSet.accessRules.length)
062: return false;
063: for (int i = 0; i < rulesLength; i++)
064: if (!this .accessRules[i]
065: .equals(otherRuleSet.accessRules[i]))
066: return false;
067: return true;
068: }
069:
070: public AccessRule[] getAccessRules() {
071: return this .accessRules;
072: }
073:
074: /**
075: * Select the first access rule which is violated when accessing a given type,
076: * or null if no 'non accessible' access rule applies.
077: * @param targetTypeFilePath the target type file path, formed as:
078: * "org/eclipse/jdt/core/JavaCore"
079: * @return the first access restriction that applies if any, null else
080: */
081: public AccessRestriction getViolatedRestriction(
082: char[] targetTypeFilePath) {
083: for (int i = 0, length = this .accessRules.length; i < length; i++) {
084: AccessRule accessRule = this .accessRules[i];
085: if (CharOperation.pathMatch(accessRule.pattern,
086: targetTypeFilePath, true/*case sensitive*/, '/')) {
087: switch (accessRule.getProblemId()) {
088: case IProblem.ForbiddenReference:
089: case IProblem.DiscouragedReference:
090: return new AccessRestriction(accessRule,
091: this .messageTemplates);
092: default:
093: return null;
094: }
095: }
096: }
097: return null;
098: }
099:
100: public String toString() {
101: return toString(true/*wrap lines*/);
102: }
103:
104: public String toString(boolean wrap) {
105: StringBuffer buffer = new StringBuffer(200);
106: buffer.append("AccessRuleSet {"); //$NON-NLS-1$
107: if (wrap)
108: buffer.append('\n');
109: for (int i = 0, length = this .accessRules.length; i < length; i++) {
110: if (wrap)
111: buffer.append('\t');
112: AccessRule accessRule = this .accessRules[i];
113: buffer.append(accessRule);
114: if (wrap)
115: buffer.append('\n');
116: else if (i < length - 1)
117: buffer.append(", "); //$NON-NLS-1$
118: }
119: buffer.append("} [templates:\""); //$NON-NLS-1$
120: for (int i = 0; i < messageTemplates.length; i++)
121: buffer.append(this .messageTemplates[i]);
122: buffer.append("\"]"); //$NON-NLS-1$
123: return buffer.toString();
124: }
125: }
|