private static CompilationUnit getCuBySource(String srcFile, String source, boolean recordable) { CompilationUnit cu = cuCache.get(srcFile); if (cu != null) { return cu; } cu = parse(source.toCharArray()); if (recordable) cu.recordModifications(); else cu.accept(new ExtraArrayDimensionRewriter()); cuCache.put(srcFile, cu); return cu; }
private static CompilationUnit getCuBySourceNoCache(String source, boolean recordable) { CompilationUnit cu = parse(source.toCharArray()); if (recordable) { cu.recordModifications(); } else { cu.accept(new ExtraArrayDimensionRewriter()); } return cu; }
public static String getSupertype(final Path path) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); return classType.getSuperclassType().toString(); }
/** * Returns name of the class, e.g. {@code FooBarBean} * * @param path path to java source file */ public static String getClassName(final Path path) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); return classType.getName().getIdentifier(); }
public static String getSupertype(final Path path) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); return classType.getSuperclassType().toString(); }
/** * Returns name of the class, e.g. {@code FooBarBean} * * @param path path to java source file */ public static String getClassName(final Path path) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); return classType.getName().getIdentifier(); }
Document document = new Document("import java.util.List;\n\nclass X\n{\n\n\tpublic void deleteme()\n\t{\n\t}\n\n}\n"); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); CompilationUnit cu = (CompilationUnit)parser.createAST(null); cu.recordModifications();
public static String addImport(final String source, final CharSequence importName) { final CompilationUnit unit = getCompilationUnit(source); unit.recordModifications(); final AST ast = unit.getAST(); addImport(unit, ast, importName); return rewrite(unit, ast); }
public static String addImport(final String source, final CharSequence importName) { final CompilationUnit unit = getCompilationUnit(source); unit.recordModifications(); final AST ast = unit.getAST(); addImport(unit, ast, importName); return rewrite(unit, ast); }
/** * Add import statement for given class * * @param path path of the java source class * @param importName name of the import e.g {@code java.util.List} */ @SuppressWarnings(UNCHECKED) public static void addImport(final Path path, final CharSequence importName) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final AST ast = unit.getAST(); addImport(unit, ast, importName); replaceFile(path, unit, ast); }
/** * Adds {@code HippoEssentialsGenerated} annotation to provided java source file (class level) * * @param path path to java source file */ public static void addHippoGeneratedBeanAnnotation(final Path path) { final String nodeJcrType = getNodeJcrType(path); if (nodeJcrType == null) { log.warn("Cannot generate internal name, jcrType was undefined"); return; } final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); final AST ast = unit.getAST(); addHippoGeneratedAnnotation(nodeJcrType, unit, classType, ast); replaceFile(path, unit, ast); }
/** * Add import statement for given class * * @param path path of the java source class * @param importName name of the import e.g {@code java.util.List} */ @SuppressWarnings(UNCHECKED) public static void addImport(final Path path, final CharSequence importName) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final AST ast = unit.getAST(); addImport(unit, ast, importName); replaceFile(path, unit, ast); }
/** * Adds {@code HippoEssentialsGenerated} annotation to provided java source file (class level) * * @param path path to java source file */ public static void addHippoGeneratedBeanAnnotation(final Path path) { final String nodeJcrType = getNodeJcrType(path); if (nodeJcrType == null) { log.warn("Cannot generate internal name, jcrType was undefined"); return; } final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); final AST ast = unit.getAST(); addHippoGeneratedAnnotation(nodeJcrType, unit, classType, ast); replaceFile(path, unit, ast); }
public static void annotateMethod(final EssentialsGeneratedMethod method, final Path path) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final AST ast = unit.getAST(); unit.accept(new ASTVisitor() { @SuppressWarnings(RAWTYPES) @Override public boolean visit(MethodDeclaration node) { final List parameters = node.parameters(); final List myParams = method.getMethodDeclaration().parameters(); if (!method.getMethodName().equals(node.getName().getIdentifier())) { log.debug("skipping {}", method.getMethodName()); return super.visit(node); } // TODO improve this check: we need to check if all parameters are equal / same type if (parameters != null && myParams != null && myParams.size() == parameters.size()) { // check method id: addHippoGeneratedAnnotation(method.getInternalName(), unit, node, ast); // rewrite file: replaceFile(path, unit, ast); } return super.visit(node); } }); }
/** * Returns name of the class, fully qualified e.g. {@code com.foo.BarBean} * * @param path path to java source file */ public static String getFullQualifiedClassName(final Path path) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); final String fullyQualifiedName = unit.getPackage().getName().getFullyQualifiedName(); final String identifier = classType.getName().getIdentifier(); return fullyQualifiedName + '.' + identifier; }
/** * Returns name of the class, fully qualified e.g. {@code com.foo.BarBean} * * @param path path to java source file */ public static String getFullQualifiedClassName(final Path path) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); final String fullyQualifiedName = unit.getPackage().getName().getFullyQualifiedName(); final String identifier = classType.getName().getIdentifier(); return fullyQualifiedName + '.' + identifier; }
ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setCompilerOptions(....); Document document = new Document(""); parser.setSource(document.get().toCharArray()); CompilationUnit unit = (CompilationUnit)parser.createAST(null); unit.recordModifications(); TextEdit edits = unit.rewrite(document, null); edits.apply(document); // now write document.get() to file
/** * Adds extends keyword to a class. If class already extends another class it will silently fail * * @param path path of java bean * @param extendingClassName name of the class bean is extending */ public static void addExtendsClass(final Path path, final String extendingClassName) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); final AST ast = unit.getAST(); final Type superclassType = classType.getSuperclassType(); if (superclassType == null) { classType.setSuperclassType(ast.newSimpleType(ast.newSimpleName(extendingClassName))); final String rewrite = rewrite(unit, ast); GlobalUtils.writeToFile(rewrite, path); } }
/** * Adds extends keyword to a class. If class already extends another class it will silently fail * * @param path path of java bean * @param extendingClassName name of the class bean is extending */ public static void addExtendsClass(final Path path, final String extendingClassName) { final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); final AST ast = unit.getAST(); final Type superclassType = classType.getSuperclassType(); if (superclassType == null) { classType.setSuperclassType(ast.newSimpleType(ast.newSimpleName(extendingClassName))); final String rewrite = rewrite(unit, ast); GlobalUtils.writeToFile(rewrite, path); } }
/** * Writes a component * * @param info component information * @param context plugin context * @return path of created file */ public static Path writeEssentialsComponent(final ComponentInformation info, final PluginContext context) { final File siteDirectory = context.getSiteDirectory(); final String sitePath = siteDirectory.getAbsolutePath(); final Path path = createJavaClass(sitePath, info.getTargetClassName(), info.getTargetPackageName(), EssentialConst.FILE_EXTENSION_JAVA); final CompilationUnit unit = getCompilationUnit(path); unit.recordModifications(); final TypeDeclaration classType = (TypeDeclaration) unit.types().get(0); final AST ast = unit.getAST(); unit.getPackage().setName(ast.newName(DOT_SPLITTER.split(info.getTargetPackageName()))); // import declaration essentials: final List<String> imports = info.getImports(); for (String anImport : imports) { addImport(unit, ast, anImport); } // add extends: classType.setSuperclassType(ast.newSimpleType(ast.newSimpleName(info.getExtendingComponentName()))); addHippoGeneratedAnnotation(info.getTargetClassName(), unit, classType, ast); replaceFile(path, unit, ast); return path; }