Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bbf82143decf6ca3f9b91f2b74cc08eecc7a2e1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*****************************************************************************
 * Copyright (c) 2011 Nicolas Deblock & Manuel Giles.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 	Nicolas Deblock  nico.deblock@gmail.com  - Initial API and implementation
 * 	Manuel Giles	 giles.manu@live.fr		 - Initial API and implementation
 * 	Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Idea of the java generator project & help for the conception 
 *
 *****************************************************************************/

package org.eclipse.papyrus.java.generator.jdtsynchronizer.impl;

import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.papyrus.java.generator.jdtsynchronizer.GeneratorPreference;
import org.eclipse.papyrus.java.generator.metamodel.jdt.jdtmm.JDTCompilationUnit;
import org.eclipse.papyrus.java.generator.metamodel.jdt.jdtmm.JDTJavaElement;
import org.eclipse.papyrus.java.generator.metamodel.jdt.jdtmm.JDTType;
import org.eclipse.papyrus.java.generator.metamodel.jdt.jdtmm.visitor.JDTVisitor;
import org.eclipse.papyrus.java.generator.metamodel.jdt.jdtmm.visitor.JDTVisitorException;

/**
 * Allow to generate compilation Unit
 * 
 * @author Deblock Nicolas & Manuel Giles
 * 
 */
public class SynchJDTCompilationUnit extends EObjectImpl implements JDTVisitor {

	private IPackageFragment ipack;

	private GeneratorPreference preference;

	/**
	 * Constructor
	 * 
	 * @param ipack
	 *        the IPackageFragment parent
	 */
	public SynchJDTCompilationUnit(IPackageFragment ipack, GeneratorPreference preference) {
		super();
		this.ipack = ipack;
		this.preference = preference;
	}

	public void visit(JDTJavaElement element) throws JDTVisitorException {
		// if element can't be generated, we stop all
		if(!element.isGenerated())
			return;
		if(SynchTools.isPrimiveType(element.getElementName()) && !element.getElementName().equals("String"))
			return;

		JDTCompilationUnit cu = (JDTCompilationUnit)element;
		try {
			ICompilationUnit icu = SynchTools.searchIJavaElement(ipack.getCompilationUnits(), cu.getElementName() + ".java");

			if(icu == null) {
				icu = ipack.createCompilationUnit(cu.getElementName() + ".java", "", true, null);
				if(!ipack.getElementName().equals(IPackageFragment.DEFAULT_PACKAGE_NAME))
					icu.createPackageDeclaration(ipack.getElementName(), null);
			}

			//call the children JDTType
			JDTVisitor visitor = new SynchJDTType(icu, preference);

			for(JDTType type : cu.getTypes())
				type.accept(visitor);

			// save the compilation unit
			icu.save(null, true);
		} catch (JavaModelException e) {
			e.printStackTrace();
			throw new JDTVisitorException(e.getMessage(), e.getCause());
		} catch (Exception e) {
			e.printStackTrace();
			throw new JDTVisitorException(e.getMessage(), e.getCause());
		}


	}

}

Back to the top