Search in sources :

Example 1 with CompilationUnit

use of org.whole.lang.java.model.CompilationUnit in project whole by wholeplatform.

the class PojoUtilsTest method getCompilationUnits.

public static Misc getCompilationUnits(Class<?>... classes) {
    MiscEntityFactory mef = MiscEntityFactory.instance;
    Misc misc = mef.createMisc(0);
    for (int i = 0; i < classes.length; i++) {
        JavaClassTemplateFactory javaSourceTemplateFactory = new JavaClassTemplateFactory(classes[i]);
        CompilationUnit compilationUnit = javaSourceTemplateFactory.create();
        Any stageUpFragment = CommonsEntityAdapterFactory.createStageUpFragment(MiscEntityDescriptorEnum.Any, compilationUnit);
        misc.wAdd(stageUpFragment);
    }
    return misc;
}
Also used : MiscEntityFactory(org.whole.lang.misc.factories.MiscEntityFactory) CompilationUnit(org.whole.lang.java.model.CompilationUnit) JavaClassTemplateFactory(org.whole.lang.java.codebase.JavaClassTemplateFactory) Misc(org.whole.lang.misc.model.Misc) Any(org.whole.lang.misc.model.Any)

Example 2 with CompilationUnit

use of org.whole.lang.java.model.CompilationUnit in project whole by wholeplatform.

the class PojoUtilsTest method getCompilationUnits.

public static Misc getCompilationUnits(ITypeRoot... typeRoots) {
    MiscEntityFactory mef = MiscEntityFactory.instance;
    Misc misc = mef.createMisc(0);
    for (int i = 0; i < typeRoots.length; i++) {
        JavaSourceTemplateFactory javaSourceTemplateFactory = new JavaSourceTemplateFactory(typeRoots[i]);
        CompilationUnit compilationUnit = javaSourceTemplateFactory.create();
        Any stageUpFragment = CommonsEntityAdapterFactory.createStageUpFragment(MiscEntityDescriptorEnum.Any, compilationUnit);
        misc.wAdd(stageUpFragment);
    }
    return misc;
}
Also used : MiscEntityFactory(org.whole.lang.misc.factories.MiscEntityFactory) CompilationUnit(org.whole.lang.java.model.CompilationUnit) Misc(org.whole.lang.misc.model.Misc) JavaSourceTemplateFactory(org.whole.lang.java.codebase.JavaSourceTemplateFactory) Any(org.whole.lang.misc.model.Any)

Example 3 with CompilationUnit

use of org.whole.lang.java.model.CompilationUnit in project whole by wholeplatform.

the class WizardPojoModelImportPage1 method getCompilationUnits.

public static Misc getCompilationUnits(List<ITypeRoot> elements) {
    Map<String, List<String>> map = new HashMap<String, List<String>>();
    MiscEntityFactory mef = MiscEntityFactory.instance;
    Misc misc = mef.createMisc(0);
    elements = new ArrayList<ITypeRoot>(elements);
    Iterator<ITypeRoot> iterator = elements.iterator();
    while (iterator.hasNext()) {
        ITypeRoot typeRoot = iterator.next();
        IType type = typeRoot.findPrimaryType();
        try {
            boolean isMember = type.isMember();
            boolean isAnonymous = type.isAnonymous();
            if (isMember || isAnonymous)
                iterator.remove();
            if (!isMember || type.getElementName().contains("Impl"))
                continue;
        } catch (JavaModelException e) {
        }
        String declaringTypeName = type.getDeclaringType().getFullyQualifiedName();
        List<String> memeberTypes = map.get(declaringTypeName);
        if (memeberTypes == null)
            map.put(declaringTypeName, memeberTypes = new ArrayList<String>());
        memeberTypes.add(typeRoot.findPrimaryType().getElementName());
    }
    for (ITypeRoot typeRoot : elements) {
        Class<?> clazz;
        try {
            clazz = Class.forName(typeRoot.findPrimaryType().getFullyQualifiedName(), false, JDTUtils.createClassLoader(typeRoot.getJavaProject(), true));
            if (clazz.getAnnotation(Deprecated.class) != null)
                continue;
        } catch (ClassNotFoundException e) {
            continue;
        }
        JavaSourceTemplateFactory templateFactory = new JavaSourceTemplateFactory(typeRoot) {

            @Override
            protected void init(String sourceAttachment, String className, IJavaProject javaProject) {
                // FIXME implement import inlining
                super.init(/* ensure reflection is used */
                null, className, javaProject);
                if (clazz.getAnnotation(Deprecated.class) != null)
                    ;
            }
        };
        templateFactory.useCanonicalNames(false);
        CompilationUnit compilationUnit = templateFactory.create();
        String compilationUnitName = typeRoot.findPrimaryType().getFullyQualifiedName();
        if (map.containsKey(compilationUnitName)) {
            List<String> memberNames = map.get(compilationUnitName);
            BodyDeclarations bodyDeclarations = compilationUnit.getTypes().get(0).getBodyDeclarations();
            IEntityIterator<TypeDeclaration> entityIterator = IteratorFactory.<TypeDeclaration>childMatcherIterator().withPattern(JavaEntityDescriptorEnum.TypeDeclaration);
            entityIterator.reset(bodyDeclarations);
            while (entityIterator.hasNext()) if (!memberNames.contains(entityIterator.next().getName().getValue()))
                entityIterator.remove();
        }
        Any stageUpFragment = CommonsEntityAdapterFactory.createStageUpFragment(MiscEntityDescriptorEnum.Any, compilationUnit);
        misc.wAdd(stageUpFragment);
    }
    return misc;
}
Also used : MiscEntityFactory(org.whole.lang.misc.factories.MiscEntityFactory) CompilationUnit(org.whole.lang.java.model.CompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) HashMap(java.util.HashMap) Misc(org.whole.lang.misc.model.Misc) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) JavaSourceTemplateFactory(org.whole.lang.java.codebase.JavaSourceTemplateFactory) BodyDeclarations(org.whole.lang.java.model.BodyDeclarations) Any(org.whole.lang.misc.model.Any) IType(org.eclipse.jdt.core.IType) IJavaProject(org.eclipse.jdt.core.IJavaProject) ArrayList(java.util.ArrayList) List(java.util.List) TypeDeclaration(org.whole.lang.java.model.TypeDeclaration)

