Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [solved] Access eclipse src classes in validator
[solved] Access eclipse src classes in validator [message #837331] Thu, 05 April 2012 14:43 Go to next message
Thomas Hergenröder is currently offline Thomas Hergenröder
Messages: 22
Registered: February 2012
Junior Member
In our xtext language we reference java classes.
I want to create a validation that checks whether eclipse knows this class
In other words, ask eclipse whether there is a class of this qualified name (incl. package) in the workspace - in src or libraries.

Additionally (if possible) check if that class is not final so classes generated by the dsl can inherit from it.

I know of jvmType.
We decided not to use them so we could compile the whole project with maven.
If we used jvmtype, we had a cyclic dependency between the java-compiler and the dsl-compiler.
The above check would only be in eclipse not in maven.

Is there a way to get this information other than using jvmType?

[Updated on: Tue, 10 April 2012 09:41]

Report message to a moderator

Re: Access eclipse src classes in validator [message #837338 is a reply to message #837331] Thu, 05 April 2012 14:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6395
Registered: July 2009
Senior Member
Hi,

you can do the very same as jvm types does yourself. you have to dig into JDTs apis for that.
but questions about jdts apis are better asked in JDT group i think.

btw Xtends maven plugin can handle this cyclic problem so you may be able to do this as well.

~Christian
Re: Access eclipse src classes in validator [message #837362 is a reply to message #837338] Thu, 05 April 2012 15:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6395
Registered: July 2009
Senior Member
Hi,

just gave it a try and it is kind of a 20liner

Model:
	refs+=Ref*
;

Ref:
	"ref" ref=FQN
;
FQN: ID ("." ID)*;


@ImplementedBy(ITypeValidationHelper.NullImpl.class)
public interface ITypeValidationHelper {
	
	boolean validate(URI uri, String name);
	
	public static class NullImpl implements ITypeValidationHelper {

		@Override
		public boolean validate(URI uri, String name) {
			return true;
		}
		
	}

}


public class MyDslJavaValidator extends AbstractMyDslJavaValidator {
	
	@Inject
	ITypeValidationHelper tvh;

	@Check
	public void checkType(Ref ref) {
		if (!tvh.validate(ref.eResource().getURI(), ref.getRef())) {
			warning("type does not exist", MyDslPackage.Literals.REF__REF);
		}
	}

}


public class JdtTypeValidationHelper implements ITypeValidationHelper {
	
	@Inject
	IWorkspace workspace;

	@Override
	public boolean validate(URI uri, String name) {
		IProject project = workspace.getRoot().getFile(new Path(uri.toPlatformString(true))).getProject();
		IJavaProject javaProject = JavaCore.create(project);
		try {
			IType type = javaProject.findType(name);
			return type != null;
		} catch (JavaModelException e) {
			return false;
		}
	}

}


public class MyDslUiModule extends AbstractMyDslUiModule {
	public MyDslUiModule(AbstractUIPlugin plugin) {
		super(plugin);
	}
	
	public Class<? extends ITypeValidationHelper> bindITypeValidationHelper() {
		return JdtTypeValidationHelper.class;
	}
}
icon14.gif  Re: Access eclipse src classes in validator [message #840606 is a reply to message #837362] Tue, 10 April 2012 09:40 Go to previous message
Thomas Hergenröder is currently offline Thomas Hergenröder
Messages: 22
Registered: February 2012
Junior Member
thank you.
That solved it.

In case anyone has a similar problem:
final URI uri = javaType.eResource().getURI();
final String qualifiedName = javaType.getQualifiedName();
if (qualifiedName != null && uri != null) {
    final IJavaProject javaProject = JavaCore.create(workspace.getRoot().getFile(new Path(uri.toPlatformString(true))).getProject());
    final IType type = javaProject.findType(qualifiedName);
    if (type == null) {
        error(...);
    } else {
        if (Flags.isFinal(type.getFlags())) {
            error(...);
        }
    }
}

That code is included in the already existing validator.

[Updated on: Tue, 10 April 2012 09:42]

Report message to a moderator

Previous Topic:Partial Embedded Xtext Editor
Next Topic:Custom hover support with Xtext 2.3
Goto Forum:
  


Current Time: Thu Oct 30 15:56:41 GMT 2014

Powered by FUDForum. Page generated in 0.02048 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software