| 1 | /******************************************************************************* | 
| 2 |  * Copyright (c) 2008 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.pde.api.tools.internal.builder; | 
| 12 |   | 
| 13 | import java.util.Set; | 
| 14 |   | 
| 15 | import org.eclipse.core.runtime.CoreException; | 
| 16 | import org.eclipse.jdt.core.Flags; | 
| 17 | import org.eclipse.pde.api.tools.internal.provisional.builder.IReference; | 
| 18 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; | 
| 19 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiMember; | 
| 20 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiType; | 
| 21 | import org.eclipse.pde.api.tools.internal.util.Signatures; | 
| 22 |   | 
| 23 |   | 
| 24 | /** | 
| 25 |  * Leak detectors keep track of all pre-requisite non-API package names to weed out | 
| 26 |  * public references. | 
| 27 |  *  | 
| 28 |  * @since 1.1 | 
| 29 |  * @noextend This class is not intended to be subclassed by clients. | 
| 30 |  */ | 
| 31 | public abstract class AbstractLeakProblemDetector extends AbstractProblemDetector { | 
| 32 |   | 
| 33 |         private Set fNonApiPackageNames; | 
| 34 |          | 
| 35 |         public AbstractLeakProblemDetector(Set nonApiPackageNames) { | 
| 36 |                 fNonApiPackageNames = nonApiPackageNames; | 
| 37 |         } | 
| 38 |          | 
| 39 |         /** | 
| 40 |          * Returns whether the referenced type name matches a non-API package. | 
| 41 |          *  | 
| 42 |          * @param reference | 
| 43 |          * @return whether the referenced type name matches a non-API package | 
| 44 |          */ | 
| 45 |         protected boolean isNonAPIReference(IReference reference) { | 
| 46 |                 String packageName = Signatures.getPackageName(reference.getReferencedTypeName()); | 
| 47 |                 if (fNonApiPackageNames.contains(packageName)) { | 
| 48 |                         return true; | 
| 49 |                 } | 
| 50 |                 // could be a reference to a package visible type | 
| 51 |                 IApiMember member = reference.getMember(); | 
| 52 |                 IApiType type = null; | 
| 53 |                 if (member.getType() == IApiElement.TYPE) { | 
| 54 |                         type = (IApiType) member; | 
| 55 |                 } else { | 
| 56 |                         type = (IApiType) member.getAncestor(IApiElement.TYPE); | 
| 57 |                 } | 
| 58 |                 String origin = Signatures.getPackageName(type.getName()); | 
| 59 |                 if (packageName.equals(origin)) { | 
| 60 |                         return true; // possible package visible reference | 
| 61 |                 } | 
| 62 |                 return false; | 
| 63 |         } | 
| 64 |          | 
| 65 |         /** | 
| 66 |          * Returns whether all enclosing types of the given member are visible. | 
| 67 |          *  | 
| 68 |          * @param member member | 
| 69 |          * @return whether all enclosing types of the given member are visible | 
| 70 |          * @throws CoreException | 
| 71 |          */ | 
| 72 |         protected boolean isEnclosingTypeVisible(IApiMember member) throws CoreException { | 
| 73 |                 IApiType type = null; | 
| 74 |                 if (member.getType() == IApiElement.TYPE) { | 
| 75 |                         type = (IApiType) member; | 
| 76 |                 } else { | 
| 77 |                         type = member.getEnclosingType(); | 
| 78 |                 } | 
| 79 |                 while (type != null) { | 
| 80 |                         if (((Flags.AccPublic | Flags.AccProtected) & type.getModifiers()) == 0) { | 
| 81 |                                 // the type is private or default protection, do not retain the reference | 
| 82 |                                 return false; | 
| 83 |                         } | 
| 84 |                         type = type.getEnclosingType(); | 
| 85 |                 } | 
| 86 |                 return true; | 
| 87 |         }         | 
| 88 | } |