List of usage examples for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameAT
int TokenNameAT
To view the source code for org.eclipse.jdt.core.compiler ITerminalSymbols TokenNameAT.
Click Source Link
From source file:org.j2eespider.util.AnnotationUtil.java
License:Open Source License
/** * Replace annotations in method.//w w w .j a v a2s . c o m */ public static void replaceAnnotationsInMethod(IMethod imethod, List<ValidatorType> annotations, String textNewAnnotations) { try { //monta o scanner do metodo IBuffer methodBuffer = imethod.getOpenable().getBuffer(); ISourceRange sourceRange = imethod.getSourceRange(); ISourceRange javadocRange = null;//imethod.getJavadocRange(); ISourceRange nameRange = imethod.getNameRange(); IScanner scanner = null; // delay initialization if (sourceRange != null && nameRange != null) { if (scanner == null) { scanner = ToolFactory.createScanner(false, false, true, false); scanner.setSource(methodBuffer.getCharacters()); } scanner.resetTo(sourceRange.getOffset(), nameRange.getOffset()); } //apaga todas as annotations que esto em annotationsType e adiciona textNewAnnotations StringBuffer sourceMethod = new StringBuffer(); int tok = scanner.getNextToken(); while (tok != ITerminalSymbols.TokenNameprivate && tok != ITerminalSymbols.TokenNameprotected && tok != ITerminalSymbols.TokenNamepublic) { //loop nas annotations if (tok == ITerminalSymbols.TokenNameAT) { //encontrou o inicio de uma annotation StringBuffer thisAnnotation = new StringBuffer("@"); tok = scanner.getNextToken(); //trabalha todo o contedo da annotation while (tok != ITerminalSymbols.TokenNameAT && tok != ITerminalSymbols.TokenNameprivate && tok != ITerminalSymbols.TokenNameprotected && tok != ITerminalSymbols.TokenNamepublic) { //pega todo o conteudo desta annotation thisAnnotation.append(scanner.getCurrentTokenSource()); //pega o nome dessa annotation tok = scanner.getNextToken(); } //verifica se para apagar essa annotation (s joga no novo sourceMethod se ela no tiver na lista que para apagar) if (!ValidatorUtil.containsValidator(annotations, thisAnnotation.toString())) { sourceMethod.append(thisAnnotation); } } } //grava o resto do metodo int posStartMethod = scanner.getCurrentTokenStartPosition(); int lengthRemoved = posStartMethod - sourceRange.getOffset(); //conta quantos caracteres foram removidos desse metodo (as annotations) String codeRemain = String.valueOf(scanner.getSource()).substring(posStartMethod, posStartMethod + sourceRange.getLength() - lengthRemoved); if (!sourceMethod.toString().equals("") && sourceMethod.toString().lastIndexOf("\n") != sourceMethod.toString().length() - 1) { //se j tem alguma annotation, ve se precisa de quebra de linha sourceMethod.append("\n\t"); } sourceMethod.append(textNewAnnotations); //adiciona as novas annotations antes do resto do mtodo sourceMethod.append(codeRemain); if (javadocRange != null) { //se tiver javadoc, no altera ele... methodBuffer.replace(sourceRange.getOffset() + javadocRange.getLength(), sourceRange.getLength() - javadocRange.getLength(), "\t" + sourceMethod.toString()); //altera o cdigo do mtodo } else { methodBuffer.replace(sourceRange.getOffset(), sourceRange.getLength(), sourceMethod.toString()); //altera o cdigo do mtodo } imethod.getOpenable().save(null, true); } catch (JavaModelException e) { } catch (InvalidInputException e) { } }
From source file:org.j2eespider.util.AnnotationUtil.java
License:Open Source License
private static boolean findAnnotation(IScanner scanner, String annotationName) throws InvalidInputException { String simpleName = Signature.getSimpleName(annotationName); StringBuffer buf = new StringBuffer(); int tok = scanner.getNextToken(); while (tok != ITerminalSymbols.TokenNameEOF) { if (tok == ITerminalSymbols.TokenNameAT) { buf.setLength(0);//from ww w .ja v a 2 s . co m tok = readName(scanner, buf); String name = buf.toString(); if (name.equals(annotationName) || name.equals(simpleName) || name.endsWith('.' + simpleName)) { return true; } } else { tok = scanner.getNextToken(); } } return false; }