public class Util extends Object
| Modifier and Type | Method and Description |
|---|---|
static void |
addFiles(Predicate<File> filter,
File file,
Collection<File> files)
Recursively adds files to the given list.
|
static void |
addFiles(String extension,
File file,
Collection<File> files)
Recursively adds files to the given list.
|
static boolean |
containsAssignableType(com.sun.tools.javac.code.Types types,
List<com.sun.tools.javac.code.Type> list,
com.sun.tools.javac.code.Type type)
Tells if the given list contains an type which is assignable from type.
|
static boolean |
containsFile(File dir,
File[] files)
Tells if the given directory or any of its sub-directory contains one of the
given files.
|
static boolean |
containsMethods(com.sun.tools.javac.tree.JCTree.JCClassDecl classDeclaration)
Tells if the given type declaration contains some method declarations.
|
static String |
escapeRegex(String regex)
This function will escape special characters within a string to ensure that
the string will not be parsed as a regular expression.
|
static void |
fillAllVariableAccesses(Map<String,com.sun.tools.javac.code.Symbol.VarSymbol> vars,
com.sun.tools.javac.tree.JCTree tree)
Fills the given map with all the variables beeing accessed within the given
code tree.
|
static void |
fillAllVariablesInScope(Map<String,com.sun.tools.javac.code.Symbol.VarSymbol> vars,
Stack<com.sun.tools.javac.tree.JCTree> scanningStack,
com.sun.tools.javac.tree.JCTree from,
com.sun.tools.javac.tree.JCTree to)
Finds all the variables accessible within the current scanning scope.
|
static void |
findDefaultMethodsInType(Set<Map.Entry<com.sun.tools.javac.tree.JCTree.JCClassDecl,com.sun.tools.javac.tree.JCTree.JCMethodDecl>> defaultMethods,
JSweetContext context,
com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol)
Fills the given set with all the default methods found in the current type
and super interfaces.
|
static com.sun.tools.javac.code.Symbol.VarSymbol |
findFieldDeclaration(com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol,
com.sun.tools.javac.util.Name name)
Finds the field in the given type that matches the given name.
|
static com.sun.tools.javac.code.Symbol |
findFirstDeclarationInClassAndSuperClasses(com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
String name,
ElementKind kind)
Find first declaration (of any kind) matching the given name.
|
static com.sun.tools.javac.code.Symbol |
findFirstDeclarationInType(Element typeSymbol,
String name)
Find first declaration (of any kind) matching the given name.
|
static com.sun.tools.javac.tree.JCTree.JCMethodDecl |
findFirstMethodDeclaration(com.sun.tools.javac.tree.JCTree.JCClassDecl typeDeclaration,
String methodName) |
static com.sun.tools.javac.code.Symbol.MethodSymbol |
findFirstMethodDeclarationInType(Element typeSymbol,
String methodName)
Finds first method matching name (no super types lookup).
|
static com.sun.tools.javac.code.Symbol.ClassSymbol |
findInnerClassDeclaration(com.sun.tools.javac.code.Symbol.ClassSymbol clazz,
String name)
Finds the first inner class declaration of the given name in the given class
hierarchy.
|
static com.sun.tools.javac.code.Symbol.MethodSymbol |
findMethodDeclarationInType(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
com.sun.tools.javac.tree.JCTree.JCMethodInvocation invocation)
Finds the method declaration within the given type, for the given invocation.
|
static com.sun.tools.javac.code.Symbol.MethodSymbol |
findMethodDeclarationInType(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
String methodName,
com.sun.tools.javac.code.Type.MethodType methodType)
Finds the method in the given type that matches the given name and signature.
|
static com.sun.tools.javac.code.Symbol.MethodSymbol |
findMethodDeclarationInType(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
String methodName,
com.sun.tools.javac.code.Type.MethodType methodType,
boolean overrides)
Finds the method in the given type that matches the given name and signature.
|
static List<com.sun.tools.javac.tree.JCTree.JCMethodDecl> |
findMethodDeclarations(com.sun.tools.javac.tree.JCTree.JCClassDecl typeDeclaration) |
static void |
findMethodDeclarationsInType(com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
Collection<String> methodNames,
Set<String> ignoredTypeNames,
List<com.sun.tools.javac.code.Symbol.MethodSymbol> result)
Finds methods by name.
|
static com.sun.tools.javac.tree.JCTree.JCVariableDecl |
findParameter(com.sun.tools.javac.tree.JCTree.JCMethodDecl method,
String name)
Finds and returns the parameter matching the given name if any.
|
static List<com.sun.tools.javac.tree.JCTree.JCClassDecl> |
findTypeDeclarationsInCompilationUnits(List<com.sun.tools.javac.tree.JCTree.JCCompilationUnit> compilationUnits) |
static com.sun.tools.javac.code.Symbol |
getAccessedSymbol(com.sun.tools.javac.tree.JCTree tree)
Gets the symbol on JCFieldAccess or JCIdent if possible, or return null.
|
static List<List<com.sun.tools.javac.tree.JCTree>> |
getExecutionPaths(com.sun.tools.javac.tree.JCTree.JCMethodDecl methodDeclaration) |
static String |
getFullMethodSignature(com.sun.tools.javac.code.Symbol.MethodSymbol method)
Gets the full signature of the given method.
|
static long |
getId()
Returns a unique id (incremental).
|
static com.sun.tools.javac.code.Symbol.TypeSymbol |
getImportedType(com.sun.tools.javac.tree.JCTree.JCImport i)
Gets the imported type (wether statically imported or not).
|
static com.sun.tools.javac.code.Symbol.PackageSymbol |
getPackageByName(JSweetContext context,
String qualifiedName)
Looks up a package element from its qualified name.
|
static String |
getRelativePath(String fromPath,
String toPath)
Gets the relative path that links the two given paths.
|
static String |
getRelativePath(com.sun.tools.javac.code.Symbol fromSymbol,
com.sun.tools.javac.code.Symbol toSymbol)
Get the relative path to reach a symbol from another one.
|
static List<com.sun.tools.javac.tree.JCTree.JCClassDecl> |
getSortedClassDeclarations(List<com.sun.tools.javac.tree.JCTree> decls)
Gets the inheritance-based sorted class declarations.
|
static String |
getSourceFilePath(Element element)
Gets the source file path of the given element.
|
static com.sun.tools.javac.code.Symbol.TypeSymbol |
getStaticImportTarget(com.sun.tools.javac.tree.JCTree.JCCompilationUnit compilationUnit,
String name)
Tells if the given symbol is statically imported in the given compilation
unit.
|
static com.sun.tools.javac.code.Symbol.ClassSymbol |
getTypeByName(JSweetContext context,
String qualifiedName)
Looks up a type element from its qualified name.
|
String |
getTypeInitalValue(String typeName)
Gets the TypeScript initial default value for a type.
|
static String |
getTypeInitialValue(com.sun.tools.javac.code.Type type)
Returns the literal for a given type inital value.
|
static boolean |
hasAbstractMethod(com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol)
Returns true if the given class declares an abstract method.
|
static boolean |
hasParent(com.sun.tools.javac.code.Symbol.ClassSymbol clazz,
String... qualifiedNamesToFind)
Recursively looks up one of the given types in the type hierachy of the given
class.
|
static boolean |
hasTypeParameters(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol)
Tells if the method uses a type parameter.
|
static boolean |
hasVarargs(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol)
Tells if the given method has varargs.
|
static boolean |
isArithmeticOperator(com.sun.source.tree.Tree.Kind kind)
Returns true is an arithmetic operator.
|
static boolean |
isArithmeticOrLogicalOperator(com.sun.source.tree.Tree.Kind kind)
Returns true is an arithmetic operator.
|
static boolean |
isAssignable(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Symbol.TypeSymbol to,
com.sun.tools.javac.code.Symbol.TypeSymbol from)
Tells if two type symbols are assignable.
|
static boolean |
isComparisonOperator(com.sun.source.tree.Tree.Kind kind)
Returns true is an comparison operator.
|
static boolean |
isConstant(com.sun.tools.javac.tree.JCTree.JCExpression expr)
Tells if the given expression is a constant.
|
static boolean |
isConstantOrNullField(com.sun.tools.javac.tree.JCTree.JCVariableDecl var)
Tells if that variable is a non-static final field initialized with a literal
value.
|
static boolean |
isCoreType(TypeMirror type)
Returns true is the type is a core.
|
static boolean |
isDeclarationOrSubClassDeclaration(Types types,
com.sun.tools.javac.code.Type.ClassType classType,
String searchedClassName) |
static boolean |
isDeclarationOrSubClassDeclarationBySimpleName(Types types,
com.sun.tools.javac.code.Type.ClassType classType,
String searchedClassSimpleName) |
static boolean |
isDeprecated(Element element) |
static boolean |
isGlobalsClassName(String qualifiedName)
Tells if this qualified name denotes a JSweet globals class.
|
static boolean |
isImported(com.sun.tools.javac.tree.JCTree.JCCompilationUnit compilationUnit,
com.sun.tools.javac.code.Symbol.TypeSymbol type)
Tells if the given type is imported in the given compilation unit.
|
static boolean |
isIntegral(com.sun.tools.javac.code.Type type)
Returns true is the type is an integral numeric value.
|
static boolean |
isInvocable(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Type.MethodType from,
com.sun.tools.javac.code.Type.MethodType target)
Tells if a method can be invoked with some given parameter types.
|
static boolean |
isLiteralExpression(com.sun.tools.javac.tree.JCTree.JCExpression expression) |
static boolean |
isNullLiteral(com.sun.tools.javac.tree.JCTree tree)
Tells if that tree is the null literal.
|
static boolean |
isNumber(TypeMirror type)
Returns true is the type is a number.
|
static boolean |
isOverridingBuiltInJavaObjectMethod(com.sun.tools.javac.code.Symbol.MethodSymbol method) |
static boolean |
isParent(com.sun.tools.javac.code.Symbol.TypeSymbol type,
com.sun.tools.javac.code.Symbol.TypeSymbol toFind)
Looks up a class in the given class hierarchy and returns true if found.
|
static boolean |
isSourceElement(Element element)
Tells if the given element is within the Java sources being compiled.
|
static boolean |
isVarargs(com.sun.tools.javac.tree.JCTree.JCVariableDecl varDecl)
Tells if this parameter declaration is varargs.
|
static <T> List<T> |
list(T... items)
Varargs to mutable list.
|
static com.sun.tools.javac.tree.JCTree |
lookupTree(JSweetContext context,
Element element)
Gets the tree that corresponds to the given element (this is a slow
implementation - do not use intensively).
|
static String |
removeExtension(String fileName)
Removes the extensions of the given file name.
|
static boolean |
scanMemberDeclarationsInType(com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
Set<String> ignoredTypeNames,
Function<Element,Boolean> scanner)
Scans member declarations in type hierachy.
|
static File |
toFile(JavaFileObject javaFileObject)
Gets the file from a Java file object.
|
static JavaFileObject |
toJavaFileObject(JavaFileManager fileManager,
File sourceFile)
Transforms a source file to a Java file object (used by javac).
|
static com.sun.tools.javac.util.List<JavaFileObject> |
toJavaFileObjects(JavaFileManager fileManager,
Collection<File> sourceFiles)
Transforms a list of source files to Java file objects (used by javac).
|
static <T> com.sun.tools.javac.util.List<T> |
toJCList(Iterable<T> collection)
Transforms a Java iterable to a javac list.
|
public static long getId()
public static boolean isSourceElement(Element element)
public static String getSourceFilePath(Element element)
public static com.sun.tools.javac.tree.JCTree lookupTree(JSweetContext context, Element element)
context - the transpiler's contextelement - the element to lookuppublic static void addFiles(String extension, File file, Collection<File> files)
extension - the extension to filter withfile - the root file/directory to look into recursivelyfiles - the list to add the files matching the extensionpublic static void addFiles(Predicate<File> filter, File file, Collection<File> files)
filter - the filter predicate to apply (only files matching the predicate
will be added)file - the root file/directory to look into recursivelyfiles - the list to add the files matching the extensionpublic static String getFullMethodSignature(com.sun.tools.javac.code.Symbol.MethodSymbol method)
public static boolean containsMethods(com.sun.tools.javac.tree.JCTree.JCClassDecl classDeclaration)
public static void fillAllVariablesInScope(Map<String,com.sun.tools.javac.code.Symbol.VarSymbol> vars, Stack<com.sun.tools.javac.tree.JCTree> scanningStack, com.sun.tools.javac.tree.JCTree from, com.sun.tools.javac.tree.JCTree to)
public static List<com.sun.tools.javac.tree.JCTree.JCClassDecl> findTypeDeclarationsInCompilationUnits(List<com.sun.tools.javac.tree.JCTree.JCCompilationUnit> compilationUnits)
public static List<com.sun.tools.javac.tree.JCTree.JCMethodDecl> findMethodDeclarations(com.sun.tools.javac.tree.JCTree.JCClassDecl typeDeclaration)
public static com.sun.tools.javac.tree.JCTree.JCMethodDecl findFirstMethodDeclaration(com.sun.tools.javac.tree.JCTree.JCClassDecl typeDeclaration,
String methodName)
public static void fillAllVariableAccesses(Map<String,com.sun.tools.javac.code.Symbol.VarSymbol> vars, com.sun.tools.javac.tree.JCTree tree)
public static com.sun.tools.javac.code.Symbol.MethodSymbol findMethodDeclarationInType(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
com.sun.tools.javac.tree.JCTree.JCMethodInvocation invocation)
public static com.sun.tools.javac.code.Symbol.MethodSymbol findMethodDeclarationInType(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
String methodName,
com.sun.tools.javac.code.Type.MethodType methodType)
public static com.sun.tools.javac.code.Symbol.MethodSymbol findMethodDeclarationInType(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
String methodName,
com.sun.tools.javac.code.Type.MethodType methodType,
boolean overrides)
public static void findMethodDeclarationsInType(com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
Collection<String> methodNames,
Set<String> ignoredTypeNames,
List<com.sun.tools.javac.code.Symbol.MethodSymbol> result)
public static com.sun.tools.javac.code.Symbol.MethodSymbol findFirstMethodDeclarationInType(Element typeSymbol, String methodName)
public static boolean isDeprecated(Element element)
public static com.sun.tools.javac.code.Symbol findFirstDeclarationInType(Element typeSymbol, String name)
public static com.sun.tools.javac.code.Symbol findFirstDeclarationInClassAndSuperClasses(com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
String name,
ElementKind kind)
public static boolean scanMemberDeclarationsInType(com.sun.tools.javac.code.Symbol.TypeSymbol typeSymbol,
Set<String> ignoredTypeNames,
Function<Element,Boolean> scanner)
public static com.sun.tools.javac.tree.JCTree.JCVariableDecl findParameter(com.sun.tools.javac.tree.JCTree.JCMethodDecl method,
String name)
public static boolean isInvocable(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Type.MethodType from,
com.sun.tools.javac.code.Type.MethodType target)
types - a reference to the types in the compilation scopefrom - the caller method signature to test (contains the parameter types)target - the callee method signaturepublic String getTypeInitalValue(String typeName)
public static void findDefaultMethodsInType(Set<Map.Entry<com.sun.tools.javac.tree.JCTree.JCClassDecl,com.sun.tools.javac.tree.JCTree.JCMethodDecl>> defaultMethods, JSweetContext context, com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol)
public static com.sun.tools.javac.code.Symbol.VarSymbol findFieldDeclaration(com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol,
com.sun.tools.javac.util.Name name)
public static boolean isGlobalsClassName(String qualifiedName)
public static boolean isVarargs(com.sun.tools.javac.tree.JCTree.JCVariableDecl varDecl)
public static File toFile(JavaFileObject javaFileObject)
public static <T> com.sun.tools.javac.util.List<T> toJCList(Iterable<T> collection)
public static com.sun.tools.javac.util.List<JavaFileObject> toJavaFileObjects(JavaFileManager fileManager, Collection<File> sourceFiles) throws IOException
IOExceptionpublic static JavaFileObject toJavaFileObject(JavaFileManager fileManager, File sourceFile) throws IOException
IOExceptionpublic static String escapeRegex(String regex)
regex - - argument which we wish to escape.@SafeVarargs public static final <T> List<T> list(T... items)
public static boolean isAssignable(com.sun.tools.javac.code.Types types,
com.sun.tools.javac.code.Symbol.TypeSymbol to,
com.sun.tools.javac.code.Symbol.TypeSymbol from)
public static boolean containsAssignableType(com.sun.tools.javac.code.Types types,
List<com.sun.tools.javac.code.Type> list,
com.sun.tools.javac.code.Type type)
public static String getRelativePath(com.sun.tools.javac.code.Symbol fromSymbol, com.sun.tools.javac.code.Symbol toSymbol)
fromSymbol - the start pathtoSymbol - the end pathgetRelativePath(String, String)public static String getRelativePath(String fromPath, String toPath)
assertEquals("../c", Util.getRelativePath("/a/b", "/a/c"));
assertEquals("..", Util.getRelativePath("/a/b", "/a"));
assertEquals("../e", Util.getRelativePath("/a/b/c", "/a/b/e"));
assertEquals("d", Util.getRelativePath("/a/b/c", "/a/b/c/d"));
assertEquals("d/e", Util.getRelativePath("/a/b/c", "/a/b/c/d/e"));
assertEquals("../../../d/e/f", Util.getRelativePath("/a/b/c", "/d/e/f"));
assertEquals("../..", Util.getRelativePath("/a/b/c", "/a"));
assertEquals("..", Util.getRelativePath("/a/b/c", "/a/b"));
Thanks to: http://mrpmorris.blogspot.com/2007/05/convert-absolute-path-to-relative- path.html
Bug fix: Renaud Pawlak
fromPath - the path to start fromtoPath - the path to reachpublic static String removeExtension(String fileName)
fileName - the given file name (can contain path)public static boolean containsFile(File dir, File[] files)
dir - the directory to look intofiles - the files to be foundpublic static boolean isIntegral(com.sun.tools.javac.code.Type type)
public static boolean isNumber(TypeMirror type)
public static boolean isCoreType(TypeMirror type)
public static boolean isArithmeticOrLogicalOperator(com.sun.source.tree.Tree.Kind kind)
public static boolean isArithmeticOperator(com.sun.source.tree.Tree.Kind kind)
public static boolean isComparisonOperator(com.sun.source.tree.Tree.Kind kind)
public static boolean isParent(com.sun.tools.javac.code.Symbol.TypeSymbol type,
com.sun.tools.javac.code.Symbol.TypeSymbol toFind)
public static boolean hasParent(com.sun.tools.javac.code.Symbol.ClassSymbol clazz,
String... qualifiedNamesToFind)
public static com.sun.tools.javac.code.Symbol.PackageSymbol getPackageByName(JSweetContext context, String qualifiedName)
public static com.sun.tools.javac.code.Symbol.ClassSymbol getTypeByName(JSweetContext context, String qualifiedName)
public static boolean hasVarargs(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol)
public static boolean hasTypeParameters(com.sun.tools.javac.code.Symbol.MethodSymbol methodSymbol)
public static boolean isImported(com.sun.tools.javac.tree.JCTree.JCCompilationUnit compilationUnit,
com.sun.tools.javac.code.Symbol.TypeSymbol type)
public static com.sun.tools.javac.code.Symbol.TypeSymbol getStaticImportTarget(com.sun.tools.javac.tree.JCTree.JCCompilationUnit compilationUnit,
String name)
public static com.sun.tools.javac.code.Symbol.TypeSymbol getImportedType(com.sun.tools.javac.tree.JCTree.JCImport i)
public static boolean isConstant(com.sun.tools.javac.tree.JCTree.JCExpression expr)
public static boolean isNullLiteral(com.sun.tools.javac.tree.JCTree tree)
public static boolean isConstantOrNullField(com.sun.tools.javac.tree.JCTree.JCVariableDecl var)
public static String getTypeInitialValue(com.sun.tools.javac.code.Type type)
public static com.sun.tools.javac.code.Symbol getAccessedSymbol(com.sun.tools.javac.tree.JCTree tree)
public static boolean hasAbstractMethod(com.sun.tools.javac.code.Symbol.ClassSymbol classSymbol)
public static boolean isOverridingBuiltInJavaObjectMethod(com.sun.tools.javac.code.Symbol.MethodSymbol method)
public static List<com.sun.tools.javac.tree.JCTree.JCClassDecl> getSortedClassDeclarations(List<com.sun.tools.javac.tree.JCTree> decls)
This method aims at overcoming TypeScrit limitation that forces a parent class to be declared before its child class (it is not the case in Java). So far this is a partial implementation that should cover most cases... for a 100% coverage we would need a much more complicated implementation that is probably not worth it.
public static com.sun.tools.javac.code.Symbol.ClassSymbol findInnerClassDeclaration(com.sun.tools.javac.code.Symbol.ClassSymbol clazz,
String name)
public static boolean isLiteralExpression(com.sun.tools.javac.tree.JCTree.JCExpression expression)
public static List<List<com.sun.tools.javac.tree.JCTree>> getExecutionPaths(com.sun.tools.javac.tree.JCTree.JCMethodDecl methodDeclaration)
public static boolean isDeclarationOrSubClassDeclaration(Types types, com.sun.tools.javac.code.Type.ClassType classType, String searchedClassName)