Search in sources :

Example 1 with ReflectExtensions

use of org.eclipse.xtext.xbase.lib.util.ReflectExtensions in project xtext-eclipse by eclipse.

the class ParametersContentAssistParserSanityTest method methodNames.

/**
 * Returns test data.
 */
@Parameters(name = "{0}")
public static Collection<Object[]> methodNames() {
    try {
        TwoParametersTestLanguageInjectorProvider injectorProvider = new TwoParametersTestLanguageInjectorProvider();
        try {
            injectorProvider.setupRegistry();
            Injector injector = injectorProvider.getInjector();
            DummyParser dummyParser = new DummyParser();
            TwoParametersTestLanguageGrammarAccess grammarAccess = injector.getInstance(TwoParametersTestLanguageGrammarAccess.class);
            RuleNames ruleNames = RuleNames.getRuleNames(grammarAccess.getGrammar(), false);
            RuleFilter ruleFilter = new RuleFilter();
            ruleFilter.setDiscardUnreachableRules(true);
            FlattenedGrammarAccess flattened = new FlattenedGrammarAccess(ruleNames, ruleFilter);
            Grammar flattenedGrammar = flattened.getFlattenedGrammar();
            dummyParser.setGrammarAccess(grammarAccess);
            dummyParser.setNameMappings(new TwoParametersTestLanguageParser.NameMappings(grammarAccess));
            dummyParser.getRuleName(null);
            Map<AbstractElement, String> nameMappings = new ReflectExtensions().get(dummyParser.getNameMappings(), "mappings");
            List<String> methodNames = Lists.newArrayList(nameMappings.values());
            Collections.sort(methodNames);
            Set<String> flattenedRuleNames = Sets.newHashSet(Lists.transform(flattenedGrammar.getRules(), r -> r.getName()));
            return Lists.transform(methodNames, (s) -> new Object[] { s, flattenedRuleNames });
        } finally {
            injectorProvider.restoreRegistry();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Arrays(java.util.Arrays) AbstractElement(org.eclipse.xtext.AbstractElement) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) TwoParametersTestLanguageGrammarAccess(org.eclipse.xtext.ui.tests.editor.contentassist.services.TwoParametersTestLanguageGrammarAccess) Lists(com.google.common.collect.Lists) TwoParametersTestLanguageInjectorProvider(org.eclipse.xtext.ui.tests.editor.contentassist.tests.TwoParametersTestLanguageInjectorProvider) Map(java.util.Map) RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Method(java.lang.reflect.Method) Parameterized(org.junit.runners.Parameterized) InternalTwoParametersTestLanguageParser(org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.internal.InternalTwoParametersTestLanguageParser) Parameter(org.junit.runners.Parameterized.Parameter) Collection(java.util.Collection) Set(java.util.Set) Test(org.junit.Test) Sets(com.google.common.collect.Sets) TwoParametersTestLanguageParser(org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.TwoParametersTestLanguageParser) Injector(com.google.inject.Injector) List(java.util.List) Grammar(org.eclipse.xtext.Grammar) ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) Assert(org.junit.Assert) Collections(java.util.Collections) ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) RuleNames(org.eclipse.xtext.xtext.RuleNames) AbstractElement(org.eclipse.xtext.AbstractElement) InternalTwoParametersTestLanguageParser(org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.internal.InternalTwoParametersTestLanguageParser) TwoParametersTestLanguageParser(org.eclipse.xtext.ui.tests.editor.contentassist.ide.contentassist.antlr.TwoParametersTestLanguageParser) Grammar(org.eclipse.xtext.Grammar) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) TwoParametersTestLanguageGrammarAccess(org.eclipse.xtext.ui.tests.editor.contentassist.services.TwoParametersTestLanguageGrammarAccess) Injector(com.google.inject.Injector) TwoParametersTestLanguageInjectorProvider(org.eclipse.xtext.ui.tests.editor.contentassist.tests.TwoParametersTestLanguageInjectorProvider) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Parameters(org.junit.runners.Parameterized.Parameters)

Example 2 with ReflectExtensions

use of org.eclipse.xtext.xbase.lib.util.ReflectExtensions in project xtext-eclipse by eclipse.

the class UriValidatorBug406208Test method testExtensionIsEncoded.

@Test
public void testExtensionIsEncoded() throws Exception {
    UriValidator validator = new UriValidator();
    new ReflectExtensions().set(validator, "registry", this);
    Assert.assertTrue(validator.isPossiblyManaged(this));
}
Also used : ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) UriValidator(org.eclipse.xtext.ui.resource.UriValidator) Test(org.junit.Test)

Example 3 with ReflectExtensions

use of org.eclipse.xtext.xbase.lib.util.ReflectExtensions in project n4js by eclipse.

the class ContentAssistParserSanityTest method methodNames.

/**
 * Returns test data.
 */
