Search in sources :

Example 11 with CheckCatalog

use of com.avaloq.tools.ddk.check.check.CheckCatalog in project dsl-devkit by dsldevkit.

the class CheckCfgTemplateProposalProvider method addConfiguredCheckTemplates.

/**
 * Adds template proposals for all checks which may be referenced in current catalog configuration. Only proposals for checks
 * which have not yet been configured are provided.
 *
 * @param templateContext
 *          the template context
 * @param context
 *          the context
 * @param acceptor
 *          the acceptor
 */
private void addConfiguredCheckTemplates(final TemplateContext templateContext, final ContentAssistContext context, final ITemplateAcceptor acceptor) {
    // NOPMD
    ConfiguredCatalog configuredCatalog = EcoreUtil2.getContainerOfType(context.getCurrentModel(), ConfiguredCatalog.class);
    Iterable<String> alreadyConfiguredCheckNames = Iterables.filter(Iterables.transform(configuredCatalog.getCheckConfigurations(), new Function<ConfiguredCheck, String>() {

        @Override
        public String apply(final ConfiguredCheck from) {
            if (from.getCheck() != null) {
                return from.getCheck().getName();
            }
            return null;
        }
    }), Predicates.notNull());
    final CheckCatalog catalog = configuredCatalog.getCatalog();
    for (final Check check : catalog.getAllChecks()) {
        // create a template on the fly
        final String checkName = check.getName();
        if (!Iterables.contains(alreadyConfiguredCheckNames, checkName)) {
            // check if referenced check has configurable parameters
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            final StringJoiner paramsJoiner = new StringJoiner(", ", " (", ")");
            // $NON-NLS-1$
            paramsJoiner.setEmptyValue("");
            for (final FormalParameter param : check.getFormalParameters()) {
                final String paramName = param.getName();
                final Object defaultValue = interpreter.evaluate(param.getRight()).getResult();
                final String valuePlaceholder = helper.createLiteralValuePattern(paramName, defaultValue);
                // $NON-NLS-1$
                paramsJoiner.add(paramName + " = " + valuePlaceholder);
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            final String severity = (catalog.isFinal() || check.isFinal()) ? "default " : "${default:Enum('SeverityKind')} ";
            // $NON-NLS-1$ //$NON-NLS-2$
            final String description = "Configures the check \"" + check.getLabel() + "\"";
            // $NON-NLS-1$
            final String contextTypeId = "com.avaloq.tools.ddk.checkcfg.CheckCfg.ConfiguredCheck." + checkName;
            // $NON-NLS-1$
            final String pattern = severity + qualifiedNameValueConverter.toString(checkName) + paramsJoiner + "${cursor}";
            Template t = new Template(checkName, description, contextTypeId, pattern, true);
            TemplateProposal tp = createProposal(t, templateContext, context, images.forConfiguredCheck(check.getDefaultSeverity()), getRelevance(t));
            acceptor.accept(tp);
        }
    }
}
Also used : FormalParameter(com.avaloq.tools.ddk.check.check.FormalParameter) TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) Function(com.google.common.base.Function) ConfiguredCatalog(com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCatalog) ConfiguredCheck(com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCheck) Check(com.avaloq.tools.ddk.check.check.Check) ConfiguredCheck(com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCheck) CheckCatalog(com.avaloq.tools.ddk.check.check.CheckCatalog) StringJoiner(java.util.StringJoiner) Template(org.eclipse.jface.text.templates.Template)

Example 12 with CheckCatalog

use of com.avaloq.tools.ddk.check.check.CheckCatalog in project dsl-devkit by dsldevkit.

the class AbstractCheckGenerationTestCase method generateAndCompile.

/**
 * Generate and compile a Check.
 *
 * @param sourceStream
 *          stream containing the Check, must not be {@code null}
 * @return map of class name to compiled class, never {@code null}
 */
