org.eclipse.jdt.core.IAnnotation Java Examples

The following examples show how to use org.eclipse.jdt.core.IAnnotation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PipelineOptionsPropertyTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromMethodWithoutDescriptionHasAbsentOptional() throws Exception {
  IMethod method = mock(IMethod.class);
  when(method.getElementName()).thenReturn("getFoo");

  IAnnotation required = mock(IAnnotation.class);
  when(method.getAnnotation(PipelineOptionsNamespaces.validationRequired(version)))
      .thenReturn(required);
  when(required.exists()).thenReturn(false);
  when(required.getElementName())
      .thenReturn(PipelineOptionsNamespaces.validationRequired(version));
  when(method.getAnnotations()).thenReturn(new IAnnotation[] {required});

  IAnnotation description = mock(IAnnotation.class);
  when(description.exists()).thenReturn(false);
  when(method.getAnnotation(PipelineOptionsNamespaces.descriptionAnnotation(version)))
      .thenReturn(description);

  PipelineOptionsProperty property = PipelineOptionsProperty.fromMethod(method, version);

  assertNull(property.getDescription());
}
 
Example #2
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void appendFlags(final IMember member) throws JavaModelException {
	if (member instanceof IAnnotatable)
		for (IAnnotation annotation : ((IAnnotatable) member).getAnnotations()) {
			appendAnnotation(annotation);
		}
	
	int flags= member.getFlags();
	final int kind= member.getElementType();
	if (kind == IJavaElement.TYPE) {
		flags&= ~Flags.AccSuper;
		final IType type= (IType) member;
		if (!type.isMember())
			flags&= ~Flags.AccPrivate;
		if (Flags.isEnum(flags))
			flags&= ~Flags.AccAbstract;
	}
	if (Flags.isEnum(flags))
		flags&= ~Flags.AccFinal;
	if (kind == IJavaElement.METHOD) {
		flags&= ~Flags.AccVarargs;
		flags&= ~Flags.AccBridge;
	}
	if (flags != 0)
		fBuffer.append(Flags.toString(flags));
}
 
Example #3
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public void appendAnnotationLabel(IAnnotation annotation, long flags) throws JavaModelException {
	fBuilder.append('@');
	appendTypeSignatureLabel(annotation, Signature.createTypeSignature(annotation.getElementName(), false), flags);
	IMemberValuePair[] memberValuePairs= annotation.getMemberValuePairs();
	if (memberValuePairs.length == 0) {
		return;
	}
	fBuilder.append('(');
	for (int i= 0; i < memberValuePairs.length; i++) {
		if (i > 0) {
			fBuilder.append(JavaElementLabels.COMMA_STRING);
		}
		IMemberValuePair memberValuePair= memberValuePairs[i];
		fBuilder.append(getMemberName(annotation, annotation.getElementName(), memberValuePair.getMemberName()));
		fBuilder.append('=');
		appendAnnotationValue(annotation, memberValuePair.getValue(), memberValuePair.getValueKind(), flags);
	}
	fBuilder.append(')');
}
 
Example #4
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void appendAnnotation(IAnnotation annotation) throws JavaModelException {
	String name= annotation.getElementName();
	if (!fStubInvisible && name.startsWith("sun.")) //$NON-NLS-1$
		return; // skip Sun-internal annotations 
	
	fBuffer.append('@');
	fBuffer.append(name);
	fBuffer.append('(');
	
	IMemberValuePair[] memberValuePairs= annotation.getMemberValuePairs();
	for (IMemberValuePair pair : memberValuePairs) {
		fBuffer.append(pair.getMemberName());
		fBuffer.append('=');
		appendAnnotationValue(pair.getValue(), pair.getValueKind());
		fBuffer.append(',');
	}
	if (memberValuePairs.length > 0)
		fBuffer.deleteCharAt(fBuffer.length() - 1);
	
	fBuffer.append(')').append('\n');
}
 
