本文整理汇总了Java中japa.parser.ast.CompilationUnit类的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit类的具体用法?Java CompilationUnit怎么用?Java CompilationUnit使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CompilationUnit类属于japa.parser.ast包,在下文中一共展示了CompilationUnit类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Fact
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
public Fact(File file, CompilationUnit compilationUnit) throws FileNotFoundException {
if (compilationUnit.getPackage() != null)
packageName = compilationUnit.getPackage().getName().toString().replace("package", "");
comment = extractTopComment(file);
imports = new HashSet<String>();
if (compilationUnit.getImports() != null)
for (ImportDeclaration imp : compilationUnit.getImports()) {
String str = imp.getName().toString();
if (!imp.isAsterisk())
str = str.substring(0, str.lastIndexOf("."));
imports.add(str);
}
declarations = new ArrayList<Declaration>();
if (compilationUnit.getTypes() != null)
for (TypeDeclaration decl : compilationUnit.getTypes()) {
if (decl instanceof ClassOrInterfaceDeclaration)
declarations.add(new Declaration(decl));
}
}
示例2: main
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
if (args.length != 2) {
System.out.println("usage: inputFile (.java) outputFile (.json)");
System.exit(1);
}
File inputFile = new File(args[0]);
String factFile = args[1];
CompilationUnit compilationUnit = JavaParser.parse(inputFile);
Fact fact = new Fact(inputFile, compilationUnit);
FileWriter writer = new FileWriter(factFile);
writer.write(gson.toJson(fact));
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
示例3: getResolvedName
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
public static ClassOrInterfaceType getResolvedName(final JavaType target,
final JavaType current, final CompilationUnit compilationUnit) {
final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(target,
compilationUnit.getImports(), current);
final ClassOrInterfaceType resolvedName = JavaParserUtils
.getClassOrInterfaceType(nameExpr);
if (current.getParameters() != null
&& current.getParameters().size() > 0) {
resolvedName.setTypeArgs(new ArrayList<Type>());
for (final JavaType param : current.getParameters()) {
resolvedName.getTypeArgs().add(
getResolvedName(target, param, compilationUnit));
}
}
return resolvedName;
}
示例4: locateTypeDeclaration
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
/**
* Searches a compilation unit and locates the declaration with the given
* type's simple name.
*
* @param compilationUnit to scan (required)
* @param javaType the target to locate (required)
* @return the located type declaration or null if it could not be found
*/
public static TypeDeclaration locateTypeDeclaration(
final CompilationUnit compilationUnit, final JavaType javaType) {
Validate.notNull(compilationUnit, "Compilation unit required");
Validate.notNull(javaType, "Java type to search for required");
if (compilationUnit.getTypes() == null) {
return null;
}
for (final TypeDeclaration candidate : compilationUnit.getTypes()) {
if (javaType.getSimpleTypeName().equals(candidate.getName())) {
// We have the required type declaration
return candidate;
}
}
return null;
}
示例5: getCompilationUnitContents
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
public final String getCompilationUnitContents(
final ClassOrInterfaceTypeDetails cid) {
Validate.notNull(cid, "Class or interface type details are required");
// Create a compilation unit to store the type to be created
final CompilationUnit compilationUnit = new CompilationUnit();
// NB: this import list is replaced at the end of this method by a
// sorted version
compilationUnit.setImports(new ArrayList<ImportDeclaration>());
if (!cid.getName().isDefaultPackage()) {
compilationUnit.setPackage(new PackageDeclaration(ASTHelper
.createNameExpr(cid.getName().getPackage()
.getFullyQualifiedPackageName())));
}
// Add the class of interface declaration to the compilation unit
final List<TypeDeclaration> types = new ArrayList<TypeDeclaration>();
compilationUnit.setTypes(types);
updateOutput(compilationUnit, null, cid, null);
return compilationUnit.toString();
}
示例6: JavaParserClassOrInterfaceTypeDetailsBuilder
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
/**
* Constructor
*
* @param compilationUnit
* @param enclosingCompilationUnitServices
* @param typeDeclaration
* @param declaredByMetadataId
* @param typeName
* @param metadataService
* @param typeLocationService
*/
private JavaParserClassOrInterfaceTypeDetailsBuilder(
final CompilationUnit compilationUnit,
final CompilationUnitServices enclosingCompilationUnitServices,
final TypeDeclaration typeDeclaration,
final String declaredByMetadataId, final JavaType typeName,
final MetadataService metadataService,
final TypeLocationService typeLocationService) {
// Check
Validate.notNull(compilationUnit, "Compilation unit required");
Validate.notBlank(declaredByMetadataId,
"Declared by metadata ID required");
Validate.notNull(typeDeclaration,
"Unable to locate the class or interface declaration");
Validate.notNull(typeName, "Name required");
// Assign
this.compilationUnit = compilationUnit;
compilationUnitServices = enclosingCompilationUnitServices == null ? getDefaultCompilationUnitServices()
: enclosingCompilationUnitServices;
this.declaredByMetadataId = declaredByMetadataId;
this.metadataService = metadataService;
name = typeName;
this.typeDeclaration = typeDeclaration;
this.typeLocationService = typeLocationService;
}
示例7: resolve
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
@Override
public NodeData resolve(Node node, String mappedClass) {
if (!(node instanceof ClassOrInterfaceDeclaration)) {
throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration");
}
final TypeDeclaration dclr = (TypeDeclaration) node;
if (!(dclr.getParentNode() instanceof CompilationUnit)) {
//handling nested classes
return null;
}
final String name = dclr.getName();
final Collection<FieldDescriptor> primaryKeyDescriptors = getPrimaryKeyDescriptors(mappedClass);
if (primaryKeyDescriptors != null && primaryKeyDescriptors.size() > 1 && nodeContainsPkFields(dclr,
primaryKeyDescriptors)) {
final NodeAndImports<ClassOrInterfaceDeclaration> primaryKeyClass = createPrimaryKeyClass(name, primaryKeyDescriptors);
final String pkClassName = primaryKeyClass.node.getName();
return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr(name + "." + pkClassName + ".class")),
new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false), primaryKeyClass.imprts, primaryKeyClass.node);
}
return null;
}
示例8: resolve
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
@Override
public NodeData resolve(Node node, String mappedClass) {
if (!(node instanceof ClassOrInterfaceDeclaration)) {
throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration");
}
final TypeDeclaration dclr = (TypeDeclaration) node;
if (!(dclr.getParentNode() instanceof CompilationUnit)) {
//handling nested classes
return null;
}
final String name = dclr.getName();
final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString();
final String enclosingClass = pckg + "." + name;
final ClassDescriptor cd = OjbUtil.findClassDescriptor(enclosingClass, descriptorRepositories);
if (cd != null) {
return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)),
new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false));
}
return null;
}
示例9: resolve
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
@Override
public final NodeData resolve(Node node, String mappedClass) {
if (!(node instanceof FieldDeclaration)) {
throw new IllegalArgumentException("this annotation belongs only on FieldDeclaration");
}
final FieldDeclaration field = (FieldDeclaration) node;
if (ResolverUtil.canFieldBeAnnotated(field)) {
final TypeDeclaration dclr = (TypeDeclaration) node.getParentNode();
final String name = dclr.getName();
final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString();
final String fullyQualifiedClass = pckg + "." + name;
final boolean mappedColumn = OjbUtil.isMappedColumn(mappedClass, ParserUtil.getFieldName(field),
descriptorRepositories);
if (mappedColumn) {
return getAnnotationNodes(fullyQualifiedClass, ParserUtil.getFieldName(field), mappedClass);
}
}
return null;
}
示例10: resolve
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
@Override
public NodeData resolve(Node node, String mappedClass) {
if (!(node instanceof ClassOrInterfaceDeclaration)) {
throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration");
}
final TypeDeclaration dclr = (TypeDeclaration) node;
if (!(dclr.getParentNode() instanceof CompilationUnit)) {
//handling nested classes
return null;
}
final String name = dclr.getName();
final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString();
final String enclosingClass = pckg + "." + name;
if (!enclosingClass.equals(mappedClass)) {
return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)),
new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false));
}
return null;
}
示例11: resolve
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
@Override
public NodeData resolve(Node node, String mappedClass) {
if (!(node instanceof ClassOrInterfaceDeclaration)) {
throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration");
}
final TypeDeclaration dclr = (TypeDeclaration) node;
if (!(dclr.getParentNode() instanceof CompilationUnit)) {
//handling nested classes
return null;
}
final String name = dclr.getName();
final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString();
final String enclosingClass = pckg + "." + name;
final Collection<String> customizedFieldsOnNode = getFieldsOnNode(dclr, getCustomizedFields(mappedClass));
if (customizedFieldsOnNode == null || customizedFieldsOnNode.isEmpty()) {
LOG.info(ResolverUtil.logMsgForClass(enclosingClass, mappedClass) + " has no customized fields");
return null;
}
return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr("CreateCustomizerFor" + customizedFieldsOnNode.toString())),
new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false));
}
示例12: getTypeOrFieldNameForMsg
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
private String getTypeOrFieldNameForMsg(final BodyDeclaration n) {
if (n instanceof TypeDeclaration) {
return ((TypeDeclaration) n).getName();
} else if (n instanceof FieldDeclaration) {
final FieldDeclaration fd = (FieldDeclaration) n;
//this wont work for nested classes but we should be in nexted classes at this point
final CompilationUnit unit = getCompilationUnit(n);
final TypeDeclaration parent = unit.getTypes().get(0);
Collection<String> variableNames = new ArrayList<String>();
if (fd.getVariables() != null) {
for (VariableDeclarator vd : fd.getVariables()) {
variableNames.add(vd.getId().getName());
}
}
return variableNames.size() == 1 ?
parent.getName() + "." + variableNames.iterator().next() :
parent.getName() + "." + variableNames.toString();
}
return null;
}
示例13: parseType
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
private JType parseType(String type, JCodeModel codeModel) {
final String text = "public class Ignored extends " + type + " {}";
try {
CompilationUnit compilationUnit = JavaParser.parse(
new ByteArrayInputStream(text.getBytes("UTF-8")), "UTF-8");
final List<TypeDeclaration> typeDeclarations = compilationUnit
.getTypes();
final TypeDeclaration typeDeclaration = typeDeclarations.get(0);
final ClassOrInterfaceDeclaration classDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
final List<ClassOrInterfaceType> _extended = classDeclaration
.getExtends();
final ClassOrInterfaceType classOrInterfaceType = _extended.get(0);
return classOrInterfaceType.accept(
this.typeToJTypeConvertingVisitor, codeModel);
} catch (ParseException pex) {
throw new IllegalArgumentException(
"Could not parse the type definition [" + type + "].", pex);
} catch (UnsupportedEncodingException uex) {
throw new UnsupportedOperationException(uex);
}
}
示例14: getNode
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
public static Node getNode(CompilationUnit unit, String fqName) {
String pkg = getQualifiedName(unit.getPackage().getName());
if (!fqName.startsWith(pkg)) {
return null;
}
if (fqName.equals(pkg)) {
return unit.getPackage();
}
String name = fqName.substring(pkg.length() + 1);
for (TypeDeclaration typeDecl : unit.getTypes()) {
if (name.startsWith(typeDecl.getName())) {
if (name.length() == typeDecl.getName().length()) {
return typeDecl;
} else {
return getNode(typeDecl, name.substring(typeDecl.getName().length() + 1));
}
}
}
return null;
}
示例15: getDeclaration
import japa.parser.ast.CompilationUnit; //导入依赖的package包/类
public static FieldDeclaration getDeclaration(CompilationUnit unit, Field field) {
TypeDeclaration typeDecl = getDeclaration(unit, field.getDeclaringClass());
if (typeDecl == null) {
return null;
}
for (BodyDeclaration decl : typeDecl.getMembers()) {
if (decl instanceof FieldDeclaration) {
FieldDeclaration fieldDecl = (FieldDeclaration) decl;
List<VariableDeclarator> variables = fieldDecl.getVariables();
if (variables.size() != 1) {
continue;
}
if (field.getName().equals(variables.get(0).getId().getName())) {
return fieldDecl;
}
}
}
return null;
}