private void printImports(PrintWriter out, ClassNode classNode) { List<String> imports = new ArrayList<String>(); ModuleNode moduleNode = classNode.getModule(); for (ImportNode importNode : moduleNode.getStarImports()) { imports.add(importNode.getPackageName()); } for (ImportNode imp : moduleNode.getImports()) { if (imp.getAlias() == null) imports.add(imp.getType().getName()); } imports.addAll(Arrays.asList(ResolveVisitor.DEFAULT_IMPORTS)); for (String imp : imports) { String s = new StringBuilder() .append("import ") .append(imp) .append((imp.charAt(imp.length() - 1) == '.') ? "*;" : ";") .toString() .replace('$', '.'); out.println(s); } out.println(); }
private Expression findStaticFieldOrPropAccessorImportFromModule(String name) { ModuleNode module = currentClass.getModule(); if (module == null) return null; Map<String, ImportNode> importNodes = module.getStaticImports(); Expression expression; String accessorName = getAccessorName(name); // look for one of these: // import static MyClass.setProp [as setOtherProp] // import static MyClass.getProp [as getOtherProp] // when resolving prop reference if (importNodes.containsKey(accessorName)) { ImportNode importNode = importNodes.get(accessorName); expression = findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticPropertyAccessor( importNode.getType(), getPropNameForAccessor(importNode.getFieldName())); if (expression != null) return expression; } if (accessorName.startsWith("get")) { accessorName = "is" + accessorName.substring(3); if (importNodes.containsKey(accessorName)) { ImportNode importNode = importNodes.get(accessorName); expression = findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticPropertyAccessor( importNode.getType(), getPropNameForAccessor(importNode.getFieldName())); if (expression != null) return expression; } } // look for one of these: // import static MyClass.prop [as otherProp] // when resolving prop or field reference if (importNodes.containsKey(name)) { ImportNode importNode = importNodes.get(name); expression = findStaticPropertyAccessor(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticField(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; } // look for one of these: // import static MyClass.* // when resolving prop or field reference for (ImportNode importNode : module.getStaticStarImports().values()) { ClassNode node = importNode.getType(); expression = findStaticPropertyAccessor(node, name); if (expression != null) return expression; expression = findStaticField(node, name); if (expression != null) return expression; } return null; }
public List<ImportNode> getAllImportNodes() { if (sortedImports == null) { sortedImports = new ArrayList<ImportNode>(); sortedImports.addAll(module.getImports()); sortedImports.addAll(module.getStarImports()); sortedImports.addAll(module.getStaticImports().values()); sortedImports.addAll(module.getStaticStarImports().values()); Collections.sort(sortedImports, new ImportNodeComparator()); sortedImports = Collections.unmodifiableList(sortedImports); } return sortedImports; }
private void printTypeName(PrintWriter out, ClassNode type) { if (ClassHelper.isPrimitiveType(type)) { if (type == ClassHelper.boolean_TYPE) { out.print("boolean"); } else if (type == ClassHelper.char_TYPE) { out.print("char"); } else if (type == ClassHelper.int_TYPE) { out.print("int"); } else if (type == ClassHelper.short_TYPE) { out.print("short"); } else if (type == ClassHelper.long_TYPE) { out.print("long"); } else if (type == ClassHelper.float_TYPE) { out.print("float"); } else if (type == ClassHelper.double_TYPE) { out.print("double"); } else if (type == ClassHelper.byte_TYPE) { out.print("byte"); } else { out.print("void"); } } else { String name = type.getName(); // check for an alias ClassNode alias = currentModule.getImportType(name); if (alias != null) name = alias.getName(); out.print(name.replace('$', '.')); } }
public CompileUnit getCompileUnit() { if (redirect != null) return redirect().getCompileUnit(); if (compileUnit == null && module != null) { compileUnit = module.getUnit(); } return compileUnit; }
private Expression transformMapEntryExpression( MapEntryExpression me, ClassNode constructorCallType) { Expression key = me.getKeyExpression(); Expression value = me.getValueExpression(); ModuleNode module = currentClass.getModule(); if (module != null && key instanceof ConstantExpression) { Map<String, ImportNode> importNodes = module.getStaticImports(); if (importNodes.containsKey(key.getText())) { ImportNode importNode = importNodes.get(key.getText()); if (importNode.getType().equals(constructorCallType)) { String newKey = importNode.getFieldName(); return new MapEntryExpression( new ConstantExpression(newKey), value.transformExpression(this)); } } } return me; }
public void visitImports(ModuleNode node) { if (node != null) { for (ImportNode importNode : node.getImports()) { visitAnnotations(importNode); importNode.visit(this); } for (ImportNode importStarNode : node.getStarImports()) { visitAnnotations(importStarNode); importStarNode.visit(this); } for (ImportNode importStaticNode : node.getStaticImports().values()) { visitAnnotations(importStaticNode); importStaticNode.visit(this); } for (ImportNode importStaticStarNode : node.getStaticStarImports().values()) { visitAnnotations(importStaticStarNode); importStaticStarNode.visit(this); } } }
private Expression findStaticMethodImportFromModule(Expression method, Expression args) { ModuleNode module = currentClass.getModule(); if (module == null || !(method instanceof ConstantExpression)) return null; Map<String, ImportNode> importNodes = module.getStaticImports(); ConstantExpression ce = (ConstantExpression) method; Expression expression; Object value = ce.getValue(); // skip non-Strings, e.g. Integer if (!(value instanceof String)) return null; final String name = (String) value; // look for one of these: // import static SomeClass.method [as otherName] // when resolving methodCall() or getProp() or setProp() if (importNodes.containsKey(name)) { ImportNode importNode = importNodes.get(name); expression = findStaticMethod(importNode.getType(), importNode.getFieldName(), args); if (expression != null) return expression; expression = findStaticPropertyAccessorGivenArgs( importNode.getType(), getPropNameForAccessor(importNode.getFieldName()), args); if (expression != null) { return new StaticMethodCallExpression( importNode.getType(), importNode.getFieldName(), args); } } // look for one of these: // import static SomeClass.someProp [as otherName] // when resolving getProp() or setProp() if (validPropName(name)) { String propName = getPropNameForAccessor(name); if (importNodes.containsKey(propName)) { ImportNode importNode = importNodes.get(propName); expression = findStaticMethod( importNode.getType(), prefix(name) + capitalize(importNode.getFieldName()), args); if (expression != null) return expression; expression = findStaticPropertyAccessorGivenArgs( importNode.getType(), importNode.getFieldName(), args); if (expression != null) { return new StaticMethodCallExpression( importNode.getType(), prefix(name) + capitalize(importNode.getFieldName()), args); } } } Map<String, ImportNode> starImports = module.getStaticStarImports(); ClassNode starImportType; if (currentClass.isEnum() && starImports.containsKey(currentClass.getName())) { ImportNode importNode = starImports.get(currentClass.getName()); starImportType = importNode == null ? null : importNode.getType(); expression = findStaticMethod(starImportType, name, args); if (expression != null) return expression; } else { for (ImportNode importNode : starImports.values()) { starImportType = importNode == null ? null : importNode.getType(); expression = findStaticMethod(starImportType, name, args); if (expression != null) return expression; expression = findStaticPropertyAccessorGivenArgs(starImportType, getPropNameForAccessor(name), args); if (expression != null) { return new StaticMethodCallExpression(starImportType, name, args); } } } return null; }
private void sortClasses() throws CompilationFailedException { for (ModuleNode module : this.ast.getModules()) { module.sortClasses(); } }
/** * Adds a ClassNode directly to the unit (ie. without source). WARNING: the source is needed for * error reporting, using this method without setting a SourceUnit will cause NullPinterExceptions */ public void addClassNode(ClassNode node) { ModuleNode module = new ModuleNode(this.ast); this.ast.addModule(module); module.addClass(node); }
private List getPrimaryClassNodes(boolean sort) { if (sort == true) { List<ModuleNode> sortedModules = this.ast.getSortedModules(); if (sortedModules != null) { return sortedModules; } } // FIXASC (groovychange) rewritten /*old{ List unsorted = new ArrayList(); Iterator modules = this.ast.getModules().iterator(); while (modules.hasNext()) { ModuleNode module = (ModuleNode) modules.next(); Iterator classNodes = module.getClasses().iterator(); while (classNodes.hasNext()) { ClassNode classNode = (ClassNode) classNodes.next(); unsorted.add(classNode); } } */ // new List<ClassNode> unsorted = new ArrayList<ClassNode>(); for (ModuleNode module : this.ast.getModules()) { unsorted.addAll(module.getClasses()); } // FIXASC (groovychange) end if (!sort) return unsorted; // GRECLIPSE: start: rewritten sort algorithm /*old{ int[] indexClass = new int[unsorted.size()]; int[] indexInterface = new int[unsorted.size()]; { int i = 0; for (Iterator<ClassNode> iter = unsorted.iterator(); iter.hasNext(); i++) { ClassNode element = iter.next(); if (element.isInterface()) { indexInterface[i] = getSuperInterfaceCount(element); indexClass[i] = -1; } else { indexClass[i] = getSuperClassCount(element); indexInterface[i] = -1; } } } List<ClassNode> sorted = getSorted(indexInterface, unsorted); sorted.addAll(getSorted(indexClass, unsorted)); */ // newcode: // Sort them by how many types are in their hierarchy, but all interfaces first. // Algorithm: // Create a list of integers. Each integer captures the index into the unsorted // list (bottom 16bits) and the count of how many types are in that types // hierarchy (top 16bits). For classes the count is augmented by 2000 so that // when sorting the classes will come out after the interfaces. // This list of integers is sorted. We then just go through it and for the // lower 16bits of each entry (0xffff) that is the index of the next value to // pull from the unsorted list and put into the sorted list. // Will break down if more than 2000 interfaces in the type hierarchy for an // individual type, or a project contains > 65535 files... but if you've got // that kind of setup, you have other problems... List<Integer> countIndexPairs = new ArrayList<Integer>(); { int i = 0; for (Iterator iter = unsorted.iterator(); iter.hasNext(); i++) { ClassNode node = (ClassNode) iter.next(); if (node.isInterface()) { countIndexPairs.add((getSuperInterfaceCount(node) << 16) + i); } else { countIndexPairs.add(((getSuperClassCount(node) + 2000) << 16) + i); } } } Collections.sort(countIndexPairs); List sorted = new ArrayList(); for (int i : countIndexPairs) { sorted.add(unsorted.get(i & 0xffff)); } this.ast.setSortedModules(sorted); // end return sorted; }
// not available in 1.6 stream public static List<ImportNode> getStarImports(ModuleNode node) { return node.getStarImports(); }
// not available in 1.6 stream public static Map<String, ImportNode> getStaticStarImports(ModuleNode node) { return node.getStaticStarImports(); }
private Expression findStaticFieldOrPropAccessorImportFromModule(String name) { ModuleNode module = currentClass.getModule(); if (module == null) return null; Map<String, ImportNode> importNodes = module.getStaticImports(); Expression expression = null; String accessorName = getAccessorName(name); // look for one of these: // import static MyClass.setProp [as setOtherProp] // import static MyClass.getProp [as getOtherProp] // when resolving prop reference if (importNodes.containsKey(accessorName)) { ImportNode importNode = importNodes.get(accessorName); expression = findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticPropertyAccessor( importNode.getType(), getPropNameForAccessor(importNode.getFieldName())); if (expression != null) return expression; } if (accessorName.startsWith("get")) { accessorName = "is" + accessorName.substring(3); if (importNodes.containsKey(accessorName)) { ImportNode importNode = importNodes.get(accessorName); expression = findStaticPropertyAccessorByFullName(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; expression = findStaticPropertyAccessor( importNode.getType(), getPropNameForAccessor(importNode.getFieldName())); if (expression != null) return expression; } } // look for one of these: // import static MyClass.prop [as otherProp] // when resolving prop or field reference if (importNodes.containsKey(name)) { ImportNode importNode = importNodes.get(name); // GRECLIPSE add try { if (!isReconcile) { // GRECLIPSE end expression = findStaticPropertyAccessor(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; // GRECLIPSE add } // end expression = findStaticField(importNode.getType(), importNode.getFieldName()); if (expression != null) return expression; // GRECLIPSE add } finally { // store the identifier to facilitate organizing static imports if (expression != null) expression.setNodeMetaData("static.import.alias", name); } // GRECLIPSE end } // look for one of these: // import static MyClass.* // when resolving prop or field reference for (ImportNode importNode : module.getStaticStarImports().values()) { ClassNode node = importNode.getType(); expression = findStaticPropertyAccessor(node, name); if (expression != null) return expression; expression = findStaticField(node, name); if (expression != null) return expression; } return null; }
public void setModule(ModuleNode module) { redirect().module = module; if (module != null) { redirect().compileUnit = module.getUnit(); } }