org.eclipse.xtext.common.types.access.TypeResource Java Examples

The following examples show how to use org.eclipse.xtext.common.types.access.TypeResource. 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: RawResolvedFeatures.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Registers the given listener to be notified on type changes, if (and only if) the type
 * if expected to be changeable.
 * 
 * @see IMirrorExtension#isSealed()
 */
protected static void requestNotificationOnChange(JvmType type, Runnable listener) {
	Resource resource = type.eResource();
	if (resource instanceof TypeResource) {
		IMirror mirror = ((TypeResource) resource).getMirror();
		if (mirror instanceof IMirrorExtension) {
			if (((IMirrorExtension) mirror).isSealed())
				return;
		}
	}
	Notifier notifier = type;
	if (resource != null) {
		if (resource.getResourceSet() != null)
			notifier = resource.getResourceSet();
		else
			notifier = resource;
	}
	JvmTypeChangeDispatcher dispatcher = JvmTypeChangeDispatcher.findResourceChangeDispatcher(notifier);
	dispatcher.requestNotificationOnChange(type, listener);
}
 
Example #2
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.7
 */
@Override
public JvmDeclaredType createType(IType type, TypeResource resource, Map<?, ?> options) {
	if (resource == null) {
		return createType(type);
	}
	if (options != null) {
		IJavaProject javaProject = (IJavaProject) options.get(TypeResource.OPTION_CLASSPATH_CONTEXT);
		if (javaProject != null) {
			return createType(type, javaProject);
		}
	}
	ResourceSet resourceSet = resource.getResourceSet();
	if (resourceSet instanceof XtextResourceSet) {
		Object project = ((XtextResourceSet) resourceSet).getClasspathURIContext();
		if (project instanceof IJavaProject) {
			return createType(type, (IJavaProject) project);
		}
	}
	return createType(type);
}
 
Example #3
Source File: JdtTypeMirror.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.7
 */
@Override
public void initialize(TypeResource typeResource, Map<?, ?> options) {
	try {
		if (typeFactory instanceof ITypeFactory.OptionsAware<?, ?>) {
			JvmDeclaredType jvmType = ((ITypeFactory.OptionsAware<IType, JvmDeclaredType>) typeFactory).createType(mirroredType, typeResource, options);
			typeResource.getContents().add(jvmType);
		} else {
			typeResource.getContents().add(typeFactory.createType(mirroredType));
		}
	} catch (RuntimeException e) {
		if (typeResourceServices != null) {
			typeResourceServices.getOperationCanceledManager().propagateAsErrorIfCancelException(e);
		}
		LOG.error("Error initializing type "+typeResource.getURI(), e);
		throw e;
	}
	this.typeResource = typeResource;
}
 
Example #4
Source File: JdtTypeProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private JvmType findTypeBySignature(String signature, TypeResource resource, boolean traverseNestedTypes) {
	// TODO: Maybe iterate the resource without computing a fragment
	String fragment = typeUriHelper.getFragment(signature);
	JvmType result = (JvmType) resource.getEObject(fragment);
	if (result != null || !traverseNestedTypes)
		return result;
	List<EObject> contents = resource.getContents();
	if (contents.isEmpty()) {
		return null;
	}
	String rootTypeName = resource.getURI().segment(1);
	String nestedTypeName = fragment.substring(rootTypeName.length() + 1);
	List<String> segments = Strings.split(nestedTypeName, "$");
	EObject rootType = contents.get(0);
	if (rootType instanceof JvmDeclaredType) {
		result = findNestedType((JvmDeclaredType) rootType, segments, 0);
	}
	return result;
}
 
Example #5
Source File: ClasspathTypeProvider.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private JvmType findTypeByClass(BinaryClass clazz) {
	try {
		IndexedJvmTypeAccess indexedJvmTypeAccess = getIndexedJvmTypeAccess();
		URI resourceURI = clazz.getResourceURI();
		if (indexedJvmTypeAccess != null) {
			URI proxyURI = resourceURI.appendFragment(clazz.getURIFragment());
			EObject candidate = indexedJvmTypeAccess.getIndexedJvmType(proxyURI, getResourceSet());
			if (candidate instanceof JvmType)
				return (JvmType) candidate;
		}
		TypeResource result = (TypeResource) getResourceSet().getResource(resourceURI, true);
		return findTypeByClass(clazz, result);
	} catch(UnknownNestedTypeException e) {
		return null;
	}
}
 