@Parameters(name = "{0}")
public static Collection<Object[]> methodNames() {
    try {
        N4JSInjectorProvider injectorProvider = new N4JSInjectorProvider();
        Injector injector = injectorProvider.getInjector();
        DummyParser dummyParser = new DummyParser();
        N4JSGrammarAccess grammarAccess = injector.getInstance(N4JSGrammarAccess.class);
        RuleNames ruleNames = RuleNames.getRuleNames(grammarAccess.getGrammar(), false);
        RuleFilter ruleFilter = new RuleFilter();
        ruleFilter.setDiscardUnreachableRules(true);
        FlattenedGrammarAccess flattened = new FlattenedGrammarAccess(ruleNames, ruleFilter);
        Grammar flattenedGrammar = flattened.getFlattenedGrammar();
        dummyParser.setGrammarAccess(grammarAccess);
        injectorProvider.restoreRegistry();
        dummyParser.getRuleName(null);
        Map<AbstractElement, String> nameMappings = new ReflectExtensions().get(dummyParser, "nameMappings");
        List<String> methodNames = Lists.newArrayList(nameMappings.values());
        Collections.sort(methodNames);
        Set<String> flattenedRuleNames = Sets.newHashSet(Lists.transform(flattenedGrammar.getRules(), r -> r.getName()));
        return Lists.transform(methodNames, (s) -> new Object[] { s, flattenedRuleNames });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : N4JSInjectorProvider(org.eclipse.n4js.N4JSInjectorProvider) Arrays(java.util.Arrays) AbstractElement(org.eclipse.xtext.AbstractElement) N4JSInjectorProvider(org.eclipse.n4js.N4JSInjectorProvider) N4JSParser(org.eclipse.n4js.ui.contentassist.antlr.N4JSParser) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) Lists(com.google.common.collect.Lists) InternalN4JSParser(org.eclipse.n4js.ui.contentassist.antlr.internal.InternalN4JSParser) Map(java.util.Map) RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Method(java.lang.reflect.Method) Parameterized(org.junit.runners.Parameterized) Parameter(org.junit.runners.Parameterized.Parameter) Collection(java.util.Collection) Set(java.util.Set) N4JSGrammarAccess(org.eclipse.n4js.services.N4JSGrammarAccess) Test(org.junit.Test) Sets(com.google.common.collect.Sets) Injector(com.google.inject.Injector) List(java.util.List) Grammar(org.eclipse.xtext.Grammar) ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) Assert(org.junit.Assert) Collections(java.util.Collections) ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) N4JSGrammarAccess(org.eclipse.n4js.services.N4JSGrammarAccess) RuleNames(org.eclipse.xtext.xtext.RuleNames) AbstractElement(org.eclipse.xtext.AbstractElement) Grammar(org.eclipse.xtext.Grammar) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) Injector(com.google.inject.Injector) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) Parameters(org.junit.runners.Parameterized.Parameters)

Example 4 with ReflectExtensions

use of org.eclipse.xtext.xbase.lib.util.ReflectExtensions in project xtext-eclipse by eclipse.

the class ProjectOpenedOrClosedListener method scheduleRemoveProjectJobIfNecessary.

/**
 * @since 2.8
 */
protected void scheduleRemoveProjectJobIfNecessary(IProject project, IResourceDelta delta) {
    try {
        ReflectExtensions reflector = new ReflectExtensions();
        Object oldInfo = reflector.get(delta, "oldInfo");
        Map<?, ?> natures = reflector.get(oldInfo, "natures");
        if (natures != null && natures.containsKey(XtextProjectHelper.NATURE_ID)) {
            scheduleRemoveProjectJob(project);
        }
    } catch (Exception e) {
        if (!reflectErrorLogged) {
            log.error("Scheduled unnecessary build due to reflective code failure", e);
            reflectErrorLogged = true;
        }
        scheduleRemoveProjectJob(project);
    }
}
Also used : ReflectExtensions(org.eclipse.xtext.xbase.lib.util.ReflectExtensions) CoreException(org.eclipse.core.runtime.CoreException)

Aggregations

ReflectExtensions (org.eclipse.xtext.xbase.lib.util.ReflectExtensions)4 Test (org.junit.Test)3 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 Injector (com.google.inject.Injector)2 Method (java.lang.reflect.Method)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 AbstractElement (org.eclipse.xtext.AbstractElement)2 Grammar (org.eclipse.xtext.Grammar)2 FlattenedGrammarAccess (org.eclipse.xtext.xtext.FlattenedGrammarAccess)2 RuleFilter (org.eclipse.xtext.xtext.RuleFilter)2 RuleNames (org.eclipse.xtext.xtext.RuleNames)2 Assert (org.junit.Assert)2 RunWith (org.junit.runner.RunWith)2 Parameterized (org.junit.runners.Parameterized)2