|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2015 Ecliptical Software Inc. 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 | + * Ecliptical Software Inc. - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package ca.ecliptical.pde.internal.ds; |
| 12 | + |
| 13 | +import org.eclipse.core.expressions.PropertyTester; |
| 14 | +import org.eclipse.core.resources.ProjectScope; |
| 15 | +import org.eclipse.core.runtime.Platform; |
| 16 | +import org.eclipse.core.runtime.preferences.IScopeContext; |
| 17 | +import org.eclipse.core.runtime.preferences.InstanceScope; |
| 18 | +import org.eclipse.jdt.core.IAnnotation; |
| 19 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 20 | +import org.eclipse.jdt.core.IJavaElement; |
| 21 | +import org.eclipse.jdt.core.IJavaProject; |
| 22 | +import org.eclipse.jdt.core.IMemberValuePair; |
| 23 | +import org.eclipse.jdt.core.IPackageFragment; |
| 24 | +import org.eclipse.jdt.core.IType; |
| 25 | +import org.eclipse.jdt.core.JavaModelException; |
| 26 | +import org.eclipse.pde.internal.ds.core.IDSConstants; |
| 27 | +import org.osgi.service.component.annotations.Component; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests if a type (or any nested type) has a {@link Component} annotation with |
| 31 | + * no <code>name</code> attribute. |
| 32 | + */ |
| 33 | +@SuppressWarnings("restriction") |
| 34 | +public class ComponentPropertyTester extends PropertyTester { |
| 35 | + |
| 36 | + private static final String COMPONENT_ANNOTATION = DSAnnotationCompilationParticipant.COMPONENT_ANNOTATION; |
| 37 | + |
| 38 | + private static final String COMPONENT_PACKAGE = COMPONENT_ANNOTATION.substring(0, COMPONENT_ANNOTATION.lastIndexOf('.')); |
| 39 | + |
| 40 | + private static final String COMPONENT_NAME = Component.class.getSimpleName(); |
| 41 | + |
| 42 | + private static final Debug debug = Debug.getDebug("component-property-tester"); //$NON-NLS-1$ |
| 43 | + |
| 44 | + public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { |
| 45 | + if (!"containsComponentWithImplicitName".equals(property)) //$NON-NLS-1$ |
| 46 | + return false; |
| 47 | + |
| 48 | + if (!(receiver instanceof IType) && !(receiver instanceof IPackageFragment)) |
| 49 | + return false; |
| 50 | + |
| 51 | + IJavaElement element = (IJavaElement) receiver; |
| 52 | + IJavaProject javaProject = element.getJavaProject(); |
| 53 | + |
| 54 | + boolean enabled = Platform.getPreferencesService().getBoolean(Activator.PLUGIN_ID, Activator.PREF_ENABLED, true, new IScopeContext[] { new ProjectScope(javaProject.getProject()), InstanceScope.INSTANCE }); |
| 55 | + if (!enabled) |
| 56 | + return false; |
| 57 | + |
| 58 | + try { |
| 59 | + return element.getElementType() == IJavaElement.TYPE ? containsImplicitName((IType) receiver) : containsImplicitName((IPackageFragment) receiver); |
| 60 | + } catch (JavaModelException e) { |
| 61 | + if (debug.isDebugging()) |
| 62 | + debug.trace(String.format("Error searching for components with implicit names in element: %s", element), e); //$NON-NLS-1$ |
| 63 | + } |
| 64 | + |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + private boolean containsImplicitName(IPackageFragment fragment) throws JavaModelException { |
| 69 | + if (!fragment.containsJavaResources()) |
| 70 | + return false; |
| 71 | + |
| 72 | + for (ICompilationUnit cu : fragment.getCompilationUnits()) { |
| 73 | + for (IType type : cu.getAllTypes()) { |
| 74 | + if (hasImplicitName(type)) |
| 75 | + return true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + private boolean containsImplicitName(IType type) throws JavaModelException { |
| 83 | + if (hasImplicitName(type)) |
| 84 | + return true; |
| 85 | + |
| 86 | + for (IType child : type.getTypes()) { |
| 87 | + if (hasImplicitName(child)) |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + public static boolean hasImplicitName(IType type) throws JavaModelException { |
| 95 | + IAnnotation[] annotations = type.getAnnotations(); |
| 96 | + for (IAnnotation annotation : annotations) { |
| 97 | + boolean isComponent = COMPONENT_ANNOTATION.equals(annotation.getElementName()); |
| 98 | + if (!isComponent) { |
| 99 | + String[][] resolved = type.resolveType(annotation.getElementName()); |
| 100 | + if (resolved != null) { |
| 101 | + for (String[] pair : resolved) { |
| 102 | + if (pair.length == 2 |
| 103 | + && COMPONENT_PACKAGE.equals(pair[0]) |
| 104 | + && COMPONENT_NAME.equals(pair[1])) { |
| 105 | + isComponent = true; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (isComponent) { |
| 113 | + IMemberValuePair[] attrs = annotation.getMemberValuePairs(); |
| 114 | + for (IMemberValuePair attr : attrs) { |
| 115 | + if (IDSConstants.ATTRIBUTE_COMPONENT_NAME.equals(attr.getMemberName())) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return false; |
| 125 | + } |
| 126 | +} |
0 commit comments