private List<String> getAliases(SingleMemberAnnotationExpr annotation) { List<String> aliases = new ArrayList<>(); if (annotation.getMemberValue() instanceof ArrayInitializerExpr) { ArrayInitializerExpr memberValue = (ArrayInitializerExpr) annotation.getMemberValue(); for (Expression expression : memberValue.getValues()) { aliases.add(expression.toString().replaceAll("\"", "")); } } return aliases; } }
protected void prepareReturnStmt(MethodDeclaration methodDeclaration, ReturnStmt returnStmt) { returnStmt.getExpression().ifPresent(expression -> { String returnStmtStr = expression.toString(); Matcher matcher = RETURN_STMT_PATTERN.matcher(returnStmtStr); if (!returnMap.containsKey(methodDeclaration.getNameAsString())) { returnMap.put(methodDeclaration.getNameAsString(), DfCollectionUtil.newArrayList()); } returnMap.get(methodDeclaration.getNameAsString()).add(matcher.find() ? matcher.group(0) : "##unanalyzable##"); }); } }
/** * Get the value of a {@link MemberValuePair} present in a list from its name. * * @param pairs The list of MemberValuePair * @param name The name of the MemberValuePair we want to get the value from. * @return The value of the MemberValuePair of given name, or null if it's not present in the list. */ public static String extractValueByName(List<MemberValuePair> pairs, String name){ for(MemberValuePair pair : pairs){ if(pair.getName().equals(name)){ return pair.getValue().toString(); } } return null; }
private List<String> getAliases(SingleMemberAnnotationExpr annotation) { List<String> aliases = new ArrayList<>(); if (annotation.getMemberValue() instanceof ArrayInitializerExpr) { ArrayInitializerExpr memberValue = (ArrayInitializerExpr) annotation.getMemberValue(); for (Expression expression : memberValue.getValues()) { aliases.add(expression.toString().replaceAll("\"", "")); } } return aliases; } }
private String createMemberParamValue(AnnotationExpr a) { Expression memberValue = a.asSingleMemberAnnotationExpr().getMemberValue(); if (memberValue.getClass().isAssignableFrom(StringLiteralExpr.class)) return memberValue.asStringLiteralExpr().asString(); if (memberValue.getClass().isAssignableFrom(NameExpr.class)) return memberValue.asNameExpr().getNameAsString(); throw new IllegalArgumentException(String.format("Javadoc param type (%s) not supported.", memberValue.toString())); }
private String createMemberParamValue(AnnotationExpr a) { Expression memberValue = a.asSingleMemberAnnotationExpr().getMemberValue(); if (memberValue.getClass().isAssignableFrom(StringLiteralExpr.class)) return memberValue.asStringLiteralExpr().asString(); if (memberValue.getClass().isAssignableFrom(NameExpr.class)) return memberValue.asNameExpr().getNameAsString(); throw new IllegalArgumentException(String.format("Javadoc param type (%s) not supported.", memberValue.toString())); }
private static final String getAnnotationParamValue(String packageName, Object expr, List<String> imports) { String str = expr.toString(); if (expr instanceof FieldAccessExpr) { FieldAccessExpr fae = (FieldAccessExpr) expr; String className = getClassName(packageName, fae.getScope().toString(), imports); return String.format("%s.%s", className, fae.getField()); } else if (expr instanceof IntegerLiteralExpr) { return str; } else if (expr instanceof LongLiteralExpr) { return str; } else if (expr instanceof DoubleLiteralExpr) { return str; } else if (expr instanceof StringLiteralExpr) { return str; } return null; } // =========================================================================
private static final String getAnnotationParamValue(String packageName, Object expr, List<String> imports) { String str = expr.toString(); if (expr instanceof FieldAccessExpr) { FieldAccessExpr fae = (FieldAccessExpr) expr; String className = getClassName(packageName, fae.getScope().toString(), imports); return String.format("%s.%s", className, fae.getField()); } else if (expr instanceof IntegerLiteralExpr) { return str; } else if (expr instanceof LongLiteralExpr) { return str; } else if (expr instanceof DoubleLiteralExpr) { return str; } else if (expr instanceof StringLiteralExpr) { return str; } return null; } // =========================================================================
/** * Process the $event variable passed on v-on. This variable must have a valid cast in front. * * @param expression The currently processed expression * @param nameExpr The variable we are processing * @param parameters The parameters this expression depends on */ private void processEventParameter(Expression expression, NameExpr nameExpr, List<VariableInfo> parameters) { Optional<Node> parentNode = nameExpr.getParentNode(); if (parentNode.isPresent() && parentNode.get() instanceof CastExpr) { CastExpr castExpr = (CastExpr) parentNode.get(); parameters.add(new VariableInfo(castExpr.getType().toString(), "$event")); } else { logger.error( "\"$event\" should always be casted to it's intended type. Example: @click=\"doSomething((Event) $event)\".", expression.toString()); } }
public SymbolReference<ResolvedTypeDeclaration> solve(ThisExpr node) { // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()) { // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()) { return SymbolReference.solved(clazz.getCorrespondingDeclaration()); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.findAncestor(CompilationUnit.class); if (cu.isPresent()) { Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()) { return SymbolReference.solved(getTypeDeclaration(classByName.get())); } } } return SymbolReference.solved(getTypeDeclaration(findContainingTypeDeclOrObjectCreationExpr(node))); }
public SymbolReference<ResolvedTypeDeclaration> solve(ThisExpr node){ // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()){ // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()){ return SymbolReference.solved(clazz.getCorrespondingDeclaration()); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.getAncestorOfType(CompilationUnit.class); if (cu.isPresent()){ Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()){ return SymbolReference.solved(getTypeDeclaration(classByName.get())); } } } return SymbolReference.solved(getTypeDeclaration(findContainingTypeDeclOrObjectCreationExpr(node))); }
@Override public ResolvedType visit(ThisExpr node, Boolean solveLambdas) { // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()){ // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()){ return new ReferenceTypeImpl(clazz.getCorrespondingDeclaration(),typeSolver); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.getAncestorOfType(CompilationUnit.class); if (cu.isPresent()){ Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()){ return new ReferenceTypeImpl(facade.getTypeDeclaration(classByName.get()), typeSolver); } } } return new ReferenceTypeImpl(facade.getTypeDeclaration(facade.findContainingTypeDeclOrObjectCreationExpr(node)), typeSolver); }
@Override public ResolvedType visit(ThisExpr node, Boolean solveLambdas) { // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()) { // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()) { return new ReferenceTypeImpl(clazz.getCorrespondingDeclaration(), typeSolver); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.findAncestor(CompilationUnit.class); if (cu.isPresent()) { Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()) { return new ReferenceTypeImpl(facade.getTypeDeclaration(classByName.get()), typeSolver); } } } return new ReferenceTypeImpl(facade.getTypeDeclaration(facade.findContainingTypeDeclOrObjectCreationExpr(node)), typeSolver); }
@Override public ResolvedType visit(ThisExpr node, Boolean solveLambdas) { // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()){ // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()){ return new ReferenceTypeImpl(clazz.getCorrespondingDeclaration(),typeSolver); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.getAncestorOfType(CompilationUnit.class); if (cu.isPresent()){ Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()){ return new ReferenceTypeImpl(facade.getTypeDeclaration(classByName.get()), typeSolver); } } } return new ReferenceTypeImpl(facade.getTypeDeclaration(facade.findContainingTypeDeclOrObjectCreationExpr(node)), typeSolver); }
public SymbolReference<ResolvedTypeDeclaration> solve(ThisExpr node){ // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()){ // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()){ return SymbolReference.solved(clazz.getCorrespondingDeclaration()); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.getAncestorOfType(CompilationUnit.class); if (cu.isPresent()){ Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()){ return SymbolReference.solved(getTypeDeclaration(classByName.get())); } } } return SymbolReference.solved(getTypeDeclaration(findContainingTypeDeclOrObjectCreationExpr(node))); }
private Optional<AnnotationImp> getAnnotationMethod(final AnnotationExpr an) { Optional<AnnotationImp> optional = Optional.empty(); final List<String> classes = this.findImportForClasses(an.getNameAsString()); for (final Class<?> clazz : this.loadClasses(classes)) { try { final AnnotationImp annotationImp = new AnnotationImp(clazz.asSubclass(Annotation.class)); annotationImp.location(LocationEnum.METHOD); an.ifNormalAnnotationExpr(normal -> { normal.getPairs().forEach(pair -> { annotationImp.property( pair.getNameAsString(), pair.getValue().toString().replaceAll("\"", "")); }); }); optional = Optional.ofNullable(annotationImp); break; } catch (final Exception e) { // Probably wrong import. } } return optional; }
private Optional<AnnotationImp> getAnnotationField(final AnnotationExpr an) { Optional<AnnotationImp> optional = Optional.empty(); final List<String> classes = this.findImportForClasses(an.getNameAsString()); for (final Class<?> clazz : this.loadClasses(classes)) { try { final AnnotationImp annotationImp = new AnnotationImp(clazz.asSubclass(Annotation.class)); annotationImp.location(LocationEnum.FIELD); an.ifNormalAnnotationExpr(normal -> { normal.getPairs().forEach(pair -> { annotationImp.property( pair.getNameAsString(), pair.getValue().toString().replaceAll("\"", "")); }); }); optional = Optional.ofNullable(annotationImp); break; } catch (final Exception e) { // Probably wrong import. } } return optional; }
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); try { Object result = engine.eval(annotation.getMemberValue().toString()); refactor(methodDeclaration, new StringLiteralExpr(result.toString()), annotation); } catch (ScriptException e) {
private void addStepToRegistry(Expression expression, MethodDeclaration methodDeclaration, SingleMemberAnnotationExpr annotation) { String parameterizedStep = expression.toString().replaceAll("\"", ""); String stepText = getStepText(parameterizedStep); stepValue = new StepValue(stepText, parameterizedStep); entry = new StepRegistryEntry(); entry.setName(methodDeclaration.getDeclarationAsString()); entry.setStepText(parameterizedStep); entry.setStepValue(stepValue); entry.setParameters(methodDeclaration.getParameters()); entry.setSpan(methodDeclaration.getRange()); entry.setHasAlias(hasAlias(annotation)); entry.setAliases(getAliases(annotation)); entry.setFileName(file); stepRegistry.addStep(stepValue, entry); }
private void addStepToRegistry(Expression expression, MethodDeclaration methodDeclaration, SingleMemberAnnotationExpr annotation) { String parameterizedStep = expression.toString().replaceAll("\"", ""); String stepText = getStepText(parameterizedStep); stepValue = new StepValue(stepText, parameterizedStep); entry = new StepRegistryEntry(); entry.setName(methodDeclaration.getDeclarationAsString()); entry.setStepText(parameterizedStep); entry.setStepValue(stepValue); entry.setParameters(methodDeclaration.getParameters()); entry.setSpan(methodDeclaration.getRange()); entry.setHasAlias(hasAlias(annotation)); entry.setAliases(getAliases(annotation)); entry.setFileName(file); stepRegistry.addStep(stepValue, entry); }