public Map<String, Class<?>> generateAndCompile(final InputStream sourceStream) {
    Validate.notNull(sourceStream, "Argument sourceStream may not be null");
    XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
    Resource res = resourceSet.createResource(URI.createURI("BugDsl27.check"));
    try {
        res.load(sourceStream, null);
    } catch (IOException e) {
        fail("Could not load test resource" + e.getMessage());
    }
    CheckCatalog root = (CheckCatalog) res.getContents().get(0);
    assertNotNull("Resource should contain a CheckCatalog", root);
    // We also should have some Jvm model here.
    JvmType type = null;
    for (EObject obj : res.getContents()) {
        if (obj instanceof JvmType) {
            type = (JvmType) obj;
            break;
        }
    }
    assertNotNull("Should have an inferred Jvm model", type);
    // Run the generator using an in-memory file system access
    InMemoryFileSystemAccess fsa = new InMemoryFileSystemAccess();
    for (OutputConfiguration output : outputConfigurationProvider.getOutputConfigurations()) {
        fsa.getOutputConfigurations().put(output.getName(), output);
    }
    generator.doGenerate(res, fsa);
    // We now should have a number of files.
    String baseName = root.getPackageName() + '.' + root.getName();
    String basePath = baseName.replace('.', '/');
    Map<String, String> sources = Maps.newHashMap();
    for (String name : GENERATED_FILES) {
        sources.put(baseName + name, fsa.readTextFile(basePath + name + ".java", IFileSystemAccess.DEFAULT_OUTPUT).toString());
    }
    // Compile the generated Java files. Raises an IllegalArgumentException if compilation failed.
    try {
        // Put together a classpath for the compiler. Since we don't know exactly what pathes would be in the transitive closure
        // that eclipse computes from the plugin dependencies, and we also cannot get conveniently at the pathes used by our
        // own class loader, let eclipse do the work: create our test project and then get the resolved classpath entries from
        // that.
        IProject project = getOrCreatePluginProject();
        IResourcesSetupUtil.waitForAutoBuild();
        // enumerateContents(project);
        IJavaProject javaProject = JavaCore.create(project);
        javaCompiler.clearClassPath();
        for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
            javaCompiler.addClassPath(entry.getPath().toString());
        }
        // Set our own class loader, otherwise the compiler fails. It can compile the sources, but then fails to load them,
        // as the implementation only can load from its temporary folder. Alternatively, we could create our own URLClassLoader
        // with all the classpathes we added above. The javaCompiler evidently is not intended to be used for sources that
        // reference external classes, but only for self-contained test sets. However, we don't have that here; our
        // generated classes do reference quite a few other classes.
        javaCompiler.setParentClassLoader(getClass().getClassLoader());
        final Map<String, Class<?>> compiledClasses = javaCompiler.compileToClasses(sources);
        assertEquals("All sources should have been compiled", sources.size(), compiledClasses.size());
        return compiledClasses;
    // CHECKSTYLE:OFF Yes, catch anything
    } catch (Exception e) {
        // CHECKSTYLE:ON
        fail("Java compilation failed: " + e.getMessage());
        return Collections.emptyMap();
    }
}
Also used : IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Resource(org.eclipse.emf.ecore.resource.Resource) InMemoryFileSystemAccess(org.eclipse.xtext.generator.InMemoryFileSystemAccess) IOException(java.io.IOException) JvmType(org.eclipse.xtext.common.types.JvmType) IProject(org.eclipse.core.resources.IProject) IOException(java.io.IOException) IJavaProject(org.eclipse.jdt.core.IJavaProject) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) OutputConfiguration(org.eclipse.xtext.generator.OutputConfiguration) EObject(org.eclipse.emf.ecore.EObject) CheckCatalog(com.avaloq.tools.ddk.check.check.CheckCatalog)

Example 13 with CheckCatalog

use of com.avaloq.tools.ddk.check.check.CheckCatalog in project dsl-devkit by dsldevkit.

the class CheckCatalogImpl method setIncludedCatalogs.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public void setIncludedCatalogs(CheckCatalog newIncludedCatalogs) {
    CheckCatalog oldIncludedCatalogs = includedCatalogs;
    includedCatalogs = newIncludedCatalogs;
    if (eNotificationRequired())
        eNotify(new ENotificationImpl(this, Notification.SET, CheckPackage.CHECK_CATALOG__INCLUDED_CATALOGS, oldIncludedCatalogs, includedCatalogs));
}
Also used : ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl) CheckCatalog(com.avaloq.tools.ddk.check.check.CheckCatalog)

