diff options
author | Ryan D. Brooks | 2016-09-28 21:09:48 +0000 |
---|---|---|
committer | Ryan D. Brooks | 2020-12-15 21:45:07 +0000 |
commit | ce72f9b5e75311d2aa97c3f417334be6188e6e37 (patch) | |
tree | 5b6c910a7033c6d71590825172656b8fc5defc7e | |
parent | 313a28fdfc5a2c19da50db29d4de6ce9b4d1ecf6 (diff) | |
download | org.eclipse.osee-ce72f9b5e75311d2aa97c3f417334be6188e6e37.tar.gz org.eclipse.osee-ce72f9b5e75311d2aa97c3f417334be6188e6e37.tar.xz org.eclipse.osee-ce72f9b5e75311d2aa97c3f417334be6188e6e37.zip |
refinement: Add rule to create static imports
Change-Id: Ic595a3686739d99a3262396c079666e2e76eb6b2
-rw-r--r-- | plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/text/rules/StaticImport.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/text/rules/StaticImport.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/text/rules/StaticImport.java new file mode 100644 index 00000000000..45e4438e648 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/text/rules/StaticImport.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2016 Boeing. + * 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: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.text.rules; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.eclipse.osee.framework.jdk.core.text.Rule; +import org.eclipse.osee.framework.jdk.core.text.change.ChangeSet; + +/** + * @author Ryan D. Brooks + */ +public class StaticImport extends Rule { + private static final Pattern importP = Pattern.compile("[\n]import "); + private final Pattern staticP; + private final Set<String> constants = new HashSet<>(); + private final String importPrefix; + + public StaticImport(String packageName, String staticClassName) { + super(null); // don't change extension on resulting file (i.e. overwrite the original file) + this.staticP = Pattern.compile("[^.](" + staticClassName + "\\.)(\\w+)"); + importPrefix = "import static " + packageName + "." + staticClassName + "."; + } + + @Override + public ChangeSet computeChanges(CharSequence seq) { + constants.clear(); + Matcher staticM = staticP.matcher(seq); + ChangeSet changeSet = new ChangeSet(seq); + + while (staticM.find()) { + ruleWasApplicable = true; + changeSet.delete(staticM.start(1), staticM.end(1)); + constants.add(staticM.group(2)); + } + if (ruleWasApplicable) { + Matcher importM = importP.matcher(seq); + importM.find(); + int index = importM.start() + 1; + for (String constant : constants) { + changeSet.insertBefore(index, importPrefix + constant + ";\n"); + } + } + return changeSet; + } + + public static void main(String[] args) throws IOException { + new StaticImport("org.eclipse.osee.framework.core.enums", "RelationOrderBaseTypes").process( + new File("c:/UserData/git/org.eclipse.osee/plugins/")); + } +}
\ No newline at end of file |