Example 4 with CompilationUnit

use of org.whole.lang.java.model.CompilationUnit in project whole by wholeplatform.

the class IteratorFactoryTest method testFragmentRootIterator.

@Test
public void testFragmentRootIterator() throws Exception {
    IEntity e = XmlBuilderPersistenceKit.instance().readModel(new ClasspathPersistenceProvider("org/whole/lang/templates/codebase/TemplateManagerArtifactsTemplates.xwl"));
    CompilationUnit cu = Matcher.find(JavaEntityDescriptorEnum.CompilationUnit, e, false);
    ImportModifier im = Matcher.find(JavaEntityDescriptorEnum.ImportModifier, e, false);
    IEntityIterator<IEntity> i1 = IteratorFactory.rootIterator();
    i1.reset(im);
    assertTrue(i1.hasNext());
    assertSame(e, i1.next());
    IEntityIterator<IEntity> i2 = IteratorFactory.fragmentRootIterator();
    i2.reset(im);
    assertTrue(i2.hasNext());
    assertSame(cu, i2.next());
}
Also used : CompilationUnit(org.whole.lang.java.model.CompilationUnit) IEntity(org.whole.lang.model.IEntity) ImportModifier(org.whole.lang.java.model.ImportModifier) ClasspathPersistenceProvider(org.whole.lang.codebase.ClasspathPersistenceProvider) Test(org.junit.Test)

Example 5 with CompilationUnit

use of org.whole.lang.java.model.CompilationUnit in project whole by wholeplatform.

the class CompilationUnitPart method getModelSpecificChildren.

protected List<IEntity> getModelSpecificChildren() {
    CompilationUnit compilationUnit = getModelEntity();
    List<IEntity> list = new ArrayList<IEntity>(3);
    list.add(compilationUnit.getPackage());
    list.add(compilationUnit.getImports());
    list.add(compilationUnit.getTypes());
    return list;
}
Also used : CompilationUnit(org.whole.lang.java.model.CompilationUnit) IEntity(org.whole.lang.model.IEntity) ArrayList(java.util.ArrayList)

Aggregations

CompilationUnit (org.whole.lang.java.model.CompilationUnit)5 MiscEntityFactory (org.whole.lang.misc.factories.MiscEntityFactory)3 Any (org.whole.lang.misc.model.Any)3 Misc (org.whole.lang.misc.model.Misc)3 ArrayList (java.util.ArrayList)2 JavaSourceTemplateFactory (org.whole.lang.java.codebase.JavaSourceTemplateFactory)2 IEntity (org.whole.lang.model.IEntity)2 HashMap (java.util.HashMap)1 List (java.util.List)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IType (org.eclipse.jdt.core.IType)1 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 Test (org.junit.Test)1 ClasspathPersistenceProvider (org.whole.lang.codebase.ClasspathPersistenceProvider)1 JavaClassTemplateFactory (org.whole.lang.java.codebase.JavaClassTemplateFactory)1 BodyDeclarations (org.whole.lang.java.model.BodyDeclarations)1 ImportModifier (org.whole.lang.java.model.ImportModifier)1 TypeDeclaration (org.whole.lang.java.model.TypeDeclaration)1