1ed50610131c8e0a3daa837371fda34f5b19651a
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / env / AccessRestriction.java
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
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.internal.compiler.util.Util;
15
16 /**
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.
19  */
20 public class AccessRestriction {
21
22         private char[][] inclusionPatterns;
23         private char[][] exclusionPatterns;
24         protected String messageTemplate;
25         AccessRestriction furtherRestriction; // subsequent restriction
26         
27         
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;
33         }
34         /**
35          * @see java.lang.Object#equals(java.lang.Object)
36          */
37         public boolean equals(Object object) {
38                 if (this == object) 
39                         return true;
40                 if (!(object instanceof AccessRestriction))
41                         return false;
42                 AccessRestriction otherRestriction = (AccessRestriction) object;
43                 if (!this.messageTemplate.equals(otherRestriction.messageTemplate)) 
44                         return false;
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)
49                                 return false;
50                         for (int i = 0; i < length; i++) {
51                                 if (!CharOperation.equals(this.inclusionPatterns[i], otherRestriction.inclusionPatterns[i]))
52                                                 return false;
53                         }
54                 }
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)
59                                 return false;
60                         for (int i = 0; i < length; i++) {
61                                 if (!CharOperation.equals(this.exclusionPatterns[i], otherRestriction.exclusionPatterns[i]))
62                                                 return false;
63                         }
64                 }
65                 if (this.furtherRestriction != otherRestriction.furtherRestriction) {
66                         if (this.furtherRestriction == null || otherRestriction.furtherRestriction == null) 
67                                 return false;
68                         if (!this.furtherRestriction.equals(otherRestriction.furtherRestriction))
69                                 return false;
70                 }
71                 return true;
72         }
73         /**
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".
76          */
77         public AccessRestriction getViolatedRestriction(char[] targetTypeName, char[] referringTypeName) {
78                 
79                 // check local inclusion/exclusion rules
80                 if (this.inclusionPatterns != null || this.exclusionPatterns != null) {
81                         if (Util.isExcluded(targetTypeName, this.inclusionPatterns, this.exclusionPatterns, false)) {
82                                 return this;
83                         }
84                 }               
85         // then check further restrictions
86                 return this.furtherRestriction != null 
87                                                 ? this.furtherRestriction.getViolatedRestriction(targetTypeName, referringTypeName)
88                                                 : null;
89         }
90         public char[][] getExclusionPatterns() {
91                         return this.exclusionPatterns;
92         }
93         public char[][] getInclusionPatterns() {
94                         return this.inclusionPatterns;
95         }
96         /**
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"
100          */
101         public String getMessageTemplate() {
102                         return this.messageTemplate;
103         }
104         
105         public String toString() {
106                 StringBuffer buffer = new StringBuffer(20);
107                 buffer
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);
117                 }
118                 return buffer.toString();
119         }
120 }