diff options
Diffstat (limited to 'extraplugins/codegen/org.eclipse.papyrus.texteditor.cdt/src/org/eclipse/papyrus/texteditor/cdt/sync/SyncCDTtoModel.java')
-rw-r--r-- | extraplugins/codegen/org.eclipse.papyrus.texteditor.cdt/src/org/eclipse/papyrus/texteditor/cdt/sync/SyncCDTtoModel.java | 189 |
1 files changed, 95 insertions, 94 deletions
diff --git a/extraplugins/codegen/org.eclipse.papyrus.texteditor.cdt/src/org/eclipse/papyrus/texteditor/cdt/sync/SyncCDTtoModel.java b/extraplugins/codegen/org.eclipse.papyrus.texteditor.cdt/src/org/eclipse/papyrus/texteditor/cdt/sync/SyncCDTtoModel.java index 0cf80705fa6..fddeddc35d8 100644 --- a/extraplugins/codegen/org.eclipse.papyrus.texteditor.cdt/src/org/eclipse/papyrus/texteditor/cdt/sync/SyncCDTtoModel.java +++ b/extraplugins/codegen/org.eclipse.papyrus.texteditor.cdt/src/org/eclipse/papyrus/texteditor/cdt/sync/SyncCDTtoModel.java @@ -71,9 +71,9 @@ import org.eclipse.uml2.uml.UMLPackage; public class SyncCDTtoModel implements Runnable { public static final String sAtParam = "@param"; //$NON-NLS-1$ - + public static final String ansiCLib = "AnsiCLibrary"; //$NON-NLS-1$ - + public SyncCDTtoModel(IEditorInput input, Classifier classifier, String projectName) { m_input = input; m_classifier = classifier; @@ -86,11 +86,12 @@ public class SyncCDTtoModel implements Runnable { CommandSupport.exec("update model from CDT", this); } + @Override public void run() { ICElement ice = CDTUITools.getEditorInputCElement(m_input); ModelListener.syncFromEditor = true; - if(ice instanceof ITranslationUnit) { + if (ice instanceof ITranslationUnit) { ICProject project = CoreModel.getDefault().getCModel().getCProject(m_projectName); IIndex index = null; @@ -99,7 +100,7 @@ public class SyncCDTtoModel implements Runnable { index.acquireReadLock(); // index = CCorePlugin.getIndexManager().getIndex(project); - ITranslationUnit itu = (ITranslationUnit)ice; + ITranslationUnit itu = (ITranslationUnit) ice; // hack: force re-evaluation of AST node, requires modified CDT! // Seems to be no longer required. // ASTProvider.getASTProvider().fCache.setActiveElement(itu); @@ -111,10 +112,10 @@ public class SyncCDTtoModel implements Runnable { updateCppInclude(itu); CUIPlugin.getDefault().getProblemMarkerManager(); - if(itu instanceof IWorkingCopy) { + if (itu instanceof IWorkingCopy) { // ((IWorkingCopy)itu).commit(true, new NullProgressMonitor()); // ((IWorkingCopy)itu).reconcile(); - ((IWorkingCopy)itu).reconcile(true, new NullProgressMonitor()); + ((IWorkingCopy) itu).reconcile(true, new NullProgressMonitor()); // ((IWorkingCopy)itu).reconcile(true, true, new NullProgressMonitor()); } @@ -123,7 +124,7 @@ public class SyncCDTtoModel implements Runnable { } catch (Exception e) { System.err.println(e); } finally { - if(index != null) { + if (index != null) { index.releaseReadLock(); } } @@ -134,7 +135,7 @@ public class SyncCDTtoModel implements Runnable { /** * Examine the children of a translation unit in order to extract the methods that are defined within * hte unit - * + * * @param itu * @param selector * @param parent @@ -144,20 +145,20 @@ public class SyncCDTtoModel implements Runnable { int position = 0; // if (parent instanceof Namespace) { - for(ICElement child : parent.getChildren()) { - if(child instanceof IParent) { - examineChildren(itu, selector, (IParent)child); + for (ICElement child : parent.getChildren()) { + if (child instanceof IParent) { + examineChildren(itu, selector, (IParent) child); } ISourceRange range = null; - if(child instanceof ISourceReference) { - range = ((ISourceReference)child).getSourceRange(); + if (child instanceof ISourceReference) { + range = ((ISourceReference) child).getSourceRange(); } - if(child instanceof IFunctionDeclaration) { + if (child instanceof IFunctionDeclaration) { // function declaration is a superclass for method declaration (but need to trace functions differently?) - String name = ((IFunctionDeclaration)child).getElementName(); + String name = ((IFunctionDeclaration) child).getElementName(); IASTNode node = selector.findEnclosingNode(range.getStartPos(), range.getLength()); - if(node instanceof IASTFunctionDefinition) { - IASTFunctionDefinition definition = (IASTFunctionDefinition)node; + if (node instanceof IASTFunctionDefinition) { + IASTFunctionDefinition definition = (IASTFunctionDefinition) node; IASTFunctionDeclarator declarator = definition.getDeclarator(); String body = getBody(itu, definition); Operation operation = updateMethod(position, parent, name, body, declarator); @@ -171,9 +172,9 @@ public class SyncCDTtoModel implements Runnable { /** * update the contents of the CppInclude directive - * + * * @param itu - * the translation unit + * the translation unit */ public void updateCppInclude(ITranslationUnit itu) { String contents = new String(itu.getContents()); @@ -181,9 +182,9 @@ public class SyncCDTtoModel implements Runnable { int preBodyEnd = contents.indexOf(Constants.cppIncPreBodyEnd); String preBody = ""; //$NON-NLS-1$ String body = ""; //$NON-NLS-1$ - if(preBodyStart != -1) { + if (preBodyStart != -1) { preBodyStart += Constants.cppIncPreBodyStart.length(); - if(preBodyEnd > preBodyStart) { + if (preBodyEnd > preBodyStart) { preBody = contents.substring(preBodyStart, preBodyEnd).trim(); } } @@ -191,13 +192,13 @@ public class SyncCDTtoModel implements Runnable { int bodyStart = contents.indexOf(Constants.cppIncBodyStart); int bodyEnd = contents.indexOf(Constants.cppIncBodyEnd); - if(bodyStart != -1) { + if (bodyStart != -1) { bodyStart += Constants.cppIncBodyStart.length() + 1; - if(bodyEnd > bodyStart) { + if (bodyEnd > bodyStart) { body = contents.substring(bodyStart, bodyEnd).trim(); } } - if(body.length() > 0 || preBody.length() > 0) { + if (body.length() > 0 || preBody.length() > 0) { Include Include = StereotypeUtil.applyApp(m_classifier, Include.class); Include.setPreBody(preBody); Include.setBody(body); @@ -206,17 +207,17 @@ public class SyncCDTtoModel implements Runnable { /** * Update a method in the model while passing the qualified name - * + * * @param position - * The position of the method within the file. Used to identify renaming operations + * The position of the method within the file. Used to identify renaming operations * @param parent - * the CDT parent which is used to get a list of children + * the CDT parent which is used to get a list of children * @param qualifiedName - * the qualified name of a method + * the qualified name of a method * @param body - * the method body + * the method body * @param declarator - * the declarator for the method + * the declarator for the method */ public Operation updateMethod(int position, IParent parent, String qualifiedName, String body, IASTFunctionDeclarator declarator) { @@ -224,19 +225,19 @@ public class SyncCDTtoModel implements Runnable { String name = names[names.length - 1]; Operation operation = m_classifier.getOperation(name, null, null); - if(operation == null) { + if (operation == null) { // operation is not found via name in the model. That does not necessarily mean that this is a new method. // It may also have been renamed. // Strategy: try to locate the operation in the model at the same "position" as the method in the file and // verify that this method does not have the same name as any method in the CDT file. - if(position < m_classifier.getOperations().size()) { + if (position < m_classifier.getOperations().size()) { operation = m_classifier.getOperations().get(position); String modelName = operation.getName(); try { - for(ICElement child : parent.getChildren()) { - if(child instanceof IMethodDeclaration) { - String cdtName = ((IMethodDeclaration)child).getElementName(); - if(cdtName.equals(modelName)) { + for (ICElement child : parent.getChildren()) { + if (child instanceof IMethodDeclaration) { + String cdtName = ((IMethodDeclaration) child).getElementName(); + if (cdtName.equals(modelName)) { // an existing operation in the CDT file already has this name operation = null; break; @@ -246,36 +247,36 @@ public class SyncCDTtoModel implements Runnable { } catch (CModelException e) { } } - if(operation != null) { + if (operation != null) { operation.setName(name); } else { // still null => create new operation in model - if(m_classifier instanceof Class) { - operation = ((Class)m_classifier).createOwnedOperation(name, null, null); + if (m_classifier instanceof Class) { + operation = ((Class) m_classifier).createOwnedOperation(name, null, null); } - else if(m_classifier instanceof DataType) { - operation = ((DataType)m_classifier).createOwnedOperation(name, null, null); + else if (m_classifier instanceof DataType) { + operation = ((DataType) m_classifier).createOwnedOperation(name, null, null); } } } OpaqueBehavior ob = null; - if(operation.getMethods().size() == 0) { + if (operation.getMethods().size() == 0) { // does not exist, create - if(m_classifier instanceof Class) { - ob = (OpaqueBehavior)((Class)m_classifier).createOwnedBehavior(name, UMLPackage.eINSTANCE.getOpaqueBehavior()); + if (m_classifier instanceof Class) { + ob = (OpaqueBehavior) ((Class) m_classifier).createOwnedBehavior(name, UMLPackage.eINSTANCE.getOpaqueBehavior()); } - else if(m_classifier instanceof DataType) { - //ob = (OpaqueBehavior) ((DataType) m_classifier).createOwnedBehavior(name, UMLPackage.eINSTANCE.getOpaqueBehavior()); + else if (m_classifier instanceof DataType) { + // ob = (OpaqueBehavior) ((DataType) m_classifier).createOwnedBehavior(name, UMLPackage.eINSTANCE.getOpaqueBehavior()); } ob.setSpecification(operation); ob.setIsReentrant(false); } else { - ob = (OpaqueBehavior)operation.getMethods().get(0); - if(!ob.getName().equals(name)) { + ob = (OpaqueBehavior) operation.getMethods().get(0); + if (!ob.getName().equals(name)) { ob.setName(name); } } @@ -283,24 +284,24 @@ public class SyncCDTtoModel implements Runnable { // a parameters.clear() is not sufficient. Otherwise stereotype applications to unresolved elements remain in the model UMLUtil.destroyElements(operation.getOwnedParameters()); UMLUtil.destroyElements(ob.getOwnedParameters()); - - for(IASTNode declaratorChild : declarator.getChildren()) { - if(declaratorChild instanceof IASTParameterDeclaration) { - IASTParameterDeclaration parameter = (IASTParameterDeclaration)declaratorChild; + + for (IASTNode declaratorChild : declarator.getChildren()) { + if (declaratorChild instanceof IASTParameterDeclaration) { + IASTParameterDeclaration parameter = (IASTParameterDeclaration) declaratorChild; IASTName parameterName = parameter.getDeclarator().getName(); IASTDeclSpecifier parameterType = parameter.getDeclSpecifier(); boolean isPointer = false; boolean isRef = false; - String array = ""; //$NON-NLS-1$ + String array = ""; //$NON-NLS-1$ String parameterTypeName = ""; //$NON-NLS-1$ try { IToken token = parameter.getDeclarator().getSyntax(); - while(token != null) { + while (token != null) { String tokenStr = token.toString(); - if(tokenStr.equals("*")) { //$NON-NLS-1$ + if (tokenStr.equals("*")) { //$NON-NLS-1$ isPointer = true; } - else if(tokenStr.equals("&")) { //$NON-NLS-1$ + else if (tokenStr.equals("&")) { //$NON-NLS-1$ isRef = true; } else if (tokenStr.equals("[")) { //$NON-NLS-1$ @@ -312,25 +313,25 @@ public class SyncCDTtoModel implements Runnable { break; } } - token = token.getNext(); + token = token.getNext(); } token = parameterType.getSyntax(); - while(token != null) { + while (token != null) { String tokenStr = token.toString(); - if(tokenStr.equals("*")) { //$NON-NLS-1$ + if (tokenStr.equals("*")) { //$NON-NLS-1$ // TODO: check, if this can be called (depending on * position with different semantics?) isPointer = true; } - else if(tokenStr.equals("&")) { //$NON-NLS-1$ + else if (tokenStr.equals("&")) { //$NON-NLS-1$ isRef = true; } - else if(tokenStr.equals("const")) { //$NON-NLS-1$ + else if (tokenStr.equals("const")) { //$NON-NLS-1$ // do nothing (use isConst() operation of parameterType) // is not part of parameter type } else { - if(parameterTypeName.length() > 0) { + if (parameterTypeName.length() > 0) { parameterTypeName += " "; //$NON-NLS-1$ } parameterTypeName += tokenStr; @@ -341,49 +342,49 @@ public class SyncCDTtoModel implements Runnable { } NamedElement umlParameterType = Utils.getQualifiedElement(Utils.getTop(m_classifier), parameterTypeName); - if(umlParameterType == null) { + if (umlParameterType == null) { umlParameterType = Utils.getQualifiedElement(Utils.getTop(m_classifier), ansiCLib + Utils.nsSep + parameterTypeName); } - if(parameterType.isRestrict()) { + if (parameterType.isRestrict()) { } Parameter umlParameter = null; - if(umlParameterType instanceof Type) { - umlParameter = operation.createOwnedParameter(parameterName.toString(), (Type)umlParameterType); - ob.createOwnedParameter(parameterName.toString(), (Type)umlParameterType); + if (umlParameterType instanceof Type) { + umlParameter = operation.createOwnedParameter(parameterName.toString(), (Type) umlParameterType); + ob.createOwnedParameter(parameterName.toString(), (Type) umlParameterType); } else { umlParameter = operation.createOwnedParameter(parameterName.toString(), null); ob.createOwnedParameter(parameterName.toString(), null); } - if(parameterType.isConst()) { + if (parameterType.isConst()) { StereotypeUtil.apply(umlParameter, Const.class); } - if(parameterType.isVolatile()) { + if (parameterType.isVolatile()) { StereotypeUtil.apply(umlParameter, Volatile.class); } - if(isPointer) { + if (isPointer) { StereotypeUtil.apply(umlParameter, Ptr.class); } - else if(isRef) { + else if (isRef) { StereotypeUtil.apply(umlParameter, Ref.class); } - if(array.length() > 0) { + if (array.length() > 0) { Array arraySt = StereotypeUtil.applyApp(umlParameter, Array.class); - if (!array.equals("[]") && (!array.equals("[ ]"))) { //$NON-NLS-1$//$NON-NLS-2$ + if (!array.equals("[]") && (!array.equals("[ ]"))) { //$NON-NLS-1$//$NON-NLS-2$ arraySt.setDefinition(array); } } - + } } - if(ob.getBodies().size() == 0) { + if (ob.getBodies().size() == 0) { ob.getLanguages().add(langID); ob.getBodies().add(""); //$NON-NLS-1$ } - for(int i = 0; i < ob.getLanguages().size(); i++) { - if(ob.getLanguages().get(i).equals(langID)) { - if(i < ob.getBodies().size()) { // should always be true, unless sync between languages/bodies is lost + for (int i = 0; i < ob.getLanguages().size(); i++) { + if (ob.getLanguages().get(i).equals(langID)) { + if (i < ob.getBodies().size()) { // should always be true, unless sync between languages/bodies is lost ob.getBodies().set(i, body); } } @@ -394,8 +395,8 @@ public class SyncCDTtoModel implements Runnable { public static String getBody(ITranslationUnit itu, IASTFunctionDefinition definition) { IASTStatement body = definition.getBody(); - if(body instanceof IASTCompoundStatement) { - IASTCompoundStatement bodyComp = (IASTCompoundStatement)body; + if (body instanceof IASTCompoundStatement) { + IASTCompoundStatement bodyComp = (IASTCompoundStatement) body; IASTFileLocation bodyLoc = bodyComp.getFileLocation(); int start = bodyLoc.getNodeOffset(); @@ -415,54 +416,54 @@ public class SyncCDTtoModel implements Runnable { int end = start; char contents[] = itu.getContents(); String comment = ""; //$NON-NLS-1$ - // backward scan for beginning /* - while(start > 0) { - if(contents[start] == '/' && contents[start + 1] == '*') { + // backward scan for beginning /* + while (start > 0) { + if (contents[start] == '/' && contents[start + 1] == '*') { start += "/**".length(); // TODO: common string constants with generator //$NON-NLS-1$ - for(int i = start; i < end; i++) { + for (int i = start; i < end; i++) { comment += contents[i]; } - comment = comment.replace("\n * ", "\n"). //$NON-NLS-1$//$NON-NLS-2$ - replace("*/", "").trim(); //$NON-NLS-1$//$NON-NLS-2$ + comment = comment.replace("\n * ", "\n"). //$NON-NLS-1$//$NON-NLS-2$ + replace("*/", "").trim(); //$NON-NLS-1$//$NON-NLS-2$ break; } start--; } - if(comment.length() > 0) { + if (comment.length() > 0) { // filter @param int atParam = comment.indexOf(sAtParam); String commentMethodOnly = (atParam != -1) ? comment.substring(0, atParam).trim() : comment; EList<Comment> commentsUML = operation.getOwnedComments(); Comment commentUML; - if(commentsUML.size() == 0) { + if (commentsUML.size() == 0) { commentUML = operation.createOwnedComment(); commentUML.getAnnotatedElements().add(commentUML); } else { commentUML = commentsUML.get(0); } - while(atParam != -1) { + while (atParam != -1) { int currentAtParam = atParam; atParam = comment.indexOf(sAtParam, atParam + 1); String commentParam = (atParam != -1) ? comment.substring(currentAtParam, atParam) : comment.substring(currentAtParam); Comment commentParamUML; int atParamName = sAtParam.length(); - while((atParamName < commentParam.length()) && Character.isWhitespace(commentParam.charAt(atParamName))) { + while ((atParamName < commentParam.length()) && Character.isWhitespace(commentParam.charAt(atParamName))) { atParamName++; } int atParamNameEnd = atParamName; - while((atParamNameEnd < commentParam.length()) && !Character.isWhitespace(commentParam.charAt(atParamNameEnd))) { + while ((atParamNameEnd < commentParam.length()) && !Character.isWhitespace(commentParam.charAt(atParamNameEnd))) { atParamNameEnd++; } - if(atParamNameEnd < commentParam.length() - 1) { + if (atParamNameEnd < commentParam.length() - 1) { String parameterName = commentParam.substring(atParamName, atParamNameEnd); String commentParamText = commentParam.substring(atParamNameEnd).trim(); Parameter parameter = operation.getOwnedParameter(parameterName, null, false, false); - if(parameter != null) { + if (parameter != null) { EList<Comment> commentsParamUML = parameter.getOwnedComments(); - if(commentsParamUML.size() == 0) { + if (commentsParamUML.size() == 0) { commentParamUML = parameter.createOwnedComment(); commentParamUML.getAnnotatedElements().add(commentParamUML); } |