List of usage examples for org.eclipse.jdt.core ILocalVariable getAnnotations
IAnnotation[] getAnnotations() throws JavaModelException;
From source file:com.ecfeed.ui.common.JavaModelAnalyser.java
License:Open Source License
public static boolean isAnnotated(ILocalVariable parameter, String annotationType) { try {//from www. jav a 2s . c o m IAnnotation[] annotations = parameter.getAnnotations(); for (IAnnotation annotation : annotations) { if (annotation.getElementName().equals(annotationType)) { return true; } } } catch (JavaModelException e) { SystemLogger.logCatch(e.getMessage()); } return false; }
From source file:com.testify.ecfeed.ui.common.JavaModelAnalyser.java
License:Open Source License
public static boolean isAnnotated(ILocalVariable parameter, String annotationType) { try {//from w w w . jav a2s . c o m IAnnotation[] annotations = parameter.getAnnotations(); for (IAnnotation annotation : annotations) { if (annotation.getElementName().equals(annotationType)) { return true; } } } catch (JavaModelException e) { } return false; }
From source file:de.gebit.integrity.ui.utils.FixtureTypeWrapper.java
License:Open Source License
/** * Replaces all values in the given parameter map with converted versions that match the types that are expected by * the given fixture method.//from w ww . j a va 2 s .c o m * * @param aFixtureMethodName * the method name * @param aParameterMap * the parameter map * @param aParameterPath * the path inside a nested object, if applicable * @param anIncludeArbitraryParametersFlag * whether arbitrary parameters shall be included * @throws JavaModelException * @throws UnexecutableException * @throws UnresolvableVariableException */ public void convertParameterValuesToFixtureDefinedTypes(String aFixtureMethodName, Map<String, Object> aParameterMap, List<String> aParameterPath, boolean anIncludeArbitraryParametersFlag) throws JavaModelException, UnresolvableVariableException, UnexecutableException { IMethod tempMethod = findMethod(aFixtureMethodName); Map<String, Object> tempFixedParamsMap = new HashMap<String, Object>(); for (ILocalVariable tempParam : tempMethod.getParameters()) { ResolvedTypeName tempParamTypeName = IntegrityDSLUIUtil .getResolvedTypeName(tempParam.getTypeSignature(), fixtureType); if (tempParamTypeName == null) { continue; } if (tempParamTypeName.getRawType().startsWith(Map.class.getName())) { // ignore the arbitrary parameter parameter } else { IAnnotation tempAnnotation = null; for (IAnnotation tempAnnotationCandidate : tempParam.getAnnotations()) { String tempCandidateName = tempAnnotationCandidate.getElementName(); if (FixtureParameter.class.getName().equals(tempCandidateName) || FixtureParameter.class.getSimpleName().equals(tempCandidateName)) { tempAnnotation = tempAnnotationCandidate; break; } } if (tempAnnotation != null) { String tempName = null; for (IMemberValuePair tempPair : tempAnnotation.getMemberValuePairs()) { if ("name".equals(tempPair.getMemberName())) { tempName = (String) tempPair.getValue(); } } if (tempName != null) { Object tempValue = aParameterMap.get(tempName); if (tempValue != null) { Class<?> tempExpectedType; try { tempExpectedType = getClass().getClassLoader() .loadClass(tempParamTypeName.getRawType()); } catch (ClassNotFoundException exc) { // we'll skip this param continue; } Object tempConvertedValue; if (tempValue instanceof Object[]) { if (!tempExpectedType.isArray()) { throw new IllegalArgumentException("The parameter '" + tempName + "' of method '" + aFixtureMethodName + "' in fixture '" + fixtureType.getFullyQualifiedName() + "' is not an array type, thus you cannot put multiple values into it!"); } Object tempConvertedValueArray = Array.newInstance( tempExpectedType.getComponentType(), ((Object[]) tempValue).length); for (int k = 0; k < ((Object[]) tempValue).length; k++) { Object tempSingleValue = ((Object[]) tempValue)[k]; Array.set(tempConvertedValueArray, k, valueConverter.convertValue( tempExpectedType.getComponentType(), tempSingleValue, null)); } tempConvertedValue = tempConvertedValueArray; } else { // if the expected type is an array, we don't want to convert to that array, but to // the // component type, of course Class<?> tempConversionTargetType = tempExpectedType.isArray() ? tempExpectedType.getComponentType() : tempExpectedType; tempConvertedValue = valueConverter.convertValue(tempConversionTargetType, tempValue, null); if (tempExpectedType.isArray()) { // ...and if the expected type is an array, now we create one Object tempNewArray = Array.newInstance(tempExpectedType.getComponentType(), 1); Array.set(tempNewArray, 0, tempConvertedValue); tempConvertedValue = tempNewArray; } } aParameterMap.put(tempName, tempConvertedValue); tempFixedParamsMap.put(tempName, tempConvertedValue); } } } } } if (anIncludeArbitraryParametersFlag && isArbitraryParameterFixtureClass()) { ArbitraryParameterEnumerator tempArbitraryParameterEnumerator = instantiateArbitraryParameterEnumerator(); List<ArbitraryParameterDefinition> tempArbitraryParameters = tempArbitraryParameterEnumerator .defineArbitraryParameters(aFixtureMethodName, tempFixedParamsMap, aParameterPath); if (tempArbitraryParameters != null) { for (ArbitraryParameterDefinition tempArbitraryParameter : tempArbitraryParameters) { String tempName = tempArbitraryParameter.getName(); Object tempValue = aParameterMap.remove(tempName); if (tempValue != null) { Object tempConvertedValue; tempConvertedValue = valueConverter.convertValue(null, tempValue, null); aParameterMap.put(tempName, tempConvertedValue); } } } } }
From source file:io.sarl.eclipse.tests.util.Jdt2EcoreTest.java
License:Apache License
/** Replies a mock of a IMethod (method or constructor). * * <table>//from w ww. jav a 2 s . c o m * <tr><td>Z</td><td>boolean</td></tr> * <tr><td>B</td><td>byte</td></tr> * <tr><td>C</td><td>char</td></tr> * <tr><td>S</td><td>short</td></tr> * <tr><td>I</td><td>int</td></tr> * <tr><td>J</td><td>long</td></tr> * <tr><td>F</td><td>float</td></tr> * <tr><td>D</td><td>double</td></tr> * <tr><td>V</td><td>void</td></tr> * <tr><td>L fully-qualified-class ;</td><td>fully-qualified-class</td></tr> * <tr><td>[ type</td><td>type[]</td></tr> * </table> */ private static IMethod createIMethodMock(boolean isConstructor, IType type, String methodName, String returnType, String[] parameterNames, String[] parameterTypes, IAnnotation[] parameterAnnotations, IAnnotation[] methodAnnotations, int flags) { try { IMethod method = AbstractSarlTest.mock(IMethod.class); when(method.getDeclaringType()).thenReturn(type); when(method.getElementName()).thenReturn(methodName); when(method.getElementType()).thenReturn(IJavaElement.METHOD); when(method.isConstructor()).thenReturn(isConstructor); IAnnotation[] ma = methodAnnotations; if (ma == null) { ma = new IAnnotation[0]; } when(method.getAnnotations()).thenReturn(ma); if (Strings.isNullOrEmpty(returnType)) { when(method.getReturnType()).thenReturn("V"); } else { when(method.getReturnType()).thenReturn(returnType); } when(method.getNumberOfParameters()).thenReturn(parameterNames.length); when(method.getParameterNames()).thenReturn(parameterNames); when(method.getParameterTypes()).thenReturn(parameterTypes); ILocalVariable[] parameters = new ILocalVariable[parameterNames.length]; for (int i = 0; i < parameterNames.length; ++i) { ILocalVariable var = AbstractSarlTest.mock(ILocalVariable.class); when(var.getElementName()).thenReturn(parameterNames[i]); when(var.getTypeSignature()).thenReturn(parameterTypes[i]); when(var.getDeclaringMember()).thenReturn(method); IAnnotation a = parameterAnnotations[i]; if (a == null) { when(var.getAnnotations()).thenReturn(new IAnnotation[0]); } else { when(var.getAnnotations()).thenReturn(new IAnnotation[] { a }); } parameters[i] = var; } when(method.getParameters()).thenReturn(parameters); when(method.getFlags()).thenReturn(flags); return method; } catch (JavaModelException exception) { throw new RuntimeException(exception); } }
From source file:org.jboss.tools.ws.jaxrs.core.jdt.JavaAnnotationLocator.java
License:Open Source License
@Override public boolean visit(SingleVariableDeclaration variableDeclaration) { // skip if parentMethod is undefined or if annotation is already located if (this.parentMethod == null || this.locatedAnnotation != null) { return false; }/*w w w .ja va 2 s . c om*/ try { if (DOMUtils.nodeMatches(variableDeclaration, location)) { final IVariableBinding variableDeclarationBinding = variableDeclaration.resolveBinding(); final IAnnotationBinding[] annotationBindings = variableDeclarationBinding.getAnnotations(); // retrieve the parameter index in the parent method final ILocalVariable localVariable = getLocalVariable(variableDeclarationBinding); if (localVariable != null) { final IAnnotation[] variableAnnotations = localVariable.getAnnotations(); for (int j = 0; j < annotationBindings.length; j++) { final IAnnotation javaAnnotation = variableAnnotations[j]; if (RangeUtils.matches(javaAnnotation.getSourceRange(), location)) { final IAnnotationBinding javaAnnotationBinding = annotationBindings[j]; this.locatedAnnotation = BindingUtils.toAnnotation(javaAnnotationBinding, javaAnnotation); break; } } } // TODO : add support for thrown exceptions } } catch (JavaModelException e) { Logger.error("Failed to analyse compilation unit method '" + this.parentMethod.getElementName() + "'", e); } // no need to carry on from here return false; }
From source file:org.jboss.tools.ws.jaxrs.core.jdt.JavaMethodSignaturesVisitor.java
License:Open Source License
@Override public boolean visit(MethodDeclaration declaration) { try {//from w w w.j av a 2 s. c om final IJavaElement element = compilationUnit.getElementAt(declaration.getStartPosition()); if (element == null || element.getElementType() != IJavaElement.METHOD) { return true; } IMethod method = (IMethod) element; if (this.method != null && !this.method.getHandleIdentifier().equals(method.getHandleIdentifier())) { return true; } final IMethodBinding methodBinding = declaration.resolveBinding(); // sometimes, the binding cannot be resolved if (methodBinding == null) { Logger.debug("Could not resolve bindings form method " + method.getElementName()); } else { final IType returnedType = getReturnType(methodBinding); //.getReturnType().getJavaElement() : null; List<JavaMethodParameter> methodParameters = new ArrayList<JavaMethodParameter>(); @SuppressWarnings("unchecked") List<SingleVariableDeclaration> parameters = declaration.parameters(); for (int i = 0; i < parameters.size(); i++) { final SingleVariableDeclaration parameter = parameters.get(i); final String paramName = parameter.getName().getFullyQualifiedName(); final IVariableBinding paramBinding = parameter.resolveBinding(); final String paramTypeName = paramBinding.getType().getQualifiedName(); final List<Annotation> paramAnnotations = new ArrayList<Annotation>(); final IAnnotationBinding[] annotationBindings = paramBinding.getAnnotations(); for (int j = 0; j < annotationBindings.length; j++) { final ILocalVariable localVariable = method.getParameters()[i]; final IAnnotation javaAnnotation = localVariable.getAnnotations()[j]; final IAnnotationBinding javaAnnotationBinding = annotationBindings[j]; paramAnnotations.add(BindingUtils.toAnnotation(javaAnnotationBinding, javaAnnotation)); } //final ISourceRange sourceRange = new SourceRange(parameter.getStartPosition(), parameter.getLength()); methodParameters.add(new JavaMethodParameter(paramName, paramTypeName, paramAnnotations)); } // TODO : add support for thrown exceptions this.methodSignatures.add(new JavaMethodSignature(method, returnedType, methodParameters)); } } catch (JavaModelException e) { Logger.error("Failed to analyse compilation unit methods", e); } return true; }
From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtils.java
License:Open Source License
/** * @return an {@link Annotation} from the given node if it is an * {@link org.eclipse.jdt.core.dom.Annotation}, or recursively calls * with the given node's parent until match, or return null * @param node//from w w w.java 2 s. c om * the current node * @param location the location in the Root {@link ASTNode} * @throws JavaModelException */ private static Annotation findAnnotation(final ASTNode node, final int location) throws JavaModelException { if (node == null) { return null; } else if (!(node instanceof org.eclipse.jdt.core.dom.Annotation)) { return findAnnotation(node.getParent(), location); } final IAnnotationBinding annotationBinding = ((org.eclipse.jdt.core.dom.Annotation) node) .resolveAnnotationBinding(); if (annotationBinding.getJavaElement() != null && annotationBinding.getJavaElement().getElementType() == IJavaElement.ANNOTATION) { return toAnnotation(annotationBinding, (IAnnotation) annotationBinding.getJavaElement()); } if (node.getParent().getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION) { final SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) node.getParent(); final IVariableBinding variableDeclarationBinding = variableDeclaration.resolveBinding(); final IAnnotationBinding[] annotationBindings = variableDeclarationBinding.getAnnotations(); // retrieve the parameter index in the parent method final IMethod parentMethod = (IMethod) variableDeclarationBinding.getDeclaringMethod().getJavaElement(); final ILocalVariable localVariable = getLocalVariable(variableDeclarationBinding, parentMethod); if (localVariable != null) { final IAnnotation[] variableAnnotations = localVariable.getAnnotations(); for (int j = 0; j < annotationBindings.length; j++) { final IAnnotation javaAnnotation = variableAnnotations[j]; if (RangeUtils.matches(javaAnnotation.getSourceRange(), location)) { final IAnnotationBinding javaAnnotationBinding = annotationBindings[j]; return toAnnotation(javaAnnotationBinding, javaAnnotation); } } } } return null; }
From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtils.java
License:Open Source License
private static List<Annotation> resolveParameterAnnotations(final ILocalVariable localVariable, final IVariableBinding paramBinding) throws JavaModelException { final List<Annotation> paramAnnotations = new ArrayList<Annotation>(); final IAnnotationBinding[] annotationBindings = paramBinding.getAnnotations(); for (int j = 0; j < annotationBindings.length; j++) { if (j < localVariable.getAnnotations().length) { final IAnnotation javaAnnotation = localVariable.getAnnotations()[j]; final IAnnotationBinding javaAnnotationBinding = annotationBindings[j]; paramAnnotations.add(JdtUtils.toAnnotation(javaAnnotationBinding, javaAnnotation)); }//w w w . j ava 2 s.c om } return paramAnnotations; }
From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtilsTestCase.java
License:Open Source License
@Test public void shouldRetrieveLocalVariableAnnotationFromNameLocation() throws CoreException { // preconditions final IType customerType = projectMonitor .resolveType("org.jboss.tools.ws.jaxrs.sample.services.CustomerResource"); final IMethod method = projectMonitor.resolveMethod(customerType, "getCustomer"); final ILocalVariable localVariable = getLocalVariable(method, "id"); final int offset = localVariable.getAnnotations()[0].getNameRange().getOffset(); // operation//from w w w . j a v a 2s . co m final Annotation foundAnnotation = JdtUtils.resolveAnnotationAt(offset, method.getCompilationUnit()); // verification assertThat(foundAnnotation, notNullValue()); assertThat(foundAnnotation.getJavaAnnotation(), notNullValue()); }
From source file:org.jboss.tools.ws.jaxrs.core.jdt.JdtUtilsTestCase.java
License:Open Source License
@Test public void shouldRetrieveLocalVariableAnnotationFromMemberPairLocation() throws CoreException { // preconditions final IType customerType = projectMonitor .resolveType("org.jboss.tools.ws.jaxrs.sample.services.CustomerResource"); final IMethod method = projectMonitor.resolveMethod(customerType, "getCustomer"); final ILocalVariable localVariable = getLocalVariable(method, "id"); final int offset = localVariable.getAnnotations()[0].getNameRange().getOffset() + "PathParam".length() + 3; // operation/*from w w w. ja v a2 s .c o m*/ final Annotation foundAnnotation = JdtUtils.resolveAnnotationAt(offset, customerType.getCompilationUnit()); // verification assertThat(foundAnnotation, notNullValue()); assertThat(foundAnnotation.getFullyQualifiedName(), equalTo("javax.ws.rs.PathParam")); assertThat(foundAnnotation.getJavaAnnotation(), notNullValue()); }