use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-eclipse by eclipse.
the class XbaseReferenceUpdater method createTextChange.
@Override
protected void createTextChange(ITextRegion referenceTextRegion, String newReferenceText, EObject referringElement, EObject newTargetElement, EReference reference, URI referringResourceURI, IRefactoringUpdateAcceptor updateAcceptor) {
if (newReferenceText != null && updateAcceptor instanceof ImportAwareUpdateAcceptor && isReferenceToJvmType(newTargetElement, reference)) {
JvmType newTargetType = (JvmType) newTargetElement;
ImportAwareUpdateAcceptor importAwareUpdateAcceptor = (ImportAwareUpdateAcceptor) updateAcceptor;
Pair<JvmDeclaredType, QualifiedName> importedTypeAndRelativeName = getImportedTypeAndRelativeName(newTargetType, importAwareUpdateAcceptor.getImportSection());
if (importedTypeAndRelativeName != null) {
JvmDeclaredType importedType = importedTypeAndRelativeName.getFirst();
QualifiedName importRelativeName = importedTypeAndRelativeName.getSecond();
importAwareUpdateAcceptor.removeImport(importedType, false, false, null);
if (isReferencedByQualifiedName(referringElement, newTargetType, importRelativeName)) {
QualifiedName newTypeQualifiedName = qualifiedNameProvider.getFullyQualifiedName(newTargetElement);
newReferenceText = getLinkText(newTypeQualifiedName, newReferenceText);
} else {
if (!isEmpty(importedType.getPackageName())) {
importAwareUpdateAcceptor.acceptImport(importedType, false, false, null);
}
newReferenceText = getLinkText(importRelativeName, newReferenceText);
}
}
}
super.createTextChange(referenceTextRegion, newReferenceText, referringElement, newTargetElement, reference, referringResourceURI, updateAcceptor);
}
use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-eclipse by eclipse.
the class XbaseHoverDocumentationProvider method handleLink.
protected void handleLink(List<?> fragments) {
if (fragments == null || fragments.isEmpty()) {
return;
}
URI elementURI = null;
String firstFragment = fragments.get(0).toString();
int hashIndex = firstFragment.indexOf("#");
if (hashIndex != -1) {
JvmDeclaredType resolvedDeclarator = getResolvedDeclarator(firstFragment.substring(0, hashIndex));
if (resolvedDeclarator != null && !resolvedDeclarator.eIsProxy()) {
String signature = firstFragment.substring(hashIndex + 1);
int indexOfOpenBracket = signature.indexOf("(");
int indexOfClosingBracket = signature.indexOf(")");
String simpleNameOfFeature = indexOfOpenBracket != -1 ? signature.substring(0, indexOfOpenBracket) : signature;
Iterable<JvmFeature> possibleCandidates = resolvedDeclarator.findAllFeaturesByName(simpleNameOfFeature);
List<String> parameterNames = null;
if (indexOfOpenBracket != -1 && indexOfClosingBracket != -1) {
parameterNames = Strings.split(signature.substring(indexOfOpenBracket + 1, indexOfClosingBracket), ",");
}
Iterator<JvmFeature> featureIterator = possibleCandidates.iterator();
while (elementURI == null && featureIterator.hasNext()) {
JvmFeature feature = featureIterator.next();
boolean valid = false;
if (feature instanceof JvmField) {
valid = true;
} else if (feature instanceof JvmExecutable) {
JvmExecutable executable = (JvmExecutable) feature;
EList<JvmFormalParameter> parameters = executable.getParameters();
if (parameterNames == null) {
valid = true;
} else if (parameters.size() == parameterNames.size()) {
valid = true;
for (int i = 0; (i < parameterNames.size() && valid); i++) {
URI parameterTypeURI = EcoreUtil.getURI(parameters.get(i).getParameterType().getType());
IEObjectDescription expectedParameter = scopeProvider.getScope(context, new HoverReference(TypesPackage.Literals.JVM_TYPE)).getSingleElement(qualifiedNameConverter.toQualifiedName(parameterNames.get(i)));
if (expectedParameter == null || !expectedParameter.getEObjectURI().equals(parameterTypeURI)) {
valid = false;
}
}
}
}
if (valid)
elementURI = EcoreUtil.getURI(feature);
}
}
} else {
IScope scope = scopeProvider.getScope(context, new HoverReference(TypesPackage.Literals.JVM_TYPE));
IEObjectDescription singleElement = scope.getSingleElement(qualifiedNameConverter.toQualifiedName(firstFragment));
if (singleElement != null)
elementURI = singleElement.getEObjectURI();
}
String label = "";
if (fragments.size() > 1) {
for (int i = 1; i < fragments.size(); i++) {
String portentialLabel = fragments.get(i).toString();
if (portentialLabel.trim().length() > 0)
label += portentialLabel;
}
}
if (label.length() == 0)
label = firstFragment;
if (elementURI == null)
buffer.append(label);
else {
buffer.append(createLinkWithLabel(XtextElementLinks.XTEXTDOC_SCHEME, elementURI, label));
}
}
use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-eclipse by eclipse.
the class XbaseUIValidator method checkRestrictedType.
protected void checkRestrictedType(final EObject context, final EStructuralFeature feature, final JvmDeclaredType typeToCheck) {
@SuppressWarnings("unchecked") Map<JvmDeclaredType, RestrictionKind> validationContext = (Map<JvmDeclaredType, RestrictionKind>) getContext().get(RestrictionKind.class);
if (validationContext == null) {
validationContext = Maps.newHashMap();
getContext().put(RestrictionKind.class, validationContext);
}
RestrictionKind restriction = validationContext.get(typeToCheck);
IJavaProject javaProject = null;
if (restriction == null) {
final IJavaElement javaElement = javaElementFinder.findElementFor(typeToCheck);
if (javaElement == null || !(javaElement instanceof IType)) {
validationContext.put(typeToCheck, RestrictionKind.VALID);
return;
}
javaProject = javaElement.getJavaProject();
restriction = computeRestriction(projectProvider.getJavaProject(context.eResource().getResourceSet()), (IType) javaElement);
validationContext.put(typeToCheck, restriction);
}
if (restriction == RestrictionKind.FORBIDDEN) {
if (javaProject == null)
javaProject = projectProvider.getJavaProject(context.eResource().getResourceSet());
addIssue("Access restriction: The type " + typeToCheck.getSimpleName() + " is not accessible due to restriction on required project " + javaProject.getElementName(), context, feature, IssueCodes.FORBIDDEN_REFERENCE);
} else if (restriction == RestrictionKind.DISCOURAGED) {
if (javaProject == null)
javaProject = projectProvider.getJavaProject(context.eResource().getResourceSet());
addIssue("Discouraged access: The type " + typeToCheck.getSimpleName() + " is not accessible due to restriction on required project " + javaProject.getElementName(), context, feature, IssueCodes.DISCOURAGED_REFERENCE);
}
}
use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-eclipse by eclipse.
the class AbstractTypeProviderTest method testInnerAnnotationType.
@Test
public void testInnerAnnotationType() throws Exception {
JvmDeclaredType declaredType = (JvmDeclaredType) getTypeProvider().findTypeByName(TypeWithInnerAnnotation.class.getName());
assertEquals(2, declaredType.getMembers().size());
// default constructor
assertTrue(Iterables.any(declaredType.getMembers(), new Predicate<JvmMember>() {
@Override
public boolean apply(JvmMember input) {
return (input instanceof JvmConstructor) && input.getSimpleName().equals(TypeWithInnerAnnotation.class.getSimpleName());
}
}));
// inner annotation type
assertTrue(Iterables.any(declaredType.getMembers(), new Predicate<JvmMember>() {
@Override
public boolean apply(JvmMember input) {
return (input instanceof JvmAnnotationType) && input.getIdentifier().equals(TypeWithInnerAnnotation.MyAnnotation.class.getName()) && input.getVisibility() == JvmVisibility.PUBLIC;
}
}));
}
use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-eclipse by eclipse.
the class AbstractTypeProviderTest method getMethodParameterAnnotationValue.
public JvmAnnotationValue getMethodParameterAnnotationValue(String name, boolean defaultValue) {
String typeName = TestAnnotation.Annotated.class.getName();
JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
JvmOperation method = getMethodFromType(type, TestAnnotation.Annotated.class, "method(java.lang.String,java.lang.String,java.lang.String)");
JvmAnnotationTarget target = method.getParameters().get(0);
JvmAnnotationValue result = getDefaultOrExplicitAnnotationValue(name, target);
if (defaultValue) {
if (isDefaultValueSupported()) {
assertTrue(result.eContainer() instanceof JvmOperation);
} else {
assertFalse(result.eContainer() instanceof JvmOperation);
}
} else {
assertFalse(result.eContainer() instanceof JvmOperation);
}
return result;
}
Aggregations