本文整理匯總了Java中com.github.javaparser.ast.body.MethodDeclaration類的典型用法代碼示例。如果您正苦於以下問題:Java MethodDeclaration類的具體用法?Java MethodDeclaration怎麽用?Java MethodDeclaration使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MethodDeclaration類屬於com.github.javaparser.ast.body包,在下文中一共展示了MethodDeclaration類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: printMembers
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
private void printMembers(final NodeList<BodyDeclaration<?>> members, final Void arg) {
BodyDeclaration<?> prev = null;
members.sort((a, b) -> {
if (a instanceof FieldDeclaration && b instanceof CallableDeclaration) {
return 1;
} else if (b instanceof FieldDeclaration && a instanceof CallableDeclaration) {
return -1;
} else if (a instanceof MethodDeclaration && !((MethodDeclaration) a).getModifiers().contains(Modifier.STATIC) && b instanceof ConstructorDeclaration) {
return 1;
} else if (b instanceof MethodDeclaration && !((MethodDeclaration) b).getModifiers().contains(Modifier.STATIC) && a instanceof ConstructorDeclaration) {
return -1;
} else {
return 0;
}
});
for (final BodyDeclaration<?> member : members) {
if (prev != null && (!prev.isFieldDeclaration() || !member.isFieldDeclaration())) printer.println();
member.accept(this, arg);
printer.println();
prev = member;
}
}
示例2: MethodNode
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
MethodNode( MethodDeclaration metDec)
{
super();
this.metDec = metDec;
type = metDec.getType();
if(metDec.isPrivate())
modifier = "- ";
else if(metDec.isPublic())
modifier = "+ ";
else if(metDec.isProtected())
modifier = "# ";
else
modifier = "~ ";
nodeName = modifier + metDec.getName() + "(";
if (metDec.getParameters() != null)
{
parameters = new ArrayList<Parameter>(metDec.getParameters());
for (Parameter p : parameters)
nodeName = nodeName + " " + p + ", ";
if (nodeName.lastIndexOf(',') > 0 )
nodeName = nodeName.substring(0, nodeName.lastIndexOf(','));
}
nodeName = nodeName + ") : " + type.toString();
}
示例3: hasMain
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
/**
* A method to check whether the file has a main method in it or not
* @param file a parameter to take the file to check
* @return true if the file has a main method; false, if it hasn't
*/
public boolean hasMain( File file) {
int index = getRow( file);
if (index != -1) {
MutableTreeNode node = ((MutableTreeNode) rootNode.getChildAt(index));
if (node instanceof ClassNode) {
for (int i = 0; i < ((ClassNode) node).getChildCount(); i++) {
TreeNode treeNode = (((ClassNode) node).getChildAt(i));
if (treeNode instanceof MethodNode) {
MethodDeclaration metDec = ((MethodNode) treeNode).metDec;
if (metDec.getNameAsString().equals("main") &&
metDec.isPublic() && metDec.isStatic()) {
ArrayList<Parameter> parameters = new ArrayList<Parameter>(metDec.getParameters());
if (parameters.size() == 1 && parameters.get(0).toString().startsWith("String[]"))
return true;
}
}
}
}
}
return false;
}
示例4: getClassNameFromMethod
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
public String getClassNameFromMethod(MethodDeclaration n) {
Optional<ClassOrInterfaceDeclaration> methodClazzNode;
String methodClazzName = null;
logger.debug("Getting class name");
// Get the name of the class this method belongs to
methodClazzNode = n.getAncestorOfType(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.class);
if (methodClazzNode.isPresent()) {
methodClazzName = methodClazzNode.get().getNameAsString();
}
if (methodClazzName != null) {
logger.debug("Found class: " + methodClazzName);
} else {
logger.debug("Did not find class name.");
}
return methodClazzName;
}
示例5: testGetClassNameFromMethod
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
@Test
public void testGetClassNameFromMethod() {
//Code from: https://github.com/javaparser/javaparser/wiki/Manual
CompilationUnit cu = new CompilationUnit();
// set the package
cu.setPackageDeclaration(new PackageDeclaration(Name.parse("com.aspectsecurity.example")));
// or a shortcut
cu.setPackageDeclaration("com.aspectsecurity.example");
// create the type declaration
ClassOrInterfaceDeclaration type = cu.addClass("GeneratedClass");
// create a method
EnumSet<Modifier> modifiers = EnumSet.of(Modifier.PUBLIC);
MethodDeclaration method = new MethodDeclaration(modifiers, new VoidType(), "main");
modifiers.add(Modifier.STATIC);
method.setModifiers(modifiers);
type.addMember(method);
assertEquals("GeneratedClass", saa.getClassNameFromMethod(method));
}
示例6: visit
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
@Override
public void visit(final NormalAnnotationExpr expr, final Void arg) {
final String fqcn = expr.getName().toString();
if (fqcn.equals(Description.class.getCanonicalName())) {
final Node parent = expr.getParentNode();
final String value = expr.getPairs().get(0).toString();
if (parent instanceof ClassOrInterfaceDeclaration) {
descriptionAnotValue = value;
} else if (parent instanceof MethodDeclaration) {
methodDescriptions.put(((MethodDeclaration) parent).getName(), value);
}
} else if (fqcn.equals(ServiceInterfaceAnnotation.class.getCanonicalName())) {
String text1 = expr.getPairs().get(0).toString();
String text2 = expr.getPairs().get(1).toString();
if (text1.contains("value")) {
sieAnnotValue = text1;
sieAnnotOsgiRegistrationType = text2;
} else {
sieAnnotValue = text2;
sieAnnotOsgiRegistrationType = text1;
}
}
super.visit(expr, arg);
}
示例7: visit
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
@Override
public void visit(MethodDeclaration method, Object arg) {
if (isInModuleStack.isEmpty() || !isInModuleStack.peek()) {
return;
}
if (!hasProvideAnnotation(method)) {
return;
}
String type = method.getType().toString();
if (hasIntoSetAnnotation(method)) {
type = "Set<" + type + ">";
}
for (Parameter parameter : method.getParameters()) {
builder.addDependency(type, parameter.getType().toString());
}
}
示例8: visit
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
public void visit(MethodDeclaration n, Object arg) {
MethodDVO methodDVO = new MethodDVO();
methodDVO.setMethodName(n.getName());
methodDVO.setVisivility(GargoyleJavaParser.toStringVisibility(n.getModifiers()));
methodDVO.setDescription(n.getComment() != null ? n.getComment().toString() : "");
ArrayList<MethodParameterDVO> methodParameterDVOList = new ArrayList<>();
methodDVO.setMethodParameterDVOList(methodParameterDVOList);
List<Parameter> parameters = n.getParameters();
for (Parameter p : parameters) {
// String string2 = p.toString();
VariableDeclaratorId id2 = p.getId();
String varName = id2.getName();
Type type = p.getType();
String typeName = type.toString();
Comment comment = p.getComment();
methodParameterDVOList.add(new MethodParameterDVO(varName, typeName, "", comment == null ? "" : comment.toString()));
}
onMethodDVOVisite.accept(methodDVO);
super.visit(n, arg);
}
示例9: visit
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
public void visit(MethodDeclaration n, Object arg) {
String name = n.getName();
if (name.startsWith("set") || name.startsWith("get")) {
String fieldName = ValueUtil.getIndexLowercase(name.substring(3, name.length()), 0);
Pair<FieldMeta, Integer> pair = fieldMes.get(fieldName);
if (pair != null) {
int count = pair.getValue().intValue() + 1;
Pair<FieldMeta, Integer> createPair = createPair(pair.getKey(), count);
fieldMes.put(fieldName, createPair);
}
}
super.visit(n, arg);
}
示例10: getParameterNames
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
private void getParameterNames(MethodDeclaration methodDeclaration, boolean isInterface) {
final EnumSet<Modifier> modifiers = methodDeclaration.getModifiers();
if (isInterface || modifiers.contains(Modifier.PUBLIC)) {
String methodName = methodDeclaration.getName().getIdentifier();
List<Parameter> parameters = methodDeclaration.getParameters();
names.className = this.className;
List<List<ParameterName>> parameterNames =
names.names.computeIfAbsent(methodName, k -> new ArrayList<>(4));
final List<ParameterName> temp = new ArrayList<>();
for (final Parameter parameter : parameters) {
ParameterName parameterName = new ParameterName();
String type = parameter.getType().toString();
String name = parameter.getName().getIdentifier();
if (name.contains("[]")) {
type = type + "[]";
name = name.replace("[]", "");
}
parameterName.type = type;
parameterName.name = name;
temp.add(parameterName);
}
parameterNames.add(temp);
}
}
示例11: renderManyToOneAnnotations
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
private void renderManyToOneAnnotations(MethodDeclaration getter) {
/*
* We need JoinColumn and ManyToOne
*/
NormalAnnotationExpr m2o = createOrFindAnnotation(getter, "javax.persistence.ManyToOne");
MemberValuePair fetch = findAnnotationPair(m2o, "fetch");
if(null == fetch) {
importIf("javax.persistence.FetchType");
m2o.addPair("fetch", "FetchType.LAZY");
}
MemberValuePair p = findAnnotationPair(m2o, "optional");
if(null == p) {
m2o.addPair("optional", Boolean.toString(m_column.isNullable()));
} else {
p.setValue(new BooleanLiteralExpr(m_column.isNullable()));
}
NormalAnnotationExpr na = createOrFindAnnotation(getter, "javax.persistence.JoinColumn");
p = findAnnotationPair(na, "name");
if(null == p) {
na.addPair("name", "\"" + m_column.getName() + "\"");
} else {
p.setValue(new StringLiteralExpr(m_column.getName()));
}
}
示例12: changePropertyType
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
public void changePropertyType(ClassOrInterfaceType newType) {
VariableDeclarator vd = getVariableDeclaration();
if(null != vd) {
vd.setType(newType);
}
MethodDeclaration getter = getGetter();
if(null != getter) {
getter.setType(newType);
}
MethodDeclaration setter = getSetter();
if(null != setter) {
setter.getParameter(0).setType(newType);
}
m_propertyType = newType;
}
示例13: deleteColumn
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
void deleteColumn(ColumnWrapper cw) {
FieldDeclaration fieldDeclaration = cw.getFieldDeclaration();
if(fieldDeclaration != null) {
if(fieldDeclaration.getVariables().size() == 1) {
fieldDeclaration.remove();
} else {
cw.getVariableDeclaration().remove();
}
}
MethodDeclaration getter = cw.getGetter();
if(null != getter) {
getter.remove();
}
MethodDeclaration setter = cw.getSetter();
if(null != setter) {
setter.remove();
}
if(m_allColumnWrappers.remove(cw)) {
m_deletedColumns.add(cw);
}
}
示例14: isValidVoltProcedure
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
@Override
public boolean isValidVoltProcedure() {
final Optional<ClassOrInterfaceDeclaration> storedProc = getClassExtendingVoltProcedure();
final Optional<MethodDeclaration> runMethod = getRunMethod();
if(!storedProc.isPresent()) {
return false;
}
if(!runMethod.isPresent()) {
return false;
}
final Optional<String> returnType = getRunMethodReturnTypeAsString();
if(!returnType.isPresent() || !ProcReturnType.isValidJavaType(getRunMethodReturnTypeAsString().get())) {
return false;
}
// TODO - checks to do, eg throws the wrong type of chcekced exception, that class extending voltproc isn't abstract
return true;
}
示例15: solve
import com.github.javaparser.ast.body.MethodDeclaration; //導入依賴的package包/類
private void solve(Node node) {
if (node instanceof ClassOrInterfaceDeclaration) {
solveTypeDecl((ClassOrInterfaceDeclaration) node);
} else if (node instanceof Expression) {
if ((getParentNode(node) instanceof ImportDeclaration) || (getParentNode(node) instanceof Expression)
|| (getParentNode(node) instanceof MethodDeclaration)
|| (getParentNode(node) instanceof PackageDeclaration)) {
// skip
} else if ((getParentNode(node) instanceof Statement) || (getParentNode(node) instanceof VariableDeclarator)) {
try {
ResolvedType ref = JavaParserFacade.get(typeSolver).getType(node);
out.println(" Line " + node.getRange().get().begin.line + ") " + node + " ==> " + ref.describe());
ok++;
} catch (UnsupportedOperationException upe) {
unsupported++;
err.println(upe.getMessage());
throw upe;
} catch (RuntimeException re) {
ko++;
err.println(re.getMessage());
throw re;
}
}
}
}