Example #6
Source File: JdtTypeProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private boolean canLink(JvmType type) {
	Resource resource = type.eResource();
	if (resource instanceof TypeResource) {
		IMirror mirror = ((TypeResource) resource).getMirror();
		if (mirror instanceof JdtTypeMirror) {
			try {
				return canLink(((JdtTypeMirror) mirror).getMirroredType());
			} catch (JavaModelException e) {
				return false;
			}
		} else {
			return true;
		}
	}
	URI resourceURI = resource.getURI();
	if (resourceURI.isPlatformResource() && resourceURI.segment(1).equals(javaProject.getProject().getName())) {
		IndexedJvmTypeAccess indexedJvmTypeAccess = this.getIndexedJvmTypeAccess();
		if (indexedJvmTypeAccess != null && indexedJvmTypeAccess.isIndexingPhase(getResourceSet())) {
			return false;
		}
	}
	return true;
}
 
Example #7
Source File: JvmDeclaredTypeImplCustom.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void requestNotificationOnChange(Runnable listener) {
	Resource resource = eResource();
	if (resource instanceof TypeResource) {
		IMirror mirror = ((TypeResource) resource).getMirror();
		if (mirror instanceof IMirrorExtension) {
			if (((IMirrorExtension) mirror).isSealed())
				return;
		}
	}
	Notifier notifier = this;
	if (resource != null) {
		if (resource.getResourceSet() != null)
			notifier = resource.getResourceSet();
		else
			notifier = resource;
	}
	JvmTypeChangeDispatcher dispatcher = JvmTypeChangeDispatcher.findResourceChangeDispatcher(notifier);
	dispatcher.requestNotificationOnChange(this, listener);
}
 
Example #8
Source File: AbstractJvmTypeProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public TypeResource createResource(URI uri) {
	TypeResource result = doCreateResource(uri);
	final IMirror createMirror = createMirror(uri);
	if (createMirror !=  null) 
		result.setMirror(createMirror);
	return result;
}
 
Example #9
Source File: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected Object evaluateField(final XAbstractFeatureCall call, final JvmField field, final Context context) {
  if ((field.isSetConstant() || (field.eResource() instanceof TypeResource))) {
    boolean _isConstant = field.isConstant();
    if (_isConstant) {
      return field.getConstantValue();
    } else {
      String _simpleName = field.getDeclaringType().getSimpleName();
      String _plus = ("Field " + _simpleName);
      String _plus_1 = (_plus + ".");
      String _simpleName_1 = field.getSimpleName();
      String _plus_2 = (_plus_1 + _simpleName_1);
      String _plus_3 = (_plus_2 + " is not a constant");
      throw new ConstantExpressionEvaluationException(_plus_3);
    }
  }
  final XExpression expression = this.containerProvider.getAssociatedExpression(field);
  boolean _contains = context.getAlreadyEvaluating().contains(expression);
  if (_contains) {
    throw new ConstantExpressionEvaluationException("Endless recursive evaluation detected.");
  }
  try {
    final Map<String, JvmIdentifiableElement> visibleFeatures = this.findVisibleFeatures(expression);
    JvmTypeReference _type = field.getType();
    ClassFinder _classFinder = context.getClassFinder();
    Set<XExpression> _alreadyEvaluating = context.getAlreadyEvaluating();
    final Context ctx = new Context(_type, _classFinder, visibleFeatures, _alreadyEvaluating);
    return this.evaluate(expression, ctx);
  } catch (final Throwable _t) {
    if (_t instanceof ConstantExpressionEvaluationException) {
      final ConstantExpressionEvaluationException e = (ConstantExpressionEvaluationException)_t;
      throw new StackedConstantExpressionEvaluationException(call, field, e);
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
}
 
Example #10
Source File: ClasspathTypeProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public TypeResource createResource(URI uri) {
	String qualifiedName = uri.lastSegment();
	if (qualifiedName.lastIndexOf('$') != -1) {
		String outermostClassName = new BinaryClass(qualifiedName, classLoader).getOutermostClassName();
		return super.createResource(URIHelperConstants.OBJECTS_URI.appendSegment(outermostClassName));
	}
	return super.createResource(uri);
}
 
Example #11
Source File: JvmDeclaredTypeSignatureHashProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public String getHash(final JvmDeclaredType type) {
	if(type.eResource() instanceof TypeResource) {
		IMirror mirror = ((TypeResource)type.eResource()).getMirror();
		if(mirror instanceof IMirrorExtension && ((IMirrorExtension) mirror).isSealed())
			return type.getIdentifier();
	}
	return cache.get(Tuples.create(HASH_CACHE_KEY, type), type.eResource(), new Provider<String>() {
		@Override
		public String get() {
			return signatureBuilderProvider.get().appendSignature(type).hash();
		}
	});
}
 
Example #12
Source File: JdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCreateResource_01() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource resource = typeProvider.createResource(primitivesURI);
	assertNotNull(resource);
	assertFalse(resource.isLoaded());
	assertTrue(resource.getContents().isEmpty());
}
 
Example #13
Source File: JdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCreateResource_02() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource resource = (TypeResource) resourceSet.createResource(primitivesURI);
	assertNotNull(resource);
	assertFalse(resource.isLoaded());
	assertTrue(resource.getContents().isEmpty());
}
 
