/** * Uses the AST Visitor to check if the Methods are according to the conventions. * * @param f - Receives the opened File * @param identifier - Receives the identifier to obtain the convention service of the list. */ public void visitorOfMethod(final File f, final int identifier) { v = new ASTVisitor() { @Override public boolean visit(MethodDeclaration node) { if (!node.isConstructor()) { String id = node.getName().getFullyQualifiedName(); if (lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getCondition()) { javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } if (lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getCondition()) { javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } } return true; } }; javaServices.parseFile(f, v); }
/** * Uses the AST Visitor to check if the constants are according to the conventions. * * @param f - Receives the opened File * @param identifier - Receives the identifier to obtain the convention service of the list. */ public void visitorOfConstants(final File f, final int identifier) { v = new ASTVisitor() { public boolean visit(VariableDeclarationFragment node) { String id = node.getName().getFullyQualifiedName(); if (lista .get(identifier) .verifyConvention(id, TypeOf.CONSTANTS) .getCondition()) { // checkVariableLowerCase javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.CONSTANTS).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } if (lista .get(identifier) .verifyConvention(id, TypeOf.CONSTANTS) .getCondition()) { // checkVariableDollar javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.CONSTANTS).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } if (lista .get(identifier) .verifyConvention(id, TypeOf.CONSTANTS) .getCondition()) { // checkVariableUnderScore javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.CONSTANTS).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } return true; }; }; javaServices.parseFile(f, v); }
/** * Uses the AST Visitor to check if the Enumerate is according to the conventions. * * @param f - Receives the opened File * @param identifier - Receives the identifier to obtain the convention service of the list. */ public void visitorOfEnum(final File f, final int identifier) { v = new ASTVisitor() { public boolean visit(EnumConstantDeclaration node) { String id = node.getName().getFullyQualifiedName(); if (lista.get(identifier).verifyConvention(id, TypeOf.ENUM).getCondition()) { javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.ENUM).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } return true; } }; javaServices.parseFile(f, v); }
@Override public void action() { JavaEditorServices editor; ServiceReference<JavaEditorServices> ref = CommandsController.getContext().getServiceReference(JavaEditorServices.class); editor = CommandsController.getContext().getService(ref); File f = editor.getOpenedFile(); try { byte[] encoded = Files.readAllBytes(Paths.get(f.toURI())); String n = new String(encoded, Charset.defaultCharset()); ITextSelection selection = editor.getTextSelected(f); if (selection.getLength() == 0) { // Comment single line int cursorPosition = editor.getCursorPosition(); int inicioDalinha = n.substring(0, cursorPosition).lastIndexOf("\r\n") + 4; String p = n.substring(0, inicioDalinha) + "//" + n.substring(inicioDalinha, n.length()); editor.setText(f, p); editor.selectText( f, inicioDalinha - 2, ((p.substring(inicioDalinha, p.length()).indexOf("\r\n"))) + 4); editor.saveFile(f); } else { // Comment block int positionSelection = selection.getOffset(); int lastPositionSelection = selection.getOffset() + selection.getLength(); int firstLineOfBlock = n.substring(0, positionSelection).lastIndexOf("\r\n") + 4; int lastLineOfBlock = lastPositionSelection + n.substring(lastPositionSelection, n.length()).indexOf("\r\n") + 4; String commentedBlock = ""; for (String line : n.substring(firstLineOfBlock, lastLineOfBlock).split("\r\n")) { commentedBlock = commentedBlock + commentLine(line); } String finalResult = n.substring(0, firstLineOfBlock); finalResult = finalResult + commentedBlock; finalResult = finalResult + n.substring(lastLineOfBlock, n.length()); editor.setText(f, finalResult); editor.saveFile(f); // Esta linha esta preparada para seleccionar todo o bloco que foi comentado, // No entanto, parece que internamente não é possível utilizar o selectAndReveal para // multiple lines, // Procurou-se no google e não se encontrou solução. Sendo que aparece apenas highlighted a // primeira do bloco editor.selectText(f, firstLineOfBlock - 2, (lastLineOfBlock - firstLineOfBlock)); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Uses the AST Visitor to check if the methods that are being filtered with the modifier are * according to the conventions * * @param f - Receives the opened File * @param m - Receives the Modifier * @param identifier - Receives the identifier to obtain the convention service of the list. */ public void visitorOfModifier(final File f, final int m, final int identifier) { if (!listaModifier.isEmpty()) { for (final FilterByModifier mod : listaModifier) { v = new ASTVisitor() { @Override public boolean visit(MethodDeclaration node) { if (!node.isConstructor()) { String id = node.getName().getFullyQualifiedName(); if (node.getModifiers() == mod.verificarModificadorMetodo()) { if (lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getCondition()) { javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } if (lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getCondition()) { javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } } } return true; } }; javaServices.parseFile(f, v); } } else { v = new ASTVisitor() { @Override public boolean visit(MethodDeclaration node) { if (!node.isConstructor()) { String id = node.getName().getFullyQualifiedName(); if (node.getModifiers() == m) { if (lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getCondition()) { javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } if (lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getCondition()) { javaServices.addAnnotation( f, AnnotationType.WARNING, lista.get(identifier).verifyConvention(id, TypeOf.METHOD).getWarning(), node.getName().getStartPosition(), node.getName().getLength()); } } } return true; } }; javaServices.parseFile(f, v); } }