1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.env;
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.util.Util;
17 * Definition of an access restriction rule used to flag forbidden references to non API code.
18 * A restriction can chain to further ones, the first violated restriction taking precedence.
20 public class AccessRestriction {
22 private char[][] inclusionPatterns;
23 private char[][] exclusionPatterns;
24 protected String messageTemplate;
25 AccessRestriction furtherRestriction; // subsequent restriction
28 public AccessRestriction(String messageTemplate, char[][] inclusionPatterns, char[][] exclusionPatterns, AccessRestriction furtherRestriction) {
29 this.messageTemplate = messageTemplate;
30 this.inclusionPatterns = inclusionPatterns;
31 this.exclusionPatterns = exclusionPatterns;
32 this.furtherRestriction = furtherRestriction;
35 * @see java.lang.Object#equals(java.lang.Object)
37 public boolean equals(Object object) {
40 if (!(object instanceof AccessRestriction))
42 AccessRestriction otherRestriction = (AccessRestriction) object;
43 if (!this.messageTemplate.equals(otherRestriction.messageTemplate))
45 if (this.inclusionPatterns != otherRestriction.inclusionPatterns) {
46 int length = this.inclusionPatterns == null ? 0 : this.inclusionPatterns.length;
47 int otherLength = otherRestriction.inclusionPatterns == null ? 0 : otherRestriction.inclusionPatterns.length;
48 if (length != otherLength)
50 for (int i = 0; i < length; i++) {
51 if (!CharOperation.equals(this.inclusionPatterns[i], otherRestriction.inclusionPatterns[i]))
55 if (this.exclusionPatterns != otherRestriction.exclusionPatterns) {
56 int length = this.exclusionPatterns == null ? 0 : this.exclusionPatterns.length;
57 int otherLength = otherRestriction.exclusionPatterns == null ? 0 : otherRestriction.exclusionPatterns.length;
58 if (length != otherLength)
60 for (int i = 0; i < length; i++) {
61 if (!CharOperation.equals(this.exclusionPatterns[i], otherRestriction.exclusionPatterns[i]))
65 if (this.furtherRestriction != otherRestriction.furtherRestriction) {
66 if (this.furtherRestriction == null || otherRestriction.furtherRestriction == null)
68 if (!this.furtherRestriction.equals(otherRestriction.furtherRestriction))
74 * Select the first restriction which is violated when accessing a given type, or null if no restriction applies.
75 * Type name is formed as: "java/lang/Object".
77 public AccessRestriction getViolatedRestriction(char[] targetTypeName, char[] referringTypeName) {
79 // check local inclusion/exclusion rules
80 if (this.inclusionPatterns != null || this.exclusionPatterns != null) {
81 if (Util.isExcluded(targetTypeName, this.inclusionPatterns, this.exclusionPatterns, false)) {
85 // then check further restrictions
86 return this.furtherRestriction != null
87 ? this.furtherRestriction.getViolatedRestriction(targetTypeName, referringTypeName)
90 public char[][] getExclusionPatterns() {
91 return this.exclusionPatterns;
93 public char[][] getInclusionPatterns() {
94 return this.inclusionPatterns;
97 * Returns readable description for problem reporting,
98 * message is expected to contain room for restricted type name
99 * e.g. "{0} has restricted access"
101 public String getMessageTemplate() {
102 return this.messageTemplate;
105 public String toString() {
106 StringBuffer buffer = new StringBuffer(20);
108 .append("AccessRestriction [includes:\"") //$NON-NLS-1$
109 .append(CharOperation.concatWith(this.inclusionPatterns,'/'))
110 .append("\"][excludes:\"") //$NON-NLS-1$
111 .append(CharOperation.concatWith(this.exclusionPatterns,'/'))
112 .append("\"][template:\"") //$NON-NLS-1$
113 .append(this.messageTemplate)
114 .append("\"]"); //$NON-NLS-1$
115 if (this.furtherRestriction != null) {
116 buffer.append('\n').append(this.furtherRestriction);
118 return buffer.toString();