Example #14
Source File: JdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testGetResource_01() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource resource = (TypeResource) resourceSet.getResource(primitivesURI, true);
	assertNotNull(resource);
	assertTrue(resource.isLoaded());
	assertEquals(9, resource.getContents().size());
}
 
Example #15
Source File: JdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testGetResource_03() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource createdResource = (TypeResource) resourceSet.createResource(primitivesURI);
	TypeResource resource = (TypeResource) resourceSet.getResource(primitivesURI, false);
	assertSame(createdResource, resource);
	assertFalse(resource.isLoaded());
	assertTrue(resource.getContents().isEmpty());
}
 
Example #16
Source File: JdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testGetResource_04() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource createdResource = (TypeResource) resourceSet.createResource(primitivesURI);
	TypeResource resource = (TypeResource) resourceSet.getResource(primitivesURI, true);
	assertSame(createdResource, resource);
	assertTrue(resource.isLoaded());
	assertEquals(9, resource.getContents().size());
}
 
Example #17
Source File: JvmOutlineNodeElementOpener.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void openEObject(EObject state) {
	try {
		if (state instanceof JvmIdentifiableElement && state.eResource() instanceof TypeResource) {
			IJavaElement javaElement = javaElementFinder.findElementFor((JvmIdentifiableElement) state);
			if (javaElement instanceof IMember) {
				IResource resource = javaElement.getResource();
				if (resource instanceof IStorage) {
					ITrace traceToSource = traceInformation.getTraceToSource((IStorage) resource);
					if (traceToSource != null) {
						ISourceRange sourceRange = ((IMember) javaElement).getSourceRange();
						ILocationInResource sourceInformation = traceToSource.getBestAssociatedLocation(new TextRegion(sourceRange.getOffset(), sourceRange.getLength()));
						if (sourceInformation != null) {
							globalURIEditorOpener.open(sourceInformation.getAbsoluteResourceURI().getURI(), javaElement, true);
							return;
						}
					}
				}
				globalURIEditorOpener.open(null, javaElement, true);
				return;
			}
		}
	} catch (Exception exc) {
		LOG.error("Error opening java editor", exc);
	}
	super.openEObject(state);
}
 
Example #18
Source File: XbaseHoverProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isValidationDisabled(EObject objectToView) {
	Resource resource = objectToView.eResource();
	if (resource instanceof XtextResource)
		return ((XtextResource)resource).isValidationDisabled();
	// If this is not done links to native java types can be navigated in the hover
	if(resource instanceof TypeResource)
		return false;
	return true; 
}
 
Example #19
Source File: JdtTypeProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private JvmType findLoadedOrDerivedObjectType(/* @NonNull */ String signature, /* @NonNull */ URI resourceURI,
		/* @Nullable */ TypeResource resource, boolean traverseNestedTypes) throws UnknownNestedTypeException {
	JvmType result = resource != null ? findTypeBySignature(signature, resource, traverseNestedTypes) : null;
	if (result != null) {
		return result;
	}
	result = findObjectTypeInIndex(signature, resourceURI);
	if (result != null) {
		return result;
	}
	return null;
}
 
Example #20
Source File: JdtTypeProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private TypeResource createResource(URI resourceURI, IType type) {
	TypeResource resource = doCreateResource(resourceURI);
	getResourceSet().getResources().add(resource);
	if (type.exists()) {
		IMirror mirror = createMirror(type);
		resource.setMirror(mirror);
	}
	return resource;
}
 