Example #5
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean isSilencedGeneratedAnnotation(IAnnotation annotation) throws JavaModelException {
	if ("javax.annotation.Generated".equals(annotation.getElementName()) || "javax.annotation.processing.Generated".equals(annotation.getElementName())) {
		IMemberValuePair[] memberValuePairs = annotation.getMemberValuePairs();
		for (IMemberValuePair m : memberValuePairs) {
			if ("value".equals(m.getMemberName())
					&& IMemberValuePair.K_STRING == m.getValueKind()) {
				if (m.getValue() instanceof String) {
					return SILENCED_CODEGENS.contains(m.getValue());
				} else if (m.getValue() instanceof Object[]) {
					for (Object val : (Object[])m.getValue()) {
						if(SILENCED_CODEGENS.contains(val)) {
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}
 
Example #6
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean isHiddenGeneratedElement(IJavaElement element) {
	// generated elements are annotated with @Generated and they need to be filtered out
	if (element instanceof IAnnotatable) {
		try {
			IAnnotation[] annotations = ((IAnnotatable) element).getAnnotations();
			if (annotations.length != 0) {
				for (IAnnotation annotation : annotations) {
					if (isSilencedGeneratedAnnotation(annotation)) {
						return true;
					}
				}
			}
		} catch (JavaModelException e) {
			//ignore
		}
	}
	return false;
}
 
Example #7
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void appendAnnotationLabel(IAnnotation annotation, long flags) throws JavaModelException {
	fBuffer.append('@');
	appendTypeSignatureLabel(annotation, Signature.createTypeSignature(annotation.getElementName(), false), flags);
	IMemberValuePair[] memberValuePairs= annotation.getMemberValuePairs();
	if (memberValuePairs.length == 0)
		return;
	fBuffer.append('(');
	for (int i= 0; i < memberValuePairs.length; i++) {
		if (i > 0)
			fBuffer.append(JavaElementLabels.COMMA_STRING);
		IMemberValuePair memberValuePair= memberValuePairs[i];
		fBuffer.append(getMemberName(annotation, annotation.getElementName(), memberValuePair.getMemberName()));
		fBuffer.append('=');
		appendAnnotationValue(annotation, memberValuePair.getValue(), memberValuePair.getValueKind(), flags);
	}
	fBuffer.append(')');
}
 
Example #8
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 6 votes vote down vote up
private String extractDefaultValue(IMethod operation, IAnnotation annot)
		throws JavaModelException, IllegalArgumentException {
	IAnnotation annotation = annot;
	final Object value = annotation.getMemberValuePairs()[0].getValue();
	final String fieldId = (value == null) ? null : value.toString();
	if (!Strings.isNullOrEmpty(fieldId)) {
		final String fieldName = Utils.createNameForHiddenDefaultValueAttribute(fieldId);
		final IField field = operation.getDeclaringType().getField(fieldName);
		if (field != null) {
			annotation = getAnnotation(field, SarlSourceCode.class.getName());
			if (annotation != null) {
				return annotation.getMemberValuePairs()[0].getValue().toString();
			}
		}
	}
	return null;
}
 
Example #9
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Replies the annotation with the given qualified name.
 *
 * @param element the annoted element.
 * @param qualifiedName the qualified name of the element.
 * @return the annotation, or {@code null} if the element is not annoted.
 */
public IAnnotation getAnnotation(IAnnotatable element, String qualifiedName) {
	if (element != null) {
		try {
			final int separator = qualifiedName.lastIndexOf('.');
			final String simpleName;
			if (separator >= 0 && separator < (qualifiedName.length() - 1)) {
				simpleName = qualifiedName.substring(separator + 1, qualifiedName.length());
			} else {
				simpleName = qualifiedName;
			}
			for (final IAnnotation annotation : element.getAnnotations()) {
				final String name = annotation.getElementName();
				if (name.equals(simpleName) || name.equals(qualifiedName)) {
					return annotation;
				}
			}
		} catch (JavaModelException e) {
			//
		}
	}
	return null;
}
 
Example #10
Source File: PipelineOptionsPropertyTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromMethodWithNoValidationRequiredAnnotationIsNotRequired()
    throws JavaModelException {
  IMethod method = mock(IMethod.class);
  when(method.getElementName()).thenReturn("getFoo");

  IAnnotation required = mock(IAnnotation.class);
  when(method.getAnnotation(PipelineOptionsNamespaces.validationRequired(version)))
      .thenReturn(required);
  when(required.exists()).thenReturn(false);
  when(required.getElementName())
      .thenReturn(PipelineOptionsNamespaces.validationRequired(version));
  when(method.getAnnotations()).thenReturn(new IAnnotation[] {required});

  IAnnotation description = mock(IAnnotation.class);
  when(description.exists()).thenReturn(false);
  when(method.getAnnotation(PipelineOptionsNamespaces.descriptionAnnotation(version)))
      .thenReturn(description);

  PipelineOptionsProperty property = PipelineOptionsProperty.fromMethod(method, version);

  assertEquals("foo", property.getName());
  assertFalse(property.isRequired());
  assertTrue(property.getGroups().isEmpty());
}
 
Example #11
Source File: PipelineOptionsPropertyTest.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromMethodWithBooleanReturnValueAndIsPropertyReturnsProperty()
    throws JavaModelException {
  IMethod method = mock(IMethod.class);
  when(method.getElementName()).thenReturn("isFoo");

  IAnnotation required = mock(IAnnotation.class);
  when(method.getAnnotation(PipelineOptionsNamespaces.validationRequired(version)))
      .thenReturn(required);
  when(required.exists()).thenReturn(false);
  when(required.getElementName())
      .thenReturn(PipelineOptionsNamespaces.validationRequired(version));
  when(method.getAnnotations()).thenReturn(new IAnnotation[] {required});

  IAnnotation description = mock(IAnnotation.class);
  when(description.exists()).thenReturn(false);
  when(method.getAnnotation(PipelineOptionsNamespaces.descriptionAnnotation(version)))
      .thenReturn(description);

  PipelineOptionsProperty property = PipelineOptionsProperty.fromMethod(method, version);

  assertEquals("foo", property.getName());
  assertFalse(property.isRequired());
  assertTrue(property.getGroups().isEmpty());
}
 
Example #12
Source File: BinaryMember.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected IAnnotation[] getAnnotations(IBinaryAnnotation[] binaryAnnotations, long tagBits) {
	IAnnotation[] standardAnnotations = getStandardAnnotations(tagBits);
	if (binaryAnnotations == null)
		return standardAnnotations;
	int length = binaryAnnotations.length;
	int standardLength = standardAnnotations.length;
	int fullLength = length + standardLength;
	if (fullLength == 0) {
		return Annotation.NO_ANNOTATIONS;
	}
	IAnnotation[] annotations = new IAnnotation[fullLength];
	for (int i = 0; i < length; i++) {
		annotations[i] = Util.getAnnotation(this, binaryAnnotations[i], null);
	}
	System.arraycopy(standardAnnotations, 0, annotations, length, standardLength);
	return annotations;
}
 
Example #13
Source File: PipelineOptionsPropertyTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromMethodWithDescriptionHasDescription() throws Exception {
  IMethod method = mock(IMethod.class);
  when(method.getElementName()).thenReturn("getFoo");

  IAnnotation required = mock(IAnnotation.class);
  when(method.getAnnotation(PipelineOptionsNamespaces.validationRequired(version)))
      .thenReturn(required);
  when(required.exists()).thenReturn(false);
  when(required.getElementName())
      .thenReturn(PipelineOptionsNamespaces.validationRequired(version));
  when(method.getAnnotations()).thenReturn(new IAnnotation[] {required});

  IAnnotation description = mock(IAnnotation.class);
  when(description.exists()).thenReturn(true);
  when(method.getAnnotation(PipelineOptionsNamespaces.descriptionAnnotation(version)))
      .thenReturn(description);
  IMemberValuePair memberValuePair = mock(IMemberValuePair.class);
  String descriptionValue = "My_description exists";
  when(memberValuePair.getValue()).thenReturn(descriptionValue);
  when(memberValuePair.getValueKind()).thenReturn(IMemberValuePair.K_STRING);
  IMemberValuePair[] memberValuePairs = new IMemberValuePair[] {memberValuePair};
  when(description.getMemberValuePairs()).thenReturn(memberValuePairs);

  PipelineOptionsProperty property = PipelineOptionsProperty.fromMethod(method, version);

  assertEquals(descriptionValue, property.getDescription());
}
 
Example #14
Source File: JavaElementDelegateJunitLaunch.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean containsJUnitTestMethod(IType type) throws JavaModelException {
	for (IMethod method : type.getMethods()) {
		int flags = method.getFlags();
		if (Modifier.isPublic(flags) && !Modifier.isStatic(flags) &&
				method.getNumberOfParameters() == 0 && Signature.SIG_VOID.equals(method.getReturnType()) &&
				method.getElementName().startsWith("test")) { //$NON-NLS-1$
			return true;
		}
		IAnnotation annotation= method.getAnnotation("Test"); //$NON-NLS-1$
		if (annotation.exists())
			return true;
	}
	return false;
}
 
Example #15
Source File: PipelineOptionsProperty.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private static Requirement fromAnnotation(
    IAnnotation requiredAnnotation, MajorVersion majorVersion) {
  if (!requiredAnnotation.exists()) {
    return new Requirement(false, Collections.<String>emptySet());
  }
  IMemberValuePair[] memberValuePairs;
  try {
    memberValuePairs = requiredAnnotation.getMemberValuePairs();
  } catch (JavaModelException e) {
    DataflowCorePlugin.logError(e, "Error while retrieving Member Value Pairs for"
        + " Validation.Required annotation %s in Java Element %s", requiredAnnotation,
        requiredAnnotation.getParent());
    return new Requirement(true, Collections.<String>emptySet());
  }
  for (IMemberValuePair memberValuePair : memberValuePairs) {
    String memberName = memberValuePair.getMemberName();
    Object memberValueObj = memberValuePair.getValue();
    if (memberName.equals(PipelineOptionsNamespaces.validationRequiredGroupField(majorVersion))
        && memberValueObj instanceof Object[]
        && memberValuePair.getValueKind() == IMemberValuePair.K_STRING) {
      Set<String> groups = new HashSet<>();
      for (Object group : (Object[]) memberValueObj) {
        groups.add(group.toString());
      }
      return new Requirement(true, groups);
    }
  }
  return new Requirement(true, Collections.<String>emptySet());
}
 
Example #16
Source File: WizardUtils.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return an empty optional if the given type is not annotated, otherwise
 *         the name of the model package and its java project respectively,
 *         which contains the types of the given annotation
 */
public static Optional<Pair<String, String>> getModelByAnnotations(IType annotatedType) {
	try {
		List<String> referencedProjects = new ArrayList<>(
				Arrays.asList(annotatedType.getJavaProject().getRequiredProjectNames()));
		referencedProjects.add(annotatedType.getJavaProject().getElementName());
		for (IAnnotation annot : annotatedType.getAnnotations()) {
			List<Object> annotValues = Stream.of(annot.getMemberValuePairs())
					.filter(mvp -> mvp.getValueKind() == IMemberValuePair.K_CLASS)
					.flatMap(mvp -> Stream.of(mvp.getValue())).collect(Collectors.toList());

			if (annotValues.isEmpty()) {
				throw new NoSuchElementException("Group is empty.");
			}

			for (Object val : annotValues) {
				List<Object> annotations = new ArrayList<>();
				if (val instanceof String) {
					annotations.add(val);
				} else {
					annotations.addAll(Arrays.asList((Object[]) val));
				}

				for (Object v : annotations) {
					String[][] resolvedTypes = resolveType(annotatedType, (String) v);
					List<String[]> resolvedTypeList = new ArrayList<>(Arrays.asList(resolvedTypes));
					for (String[] type : resolvedTypeList) {
						Optional<Pair<String, String>> model = ModelUtils.getModelOf(type[0], referencedProjects);
						if (model.isPresent()) {
							return model;
						}
					}
				}
			}
		}
	} catch (JavaModelException e) {
	}

	return Optional.empty();
}
 
Example #17
Source File: PipelineOptionsPropertyTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromMethodWithNonStringDescriptionHasAbsentOptional() throws Exception {
  IMethod method = mock(IMethod.class);
  when(method.getElementName()).thenReturn("getFoo");

  IAnnotation required = mock(IAnnotation.class);
  when(method.getAnnotation(PipelineOptionsNamespaces.validationRequired(version)))
      .thenReturn(required);
  when(required.exists()).thenReturn(false);
  when(required.getElementName())
      .thenReturn(PipelineOptionsNamespaces.validationRequired(version));
  when(method.getAnnotations()).thenReturn(new IAnnotation[] {required});

  IAnnotation description = mock(IAnnotation.class);
  when(description.exists()).thenReturn(true);
  when(method.getAnnotation(PipelineOptionsNamespaces.descriptionAnnotation(version)))
      .thenReturn(description);
  IMemberValuePair memberValuePair = mock(IMemberValuePair.class);
  when(memberValuePair.getValue()).thenReturn(3);
  when(memberValuePair.getValueKind()).thenReturn(IMemberValuePair.K_INT);
  IMemberValuePair[] memberValuePairs = new IMemberValuePair[] {memberValuePair};
  when(description.getMemberValuePairs()).thenReturn(memberValuePairs);

  PipelineOptionsProperty property = PipelineOptionsProperty.fromMethod(method, version);

  assertNull(property.getDescription());
}
 
Example #18
Source File: ElementTypeTeller.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean checkPackageInfo(ICompilationUnit compUnit) throws JavaModelException {
	for (IPackageDeclaration packDecl : compUnit.getPackageDeclarations()) {
		for (IAnnotation annot : packDecl.getAnnotations()) {
			// Because names are not resolved in IJavaElement AST
			// representation, we have to manually check if a given
			// annotation is really the Model annotation.
			if (isImportedNameResolvedTo(compUnit, annot.getElementName(), Model.class.getCanonicalName())) {
				return true;
			}
		}
	}
	return false;
}
 
Example #19
Source File: Jdt2EcoreTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
public void isGeneratedOperation_generatedAnnotation() throws JavaModelException {
	IAnnotation annot = mock(IAnnotation.class);
	when(annot.getElementName()).thenReturn("javax.annotation.Generated");
	IMethod m = mock(IMethod.class);
	when(m.getAnnotations()).thenReturn(new IAnnotation[] {
			annot
	});
	assertTrue(this.jdt2ecore.isGeneratedOperation(m));
}
 
Example #20
Source File: PipelineOptionsPropertyTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromMethodWithDefaultHasDefaultProvided() throws JavaModelException {
  IMethod method = mock(IMethod.class);
  when(method.getElementName()).thenReturn("getFoo");

  IAnnotation required = mock(IAnnotation.class);
  when(method.getAnnotation(PipelineOptionsNamespaces.validationRequired(version)))
      .thenReturn(required);
  when(required.exists()).thenReturn(true);
  when(required.getElementName())
      .thenReturn(PipelineOptionsNamespaces.validationRequired(version));

  IMemberValuePair[] memberValuePairs = new IMemberValuePair[0];
  when(required.getMemberValuePairs()).thenReturn(memberValuePairs);

  IAnnotation defaultAnnotation = mock(IAnnotation.class);
  when(defaultAnnotation.getElementName())
      .thenReturn(PipelineOptionsNamespaces.defaultProvider(version) + ".String");
  when(method.getAnnotations()).thenReturn(new IAnnotation[] {required, defaultAnnotation});

  IAnnotation description = mock(IAnnotation.class);
  when(description.exists()).thenReturn(false);
  when(method.getAnnotation(PipelineOptionsNamespaces.descriptionAnnotation(version)))
      .thenReturn(description);

  PipelineOptionsProperty property = PipelineOptionsProperty.fromMethod(method, version);

  assertEquals("foo", property.getName());
  assertTrue(property.isRequired());
  assertTrue(property.isDefaultProvided());
  assertFalse(property.isUserValueRequired());
}
 
Example #21
Source File: PlatformJavaModelUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the corresponding IAnnotation.
 *
 * @param qualifiedAnnotationName the fully qualified annotation type name
 * @param annotatable the IAnnotatable java element which contains the
 *          annotation
 * @param contextType the type which is used to lookup imports
 * @return the IAnnotation or null
 * @throws JavaModelException
 */
public static Object getAnnotation(String qualifiedAnnotationName,
    Object annotatable, IType contextType) throws JavaModelException {
  for (IAnnotation annotation : ((IAnnotatable) annotatable).getAnnotations()) {
    if (qualifiedAnnotationName.equals(resolveTypeName(contextType,
        annotation.getElementName()))) {
      return annotation;
    }
  }

  return null;
}
 
Example #22
Source File: Jdt2EcoreTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Test
public void isGeneratedOperation_noAnnotation() throws JavaModelException {
	IMethod m = mock(IMethod.class);
	when(m.getAnnotations()).thenReturn(new IAnnotation[] {
	});
	assertFalse(this.jdt2ecore.isGeneratedOperation(m));
}
 
Example #23
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void appendAnnotationValue(Object value, int valueKind) throws JavaModelException {
	if (value instanceof Object[]) {
		Object[] objects= (Object[]) value;
		fBuffer.append('{');
		for (Object object : objects) {
			appendAnnotationValue(object, valueKind);
			fBuffer.append(',');
		}
		if (objects.length > 0)
			fBuffer.deleteCharAt(fBuffer.length() - 1);
		fBuffer.append('}');
		
	} else {
		switch (valueKind) {
			case IMemberValuePair.K_ANNOTATION:
				appendAnnotation((IAnnotation) value);
				break;
			case IMemberValuePair.K_STRING:
				fBuffer.append('"').append(value).append('"');
				break;
				
			default:
				fBuffer.append(value);
				break;
		}
	}
}
 
Example #24
Source File: JavaElementLabelComposer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void appendAnnotationLabels(IAnnotation[] annotations, long flags) throws JavaModelException {
	for (int j= 0; j < annotations.length; j++) {
		IAnnotation annotation= annotations[j];
		appendAnnotationLabel(annotation, flags);
		fBuffer.append(' ');
	}
}
 
Example #25
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Create the formal parameters for the given operation.
 *
 * @param parameterBuilder the code builder.
 * @param operation the operation that describes the formal parameters.
 * @return the parameters.
 * @throws JavaModelException if the Java model is invalid.
 * @throws IllegalArgumentException if the signature is not syntactically correct.
 */
protected IFormalParameterBuilder[] createFormalParametersWith(
		ParameterBuilder parameterBuilder,
		IMethod operation) throws JavaModelException, IllegalArgumentException {
	final boolean isVarargs = Flags.isVarargs(operation.getFlags());
	final ILocalVariable[] rawParameters = operation.getParameters();
	final FormalParameterProvider parameters = getFormalParameterProvider(operation);
	final int len = parameters.getFormalParameterCount();
	final IFormalParameterBuilder[] paramBuilders = new IFormalParameterBuilder[len];
	for (int i = 0; i < len; ++i) {
		final ILocalVariable rawParameter = rawParameters[i];
		final IAnnotation annotation = getAnnotation(rawParameter, DefaultValue.class.getName());
		final String defaultValue = (annotation != null) ? extractDefaultValue(operation, annotation) : null;
		final boolean isV = isVarargs && i == len - 1;
		String type = parameters.getFormalParameterType(i, isV);
		if (isV && type.endsWith("[]")) { //$NON-NLS-1$
			type = type.substring(0, type.length() - 2);
		}
		final IFormalParameterBuilder sarlParameter = parameterBuilder.addParameter(parameters.getFormalParameterName(i));
		sarlParameter.setParameterType(type);
		if (defaultValue != null) {
			sarlParameter.getDefaultValue().setExpression(defaultValue);
		}
		if (isV) {
			sarlParameter.setVarArg(true);
		}
		paramBuilders[i] = sarlParameter;
	}
	return paramBuilders;
}
 
Example #26
Source File: CompletionUnitStructureRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected IAnnotation acceptAnnotation(
		org.eclipse.jdt.internal.compiler.ast.Annotation annotation,
		AnnotatableInfo parentInfo,
		JavaElement parentHandle) {
	if (annotation instanceof CompletionOnMarkerAnnotationName) {
		if (hasEmptyName(annotation.type, this.assistNode)) {
			super.acceptAnnotation(annotation, null, parentHandle);
			return null;
		}
	}
	return super.acceptAnnotation(annotation, parentInfo, parentHandle);
}
 
Example #27
Source File: SourceTypeConverter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException {
	IAnnotation[] annotations = element.getAnnotations();
	int length = annotations.length;
	Annotation[] astAnnotations = new Annotation[length];
	if (length > 0) {
		char[] cuSource = getSource();
		int recordedAnnotations = 0;
		for (int i = 0; i < length; i++) {
			ISourceRange positions = annotations[i].getSourceRange();
			int start = positions.getOffset();
			int end = start + positions.getLength();
			char[] annotationSource = CharOperation.subarray(cuSource, start, end);
			if (annotationSource != null) {
    			Expression expression = parseMemberValue(annotationSource);
    			/*
    			 * expression can be null or not an annotation if the source has changed between
    			 * the moment where the annotation source positions have been retrieved and the moment were
    			 * this parsing occurred.
    			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916
    			 */
    			if (expression instanceof Annotation) {
    				astAnnotations[recordedAnnotations++] = (Annotation) expression;
    			}
			}
		}
		if (length != recordedAnnotations) {
			// resize to remove null annotations
			System.arraycopy(astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0, recordedAnnotations);
		}
	}
	return astAnnotations;
}
 
Example #28
Source File: BinaryMember.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected IAnnotation[] getStandardAnnotations(long tagBits) {
	if ((tagBits & TagBits.AllStandardAnnotationsMask) == 0)
		return Annotation.NO_ANNOTATIONS;
	ArrayList annotations = new ArrayList();

	if ((tagBits & TagBits.AnnotationTargetMASK) != 0) {
		annotations.add(getAnnotation(TypeConstants.JAVA_LANG_ANNOTATION_TARGET));
	}
	if ((tagBits & TagBits.AnnotationRetentionMASK) != 0) {
		annotations.add(getAnnotation(TypeConstants.JAVA_LANG_ANNOTATION_RETENTION));
	}
	if ((tagBits & TagBits.AnnotationDeprecated) != 0) {
		annotations.add(getAnnotation(TypeConstants.JAVA_LANG_DEPRECATED));
	}
	if ((tagBits & TagBits.AnnotationDocumented) != 0) {
		annotations.add(getAnnotation(TypeConstants.JAVA_LANG_ANNOTATION_DOCUMENTED));
	}
	if ((tagBits & TagBits.AnnotationInherited) != 0) {
		annotations.add(getAnnotation(TypeConstants.JAVA_LANG_ANNOTATION_INHERITED));
	}
	if ((tagBits & TagBits.AnnotationPolymorphicSignature) != 0) {
		annotations.add(getAnnotation(TypeConstants.JAVA_LANG_INVOKE_METHODHANDLE_$_POLYMORPHICSIGNATURE));
	}
	if ((tagBits & TagBits.AnnotationSafeVarargs) != 0) {
		annotations.add(getAnnotation(TypeConstants.JAVA_LANG_SAFEVARARGS));
	}
	// note that JAVA_LANG_SUPPRESSWARNINGS and JAVA_LANG_OVERRIDE cannot appear in binaries
	return (IAnnotation[]) annotations.toArray(new IAnnotation[annotations.size()]);
}
 
Example #29
Source File: JavaElementDeltaBuilder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Records this elements info, and attempts
 * to record the info for the children.
 */
private void recordElementInfo(IJavaElement element, JavaModel model, int depth) {
	if (depth >= this.maxDepth) {
		return;
	}
	JavaElementInfo info = (JavaElementInfo)JavaModelManager.getJavaModelManager().getInfo(element);
	if (info == null) // no longer in the java model.
		return;
	this.infos.put(element, info);

	if (element instanceof IParent) {
		IJavaElement[] children = info.getChildren();
		if (children != null) {
			insertPositions(children, false);
			for(int i = 0, length = children.length; i < length; i++)
				recordElementInfo(children[i], model, depth + 1);
		}
	}
	IAnnotation[] annotations = null;
	if (info instanceof AnnotatableInfo)
		annotations = ((AnnotatableInfo) info).annotations;
	if (annotations != null) {
		if (this.annotationInfos == null)
			this.annotationInfos = new HashMap();
		JavaModelManager manager = JavaModelManager.getJavaModelManager();
		for (int i = 0, length = annotations.length; i < length; i++) {
			this.annotationInfos.put(annotations[i], manager.getInfo(annotations[i]));
		}
	}
}
 
Example #30
Source File: BinaryMethod.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IAnnotation[] getAnnotations(JavaElement annotationParent, IBinaryAnnotation[] binaryAnnotations) {
	if (binaryAnnotations == null) return Annotation.NO_ANNOTATIONS;
	int length = binaryAnnotations.length;
	IAnnotation[] annotations = new IAnnotation[length];
	for (int i = 0; i < length; i++) {
		annotations[i] = Util.getAnnotation(annotationParent, binaryAnnotations[i], null);
	}
	return annotations;
}