@Override public JCExpression classReference( Inliner inliner, CharSequence topLevelClazz, CharSequence fullyQualifiedClazz) { if (Refaster.class.getName().contentEquals(fullyQualifiedClazz)) { // Special handling to ensure that the pretty-printer always recognizes Refaster references return inliner.maker().Ident(inliner.asName("Refaster")); } String packge = topLevelClazz.toString(); int lastDot = packge.lastIndexOf('.'); packge = (lastDot >= 0) ? packge.substring(0, lastDot) : ""; PackageSymbol currentPackage = inliner.getContext().get(PackageSymbol.class); if (currentPackage == null || !currentPackage.getQualifiedName().contentEquals(packge) || !topLevelClazz.toString().contentEquals(fullyQualifiedClazz)) { // don't import classes from the same package as the class we're refactoring inliner.addImport(fullyQualifiedClazz.toString()); } String simpleName = fullyQualifiedClazz.toString(); simpleName = simpleName.substring(simpleName.lastIndexOf('.') + 1); return inliner.maker().Ident(inliner.asName(simpleName)); }
if (currentPackage == null || !currentPackage.getQualifiedName().contentEquals(packge)) {
Id(int value, @Nullable Symbol rSymbol) { this.value = value; if (rSymbol != null) { ClassName className = ClassName.get(rSymbol.packge().getQualifiedName().toString(), R, rSymbol.enclClass().name.toString()); String resourceName = rSymbol.name.toString(); this.code = className.topLevelClassName().equals(ANDROID_R) ? CodeBlock.of("$L.$N", className, resourceName) : CodeBlock.of("$T.$N", className, resourceName); this.qualifed = true; } else { this.code = CodeBlock.of("$L", value); this.qualifed = false; } }
if (currentPackage == null || !currentPackage.getQualifiedName().contentEquals(packge)) {
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!MATCHER.matches(tree, state)) { return NO_MATCH; } if (state.getTypes().closure(ASTHelpers.getSymbol(tree).enclClass().asType()).stream() .anyMatch( s -> s.asElement() .packge() .getQualifiedName() .toString() .startsWith("java.util.concurrent"))) { return NO_MATCH; } if (blockEndsInBreakOrReturn(state)) { return NO_MATCH; } ExpressionTree collection = getReceiver(tree); if (collection == null) { return NO_MATCH; } if (!enclosingLoop(state.getPath(), collection)) { return NO_MATCH; } return describeMatch(tree); }
@Override public JCExpression classReference( Inliner inliner, CharSequence topLevelClazz, CharSequence fullyQualifiedClazz) { if (Refaster.class.getName().contentEquals(fullyQualifiedClazz)) { // Special handling to ensure that the pretty-printer always recognizes Refaster references return inliner.maker().Ident(inliner.asName("Refaster")); } String packge = topLevelClazz.toString(); int lastDot = packge.lastIndexOf('.'); packge = (lastDot >= 0) ? packge.substring(0, lastDot) : ""; PackageSymbol currentPackage = inliner.getContext().get(PackageSymbol.class); if (currentPackage == null || !currentPackage.getQualifiedName().contentEquals(packge) || !topLevelClazz.equals(fullyQualifiedClazz)) { // don't import classes from the same package as the class we're refactoring inliner.addImport(fullyQualifiedClazz.toString()); } String simpleName = fullyQualifiedClazz.toString(); simpleName = simpleName.substring(simpleName.lastIndexOf('.') + 1); return inliner.maker().Ident(inliner.asName(simpleName)); }
Id(int value, @Nullable Symbol rSymbol) { this.value = value; if (rSymbol != null) { ClassName className = ClassName.get(rSymbol.packge().getQualifiedName().toString(), R, rSymbol.enclClass().name.toString()); String resourceName = rSymbol.name.toString(); this.code = className.topLevelClassName().equals(ANDROID_R) ? CodeBlock.of("$L.$N", className, resourceName) : CodeBlock.of("$T.$N", className, resourceName); this.qualifed = true; } else { this.code = CodeBlock.of("$L", value); this.qualifed = false; } }
/** * Get package name. */ public String qualifiedName() { if (qualifiedName == null) { Name fullname = sym.getQualifiedName(); // Some bogus tests depend on the interned "" being returned. // See 6457276. qualifiedName = fullname.isEmpty() ? "" : fullname.toString(); } return qualifiedName; }
/** Return true if the given symbol is defined in the current package. */ public static boolean inSamePackage(Symbol targetSymbol, VisitorState state) { JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit(); PackageSymbol usePackage = compilationUnit.packge; PackageSymbol targetPackage = targetSymbol.packge(); return targetPackage != null && usePackage != null && targetPackage.getQualifiedName().equals(usePackage.getQualifiedName()); }
private static boolean symbolInSupportLibrary(Symbol sym) { String packageName = ASTHelpers.enclosingPackage(sym).getQualifiedName().toString(); return packageName.startsWith(PACKAGE_ANDROID_SUPPORT) || packageName.startsWith(PACKAGE_ANDROIDX) || packageName.startsWith(PACKAGE_MATERIAL); }
isDefinitionScope = topLevel.packge.getQualifiedName().toString().startsWith(JSweetConfig.LIBS_PACKAGE + "."); context.topLevelPackageNames.add(topLevelPackage.getQualifiedName().toString());
s = cuName.split("\\."); cuName = s[0]; String javaSourceFileRelativeFullName = (cu.packge.getQualifiedName().toString().replace(".", File.separator) + File.separator + cuName + ".java"); files[i].javaSourceDirRelativeFile = new File(javaSourceFileRelativeFullName); cu.getSourceFile().getName().length() - javaSourceFileRelativeFullName.length())); String packageName = isNoRootDirectories() ? context.getRootRelativeJavaName(cu.packge) : cu.packge.getQualifiedName().toString(); String outputFileRelativePathNoExt = packageName.replace(".", File.separator) + File.separator + cuName; String outputFileRelativePath = outputFileRelativePathNoExt
@Override public void visitApply(JCMethodInvocation invocation) { // TODO: same for static variables if (invocation.meth instanceof JCIdent && JSweetConfig.TS_STRICT_MODE_KEYWORDS.contains(invocation.meth.toString().toLowerCase())) { PackageSymbol invocationPackage = (PackageSymbol) ((JCIdent) invocation.meth).sym .getEnclosingElement().getEnclosingElement(); String rootRelativeInvocationPackageName = getRootRelativeName(invocationPackage); if (rootRelativeInvocationPackageName.indexOf('.') == -1) { super.visitApply(invocation); return; } String targetRootPackageName = rootRelativeInvocationPackageName.substring(0, rootRelativeInvocationPackageName.indexOf('.')); String pathToReachRootPackage = Util.getRelativePath( "/" + compilationUnit.packge.getQualifiedName().toString().replace('.', '/'), "/" + targetRootPackageName); if (pathToReachRootPackage == null) { super.visitApply(invocation); return; } File moduleFile = new File(new File(pathToReachRootPackage), JSweetConfig.MODULE_FILE_NAME); if (!invocationPackage.toString().equals(compilationUnit.packge.getSimpleName().toString())) { useModule(false, invocationPackage, invocation, targetRootPackageName, moduleFile.getPath().replace('\\', '/'), null); } } super.visitApply(invocation); }
@Override public void visitTopLevel(JCCompilationUnit compilationUnit) { currentTopLevel = compilationUnit; if (pass == 1) { getGraph().add(compilationUnit); } else { if (compilationUnit.packge.getQualifiedName().toString().startsWith(JSweetConfig.LIBS_PACKAGE + ".")) { // skip definitions return; } currentTopLevelImportedTypes.clear(); for (JCImport i : compilationUnit.getImports()) { if (i.qualid.type != null) { currentTopLevelImportedTypes.add(i.qualid.type); } // TypeSymbol type = Util.getImportedType(i); // if (type != null) { // JCCompilationUnit target = typesToCompilationUnits.get(type); // if (target != null && getGraph().contains(target)) { // logger.debug("adding import dependency: " + // currentTopLevel.getSourceFile() + " -> " + // target.getSourceFile()); // getGraph().addEdge(target, currentTopLevel); // } // } } } super.visitTopLevel(compilationUnit); currentTopLevel = null; }
private boolean checkRootPackageParent(JCCompilationUnit topLevel, PackageSymbol rootPackage, PackageSymbol parentPackage) { if (parentPackage == null) { return true; } if (!context.options.isNoRootDirectories() || context.options.isBundle()) { return true; } if (context.isRootPackage(parentPackage)) { report(topLevel.getPackageName(), JSweetProblem.ENCLOSED_ROOT_PACKAGES, rootPackage.getQualifiedName().toString(), parentPackage.getQualifiedName().toString()); return false; } for (Symbol s : parentPackage.getEnclosedElements()) { if (s instanceof ClassSymbol) { if (Util.isSourceElement(s)) { report(topLevel.getPackageName(), JSweetProblem.CLASS_OUT_OF_ROOT_PACKAGE_SCOPE, s.getQualifiedName().toString(), rootPackage.getQualifiedName().toString()); return false; } } } return checkRootPackageParent(topLevel, rootPackage, (PackageSymbol) parentPackage.owner); }
private String getStaticContainerFullName(JCImport importDecl) { if (importDecl.getQualifiedIdentifier() instanceof JCFieldAccess) { JCFieldAccess fa = (JCFieldAccess) importDecl.getQualifiedIdentifier(); String name = context.getRootRelativeJavaName(fa.selected.type.tsym); // function is a top-level global function (no need to import) if (JSweetConfig.GLOBALS_CLASS_NAME.equals(name)) { return null; } boolean globals = name.endsWith("." + JSweetConfig.GLOBALS_CLASS_NAME); if (globals) { name = name.substring(0, name.length() - JSweetConfig.GLOBALS_CLASS_NAME.length() - 1); } // function belong to the current package (no need to import) if (compilationUnit.packge.getQualifiedName().toString().startsWith(name)) { return null; } return name; } return null; }
@Override public void visitTopLevel(JCCompilationUnit topLevel) { if (topLevel.packge.getQualifiedName().toString().startsWith(JSweetConfig.LIBS_PACKAGE + ".")) { return; } this.compilationUnit = topLevel; super.visitTopLevel(topLevel); }
private void initSourceFileJavaPaths(SourceFile file, JCCompilationUnit cu) { String[] s = cu.getSourceFile().getName().split(File.separator.equals("\\") ? "\\\\" : File.separator); String cuName = s[s.length - 1]; s = cuName.split("\\."); cuName = s[0]; String javaSourceFileRelativeFullName = (cu.packge.getQualifiedName().toString().replace(".", File.separator) + File.separator + cuName + ".java"); file.javaSourceDirRelativeFile = new File(javaSourceFileRelativeFullName); file.javaSourceDir = new File(cu.getSourceFile().getName().substring(0, cu.getSourceFile().getName().length() - javaSourceFileRelativeFullName.length())); }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!MATCHER.matches(tree, state)) { return NO_MATCH; } if (state.getTypes().closure(ASTHelpers.getSymbol(tree).enclClass().asType()).stream() .anyMatch( s -> s.asElement() .packge() .getQualifiedName() .toString() .startsWith("java.util.concurrent"))) { return NO_MATCH; } if (blockEndsInBreakOrReturn(state)) { return NO_MATCH; } ExpressionTree collection = getReceiver(tree); if (collection == null) { return NO_MATCH; } if (!enclosingLoop(state.getPath(), collection)) { return NO_MATCH; } return describeMatch(tree); }
/** Return true if the given symbol is defined in the current package. */ public static boolean inSamePackage(Symbol targetSymbol, VisitorState state) { JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit(); PackageSymbol usePackage = compilationUnit.packge; PackageSymbol targetPackage = targetSymbol.packge(); return targetPackage != null && usePackage != null && targetPackage.getQualifiedName().equals(usePackage.getQualifiedName()); }