Example #21
Source File: LinkingXpectMethod.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Similar to {@link #linkedName(IStringExpectation, ICrossEReferenceAndEObject)} but concatenating the fully
 * qualified name again instead of using the qualified name provider, as the latter may not create a valid name for
 * non-globally available elements.
 * <p>
 * The qualified name created by retrieving all "name" properties of the target and its containers, using '/' as
 * separator.
 */
@Xpect
@ParameterParser(syntax = "('at' arg1=OFFSET)?")
public void linkedPathname(@StringExpectation IStringExpectation expectation,
		ICrossEReferenceAndEObject arg1) {
	EObject targetObject = (EObject) arg1.getEObject().eGet(arg1.getCrossEReference());
	if (targetObject == null) {
		Assert.fail("Reference is null");
		return; // to avoid warnings in the following
	}
	if (targetObject.eIsProxy())
		Assert.fail("Reference is a Proxy: " + ((InternalEObject) targetObject).eProxyURI());
	Resource targetResource = targetObject.eResource();
	if (targetResource instanceof TypeResource)
		targetResource = arg1.getEObject().eResource();
	if (!(targetResource instanceof XtextResource))
		Assert.fail("Referenced EObject is not in an XtextResource.");

	Deque<String> segments = new ArrayDeque<>();
	do {
		EStructuralFeature nameFeature = targetObject.eClass().getEStructuralFeature("name");
		if (nameFeature != null) {
			Object obj = targetObject.eGet(nameFeature);
			if (obj instanceof String) {
				segments.push((String) obj);
			}
		} else {
			if (targetObject instanceof NamedElement) {
				segments.push(((NamedElement) targetObject).getName());
			}
		}
		targetObject = targetObject.eContainer();
	} while (targetObject != null);
	String pathname = Joiner.on('/').join(segments);
	expectation.assertEquals(pathname);
}
 
Example #22
Source File: PrimitiveMirror.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void initialize(TypeResource typeResource) {
	for(Class<?> primitiveClass: Primitives.ALL_PRIMITIVE_TYPES) {
		JvmType type = typeFactory.createType(primitiveClass);
		typeResource.getContents().add(type);
	}
}
 
Example #23
Source File: ReflectionTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCreateResource_01() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource resource = getTypeProvider().createResource(primitivesURI);
	assertNotNull(resource);
	assertFalse(resource.isLoaded());
	assertTrue(resource.getContents().isEmpty());
}
 
Example #24
Source File: ReflectionTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCreateResource_02() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource resource = (TypeResource) resourceSet.createResource(primitivesURI);
	assertNotNull(resource);
	assertFalse(resource.isLoaded());
	assertTrue(resource.getContents().isEmpty());
}
 
Example #25
Source File: ReflectionTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGetResource_01() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource resource = (TypeResource) resourceSet.getResource(primitivesURI, true);
	assertNotNull(resource);
	assertTrue(resource.isLoaded());
	assertEquals(9, resource.getContents().size());
}
 
Example #26
Source File: ReflectionTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGetResource_03() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource createdResource = (TypeResource) resourceSet.createResource(primitivesURI);
	TypeResource resource = (TypeResource) resourceSet.getResource(primitivesURI, false);
	assertSame(createdResource, resource);
	assertFalse(resource.isLoaded());
	assertTrue(resource.getContents().isEmpty());
}
 
Example #27
Source File: ReflectionTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testGetResource_04() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource createdResource = (TypeResource) resourceSet.createResource(primitivesURI);
	TypeResource resource = (TypeResource) resourceSet.getResource(primitivesURI, true);
	assertSame(createdResource, resource);
	assertTrue(resource.isLoaded());
	assertEquals(9, resource.getContents().size());
}
 
Example #28
Source File: PrimitiveMirrorTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	callCount = 0;
	helper = new PrimitiveTypeFactory();
	mirror = new PrimitiveMirror(this);
	resource = new TypeResource();
	resource.setMirror(mirror);
}
 
Example #29
Source File: ClasspathTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCreateResource_01() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource resource = getTypeProvider().createResource(primitivesURI);
	assertNotNull(resource);
	assertFalse(resource.isLoaded());
	assertTrue(resource.getContents().isEmpty());
}
 
Example #30
Source File: ClasspathTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCreateResource_02() {
	URI primitivesURI = URI.createURI("java:/Primitives");
	TypeResource resource = (TypeResource) resourceSet.createResource(primitivesURI);
	assertNotNull(resource);
	assertFalse(resource.isLoaded());
	assertTrue(resource.getContents().isEmpty());
}