|
| 1 | +package JavaExtractor.Common; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.stream.Collectors; |
| 5 | +import java.util.stream.Stream; |
| 6 | + |
| 7 | +import com.github.javaparser.ast.Node; |
| 8 | +import com.github.javaparser.ast.UserDataKey; |
| 9 | + |
| 10 | +import JavaExtractor.FeaturesEntities.ProgramNode; |
| 11 | +import JavaExtractor.FeaturesEntities.Property; |
| 12 | + |
| 13 | +public final class Common { |
| 14 | + public static final UserDataKey<Property> PropertyKey = new UserDataKey<Property>() { |
| 15 | + }; |
| 16 | + public static final UserDataKey<ProgramNode> ProgramNodeKey = new UserDataKey<ProgramNode>() { |
| 17 | + }; |
| 18 | + public static final UserDataKey<Integer> ChildId = new UserDataKey<Integer>() { |
| 19 | + }; |
| 20 | + public static final String EmptyString = ""; |
| 21 | + public static final String UTF8 = "UTF-8"; |
| 22 | + public static final String EvaluateTempDir = "EvalTemp"; |
| 23 | + |
| 24 | + public static final String FieldAccessExpr = "FieldAccessExpr"; |
| 25 | + public static final String ClassOrInterfaceType = "ClassOrInterfaceType"; |
| 26 | + public static final String MethodDeclaration = "MethodDeclaration"; |
| 27 | + public static final String NameExpr = "NameExpr"; |
| 28 | + public static final String MethodCallExpr = "MethodCallExpr"; |
| 29 | + public static final String DummyNode = "DummyNode"; |
| 30 | + public static final String BlankWord = "BLANK"; |
| 31 | + |
| 32 | + public static final int c_MaxLabelLength = 50; |
| 33 | + public static final String methodName = "METHOD_NAME"; |
| 34 | + public static final String internalSeparator = "|"; |
| 35 | + |
| 36 | + public static String normalizeName(String original, String defaultString) { |
| 37 | + original = original.toLowerCase().replaceAll("\\\\n", "") // escaped new |
| 38 | + // lines |
| 39 | + .replaceAll("//s+", "") // whitespaces |
| 40 | + .replaceAll("[\"',]", "") // quotes, apostrophies, commas |
| 41 | + .replaceAll("\\P{Print}", ""); // unicode weird characters |
| 42 | + String stripped = original.replaceAll("[^A-Za-z]", ""); |
| 43 | + if (stripped.length() == 0) { |
| 44 | + String carefulStripped = original.replaceAll(" ", "_"); |
| 45 | + if (carefulStripped.length() == 0) { |
| 46 | + return defaultString; |
| 47 | + } else { |
| 48 | + return carefulStripped; |
| 49 | + } |
| 50 | + } else { |
| 51 | + return stripped; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static boolean isMethod(Node node) { |
| 56 | + String type = node.getUserData(Common.PropertyKey).getType(); |
| 57 | + |
| 58 | + return isMethod(node, type); |
| 59 | + } |
| 60 | + |
| 61 | + public static boolean isMethod(Node node, String type) { |
| 62 | + Property parentProperty = node.getParentNode().getUserData(Common.PropertyKey); |
| 63 | + if (parentProperty == null) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + String parentType = parentProperty.getType(); |
| 68 | + return Common.NameExpr.equals(type) && Common.MethodDeclaration.equals(parentType); |
| 69 | + } |
| 70 | + |
| 71 | + public static ArrayList<String> splitToSubtokens(String str1) { |
| 72 | + String str2 = str1.trim(); |
| 73 | + return Stream.of(str2.split("(?<=[a-z])(?=[A-Z])|_|[0-9]|(?<=[A-Z])(?=[A-Z][a-z])|\\s+")) |
| 74 | + .filter(s -> s.length() > 0).map(s -> Common.normalizeName(s, Common.EmptyString)) |
| 75 | + .filter(s -> s.length() > 0).collect(Collectors.toCollection(ArrayList::new)); |
| 76 | + } |
| 77 | +} |
0 commit comments