| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009 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.search; |
| 12 | |
| 13 | import java.util.Set; |
| 14 | |
| 15 | import org.eclipse.core.runtime.CoreException; |
| 16 | import org.eclipse.pde.api.tools.internal.provisional.IApiAnnotations; |
| 17 | import org.eclipse.pde.api.tools.internal.provisional.VisibilityModifiers; |
| 18 | import org.eclipse.pde.api.tools.internal.provisional.builder.IReference; |
| 19 | import org.eclipse.pde.api.tools.internal.provisional.comparator.ApiScope; |
| 20 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
| 21 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiElement; |
| 22 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiMember; |
| 23 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiScope; |
| 24 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiType; |
| 25 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer; |
| 26 | import org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor; |
| 27 | |
| 28 | /** |
| 29 | * Default implementation of an {@link IApiSearchRequestor} to use with the |
| 30 | * {@link ApiSearchEngine}. This requestor returns a search scope |
| 31 | * composed of the dependent (visible) {@link IApiComponent}s for the given |
| 32 | * {@link IApiElement} |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | public class UseSearchRequestor implements IApiSearchRequestor { |
| 37 | |
| 38 | /** |
| 39 | * The backing elements to search with |
| 40 | */ |
| 41 | private Set fComponentIds = null; |
| 42 | |
| 43 | /** |
| 44 | * The mask to use while searching |
| 45 | */ |
| 46 | private int fSearchMask = 0; |
| 47 | |
| 48 | /** |
| 49 | * The search scope for this requestor |
| 50 | */ |
| 51 | private IApiScope fScope = null; |
| 52 | |
| 53 | /** |
| 54 | * Patterns for jar API type roots to not scan |
| 55 | */ |
| 56 | private String[] jarPatterns = null; |
| 57 | |
| 58 | /** |
| 59 | * Constructor |
| 60 | * @param elements an array of {@link IApiElement}s for the search engine to use |
| 61 | * @param scope the raw list of {@link IApiElement}s to extract references from |
| 62 | * @param searchkinds the kinds of references to search for. |
| 63 | * <br>Options include: |
| 64 | * <ol> |
| 65 | * <li>{@link #INCLUDE_API}</li> |
| 66 | * <li>{@link #INCLUDE_INTERNAL}</li> |
| 67 | * </ol> |
| 68 | */ |
| 69 | public UseSearchRequestor(Set/*<String>*/ elementnames, IApiElement[] scope, int searchkinds) { |
| 70 | fSearchMask = searchkinds; |
| 71 | fComponentIds = elementnames; |
| 72 | prepareScope(scope); |
| 73 | } |
| 74 | |
| 75 | /* (non-Javadoc) |
| 76 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor#acceptComponent(org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent) |
| 77 | */ |
| 78 | public boolean acceptComponent(IApiComponent component) { |
| 79 | return true; //fComponentIds.contains(component); |
| 80 | } |
| 81 | |
| 82 | /* (non-Javadoc) |
| 83 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor#acceptContainer(org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeContainer) |
| 84 | */ |
| 85 | public boolean acceptContainer(IApiTypeContainer container) { |
| 86 | return considerTypeContainer(container); |
| 87 | } |
| 88 | |
| 89 | /* (non-Javadoc) |
| 90 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor#acceptMember(org.eclipse.pde.api.tools.internal.provisional.model.IApiMember) |
| 91 | */ |
| 92 | public boolean acceptMember(IApiMember member) { |
| 93 | // don't consider inner types, as they are considered with the root type |
| 94 | switch(member.getType()) { |
| 95 | case IApiElement.TYPE: { |
| 96 | IApiType type = (IApiType) member; |
| 97 | return !(type.isMemberType() || type.isLocal()); |
| 98 | } |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns if the given {@link IApiTypeContainer} should be processed |
| 105 | * @param container |
| 106 | * @return true if the container should be processed false otherwise |
| 107 | */ |
| 108 | boolean considerTypeContainer(IApiTypeContainer container) { |
| 109 | if(jarPatterns != null && container != null) { |
| 110 | if(container.getContainerType() == IApiTypeContainer.ARCHIVE) { |
| 111 | String[] pparts = null; |
| 112 | for (int i = 0; i < jarPatterns.length; i++) { |
| 113 | pparts = jarPatterns[i].split(":"); //$NON-NLS-1$ |
| 114 | if(pparts.length != 2) { |
| 115 | continue; |
| 116 | } |
| 117 | if(container.getApiComponent().getId().equals(pparts[0])) { |
| 118 | if(container.getName().endsWith(pparts[1])) { |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | /* (non-Javadoc) |
| 129 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor#acceptReference(org.eclipse.pde.api.tools.internal.provisional.builder.IReference) |
| 130 | */ |
| 131 | public boolean acceptReference(IReference reference) { |
| 132 | try { |
| 133 | IApiMember member = reference.getResolvedReference(); |
| 134 | if(member != null) { |
| 135 | IApiComponent component = member.getApiComponent(); |
| 136 | if(!fComponentIds.contains(component.getId())) { |
| 137 | return false; |
| 138 | } |
| 139 | if(component.equals(reference.getMember().getApiComponent())) { |
| 140 | return false; |
| 141 | } |
| 142 | if(includesAPI() && includesInternal()) { |
| 143 | return true; |
| 144 | } |
| 145 | IApiAnnotations annots = component.getApiDescription().resolveAnnotations(member.getHandle()); |
| 146 | if(annots != null) { |
| 147 | int vis = annots.getVisibility(); |
| 148 | if(VisibilityModifiers.isAPI(vis) && includesAPI()) { |
| 149 | return true; |
| 150 | } |
| 151 | else if(VisibilityModifiers.isPrivate(vis) && includesInternal()) { |
| 152 | return true; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | catch(CoreException ce) { |
| 158 | //ApiPlugin.log(ce); |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | /* (non-Javadoc) |
| 164 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor#getReferenceKinds() |
| 165 | */ |
| 166 | public int getReferenceKinds() { |
| 167 | int kinds = IReference.MASK_REF_ALL & ~IReference.REF_CONSTANTPOOL; |
| 168 | return kinds; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Prepares the search scope based on the available entries in the constructor |
| 173 | * @param elements |
| 174 | */ |
| 175 | private void prepareScope(IApiElement[] elements) { |
| 176 | if(elements != null) { |
| 177 | fScope = new ApiScope(); |
| 178 | for(int i = 0; i < elements.length; i++) { |
| 179 | fScope.addElement(elements[i].getApiComponent()); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* (non-Javadoc) |
| 185 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor#getScope() |
| 186 | */ |
| 187 | public IApiScope getScope() { |
| 188 | return fScope; |
| 189 | } |
| 190 | |
| 191 | /* (non-Javadoc) |
| 192 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor#includesAPI() |
| 193 | */ |
| 194 | public boolean includesAPI() { |
| 195 | return (fSearchMask & INCLUDE_API) > 0; |
| 196 | } |
| 197 | |
| 198 | /* (non-Javadoc) |
| 199 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiSearchRequestor#includesInternal() |
| 200 | */ |
| 201 | public boolean includesInternal() { |
| 202 | return (fSearchMask & INCLUDE_INTERNAL) > 0; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * The patterns for jar names to exclude from the search |
| 207 | * @param patterns |
| 208 | */ |
| 209 | public void setJarPatterns(String[] patterns) { |
| 210 | jarPatterns = patterns; |
| 211 | } |
| 212 | } |