Skip to content

Commit

Permalink
Minor: No semantic changes, just a bunch of static Nullness checks an…
Browse files Browse the repository at this point in the history
…d some minor corrections
  • Loading branch information
JulianSchuette committed Jul 16, 2020
1 parent 90d6675 commit bf31210
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static void reset() {
INSTANCE = new TypeManager();
}

@NonNull
public Map<Type, List<Type>> getTypeState() {
return typeState;
}
Expand Down Expand Up @@ -182,7 +183,8 @@ private Set<Type> unwrapTypes(Collection<Type> types, WrapState wrapState) {
}
}

public Optional<Type> getCommonType(Collection<Type> types) {
@NonNull
public Optional<Type> getCommonType(@NonNull Collection<Type> types) {

boolean sameType =
types.stream().map(t -> t.getClass().getCanonicalName()).collect(Collectors.toSet()).size()
Expand Down Expand Up @@ -286,6 +288,7 @@ private Set<Ancestor> getAncestors(RecordDeclaration record, int depth) {
return ancestors;
}

@NonNull
public Language getLanguage() {
if (frontend instanceof JavaLanguageFrontend) {
return Language.JAVA;
Expand Down
55 changes: 32 additions & 23 deletions src/main/java/de/fraunhofer/aisec/cpg/graph/type/TypeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
*/
public class TypeParser {

private TypeParser() {
throw new IllegalStateException("Do not instantiate the TypeParser");
}

public static final String UNKNOWN_TYPE_STRING = "UNKNOWN";
private static final List<String> primitives =
List.of("byte", "short", "int", "long", "float", "double", "boolean", "char");
Expand All @@ -65,6 +61,10 @@ private TypeParser() {
private static final String ELABORATED_TYPE_UNION = "union";
private static final String ELABORATED_TYPE_ENUM = "enum";

private TypeParser() {
throw new IllegalStateException("Do not instantiate the TypeParser");
}

public static void reset() {
TypeParser.languageSupplier = () -> TypeManager.getInstance().getLanguage();
}
Expand Down Expand Up @@ -237,7 +237,7 @@ private static Matcher getFunctionPtrMatcher(@NonNull List<String> type) {
typeStringBuilder.append(typePart);
}

String typeString = typeStringBuilder.toString().strip();
String typeString = typeStringBuilder.toString().trim();

Matcher matcher = functionPtrRegex.matcher(typeString);
if (matcher.find()) {
Expand All @@ -247,7 +247,7 @@ private static Matcher getFunctionPtrMatcher(@NonNull List<String> type) {
}

private static boolean isIncompleteType(String typeName) {
return typeName.strip().equals("void");
return typeName.trim().equals("void");
}

private static boolean isUnknownType(String typeName) {
Expand All @@ -261,7 +261,8 @@ private static boolean isUnknownType(String typeName) {
* @param type typeString
* @return typeString without spaces in the generic Expression
*/
private static String fixGenerics(String type) {
@NonNull
private static String fixGenerics(@NonNull String type) {
StringBuilder out = new StringBuilder();
int bracketCount = 0;
int iterator = 0;
Expand Down Expand Up @@ -303,7 +304,7 @@ private static String fixGenerics(String type) {
}

private static void processBlockUntilLastSplit(
String type, int lastSplit, int newPosition, List<String> typeBlocks) {
@NonNull String type, int lastSplit, int newPosition, @NonNull List<String> typeBlocks) {
String substr = type.substring(lastSplit, newPosition);
if (substr.length() != 0) {
typeBlocks.add(substr);
Expand All @@ -323,9 +324,9 @@ public static List<String> separate(@NonNull String type) {
type = type.replace("::", ".");
type = type.split("=")[0];

// Guarantee that there is no arbitraty number of whitespces
// Guarantee that there is no arbitrary number of whitespaces
String[] typeSubpart = type.split(" ");
type = String.join(" ", typeSubpart).strip();
type = String.join(" ", typeSubpart).trim();

List<String> typeBlocks = new ArrayList<>();

Expand Down Expand Up @@ -395,13 +396,13 @@ public static List<String> separate(@NonNull String type) {

private static List<Type> getParameterList(String parameterList) {
if (parameterList.startsWith("(") && parameterList.endsWith(")")) {
parameterList = parameterList.strip().substring(1, parameterList.strip().length() - 1);
parameterList = parameterList.trim().substring(1, parameterList.trim().length() - 1);
}
List<Type> parameters = new ArrayList<>();
String[] parametersSplit = parameterList.split(",");
for (String parameter : parametersSplit) {
if (parameter.length() > 0) {
parameters.add(createFrom(parameter.strip(), true));
parameters.add(createFrom(parameter.trim(), true));
}
}

Expand All @@ -414,7 +415,7 @@ private static List<Type> getGenerics(String typeName) {
List<Type> genericList = new ArrayList<>();
String[] parametersSplit = generics.split(",");
for (String parameter : parametersSplit) {
genericList.add(createFrom(parameter.strip(), true));
genericList.add(createFrom(parameter.trim(), true));
}

return genericList;
Expand Down Expand Up @@ -461,12 +462,13 @@ private static Type performBracketContentAction(Type finalType, String part) {
* Makes sure to apply Expressions containing brackets that change the binding of operators e.g.
* () can change the binding order of operators
*
* @param finalType Modifications are applyed to this type which is the result of the preceding
* @param finalType Modifications are applied to this type which is the result of the preceding
* type calculations
* @param bracketExpressions List of Strings containing bracket expressions
* @return modified finalType performing the resolution of the bracket expressions
*/
private static Type resolveBracketExpression(Type finalType, List<String> bracketExpressions) {
private static Type resolveBracketExpression(
@NonNull Type finalType, @NonNull List<String> bracketExpressions) {
for (String bracketExpression : bracketExpressions) {
List<String> splitExpression =
separate(bracketExpression.substring(1, bracketExpression.length() - 1));
Expand All @@ -479,13 +481,13 @@ private static Type resolveBracketExpression(Type finalType, List<String> bracke
}

/**
* Help function that removes access modifier from the typeString
* Helper function that removes access modifier from the typeString.
*
* @param type provided typeString
* @return typeString without access modifier
*/
private static String clear(String type) {
return type.replaceAll("public|private|protected", "").strip();
private static String clear(@NonNull String type) {
return type.replaceAll("public|private|protected", "").trim();
}

/**
Expand All @@ -495,7 +497,7 @@ private static String clear(String type) {
* @param stringList
* @return
*/
private static boolean isPrimitiveType(List<String> stringList) {
private static boolean isPrimitiveType(@NonNull List<String> stringList) {
for (String s : stringList) {
if (primitives.contains(s)) {
return true;
Expand All @@ -511,7 +513,8 @@ private static boolean isPrimitiveType(List<String> stringList) {
* @param typeBlocks
* @return separated words of compound types are joined into one string
*/
private static List<String> joinPrimitive(List<String> typeBlocks) {
@NonNull
private static List<String> joinPrimitive(@NonNull List<String> typeBlocks) {
List<String> joinedTypeBlocks = new ArrayList<>();
StringBuilder primitiveType = new StringBuilder();
boolean foundPrimitive = false;
Expand Down Expand Up @@ -547,7 +550,8 @@ private static List<String> joinPrimitive(List<String> typeBlocks) {
* @param newRoot root the chain is swapped with
* @return oldchain but root replaced with newRoot
*/
public static Type reWrapType(Type oldChain, Type newRoot) {
@NonNull
public static Type reWrapType(@NonNull Type oldChain, @NonNull Type newRoot) {
if (oldChain.isFirstOrderType()) {
newRoot.setTypeOrigin(oldChain.getTypeOrigin());
}
Expand Down Expand Up @@ -582,12 +586,16 @@ public static Type reWrapType(Type oldChain, Type newRoot) {
* @param string the string representation of the type
* @return the type
*/
public static Type createIgnoringAlias(String string) {
@NonNull
public static Type createIgnoringAlias(@NonNull String string) {
return createFrom(string, false);
}

@NonNull
private static Type postTypeParsing(
List<String> subPart, Type finalType, List<String> bracketExpressions) {
@NonNull List<String> subPart,
@NonNull Type finalType,
@NonNull List<String> bracketExpressions) {
for (String part : subPart) {
if (part.equals("*")) {
// Creates a Pointer to the finalType
Expand Down Expand Up @@ -665,6 +673,7 @@ private static ObjectType.Modifier determineModifier(
* @param resolveAlias should replace with original type in typedefs
* @return new type representing the type string
*/
@NonNull
public static Type createFrom(@NonNull String type, boolean resolveAlias) {
// Check if Problems during Parsing
if (type.contains("?")
Expand Down

0 comments on commit bf31210

Please sign in to comment.