Example 14 with CheckCatalog

use of com.avaloq.tools.ddk.check.check.CheckCatalog in project dsl-devkit by dsldevkit.

the class CheckMarkerHelpExtensionTest method testRemoveElement.

/**
 * Tests if a marker help element is removed if the corresponding check is deleted.
 *
 * @throws Exception
 *           the exception
 */
@Test
public void testRemoveElement() throws Exception {
    final CheckCatalog catalogWithTwoChecks = parser.parse(CATALOG_WITH_TWO_CHECKS);
    IPluginExtension extension = createMarkerHelpExtension(catalogWithTwoChecks);
    assertEquals("Original catalog has two marker help extensions", 2, extension.getChildCount());
    CheckCatalog catalogWithOneCheck = parser.parse(CATALOG_WITH_SECOND_CHECK_ONDEMAND);
    markerUtil.updateExtension(catalogWithOneCheck, extension);
    assertEquals("Updated catalog has one marker help extension only", 1, extension.getChildCount());
    assertEquals("The element for the removed check has been deleted.", SECONDCHECK_CONTEXTID, ((IPluginElement) extension.getChildren()[0]).getAttribute(CheckMarkerHelpExtensionHelper.CONTEXT_ID_ATTRIBUTE_TAG).getValue());
}
Also used : IPluginExtension(org.eclipse.pde.core.plugin.IPluginExtension) IPluginElement(org.eclipse.pde.core.plugin.IPluginElement) CheckCatalog(com.avaloq.tools.ddk.check.check.CheckCatalog) Test(org.junit.Test)

Example 15 with CheckCatalog

use of com.avaloq.tools.ddk.check.check.CheckCatalog in project dsl-devkit by dsldevkit.

the class CheckJavaValidator method checkCircularDependency.

/**
 * Checks catalogs for circular dependencies in included catalogs. A catalog cannot include itself and may not include another catalog which includes the
 * current one.
 *
 * @param catalog
 *          the catalog to be checked
 */
@Check
public void checkCircularDependency(final CheckCatalog catalog) {
    if (catalog.getIncludedCatalogs() == null) {
        return;
    }
    final Set<CheckCatalog> visitedCatalogs = Sets.newHashSet(catalog);
    CheckCatalog current = catalog.getIncludedCatalogs();
    while (current != null) {
        if (visitedCatalogs.add(current)) {
            current = current.getIncludedCatalogs();
        } else {
            error(Messages.CheckJavaValidator_CIRCULAR_DEPENDENCY_IN_INCLUDED_CATALOGS, catalog, CheckPackage.Literals.CHECK_CATALOG__INCLUDED_CATALOGS, IssueCodes.INCLUDED_CATALOGS_WITH_CIRCULAR_DEPENDENCIES);
            break;
        }
    }
}
Also used : CheckCatalog(com.avaloq.tools.ddk.check.check.CheckCatalog) Check(org.eclipse.xtext.validation.Check)

Aggregations

CheckCatalog (com.avaloq.tools.ddk.check.check.CheckCatalog)17 IPluginElement (org.eclipse.pde.core.plugin.IPluginElement)4 IPluginExtension (org.eclipse.pde.core.plugin.IPluginExtension)4 Test (org.junit.Test)4 Check (com.avaloq.tools.ddk.check.check.Check)3 ConfiguredCatalog (com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCatalog)3 ConfiguredCheck (com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCheck)3 FormalParameter (com.avaloq.tools.ddk.check.check.FormalParameter)2 CheckConfiguration (com.avaloq.tools.ddk.checkcfg.checkcfg.CheckConfiguration)2 Function (com.google.common.base.Function)2 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)2 Template (org.eclipse.jface.text.templates.Template)2 TemplateProposal (org.eclipse.jface.text.templates.TemplateProposal)2 IPluginObject (org.eclipse.pde.core.plugin.IPluginObject)2 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)2 Check (org.eclipse.xtext.validation.Check)2 Category (com.avaloq.tools.ddk.check.check.Category)1